Babel 7 doesn't change CONST to VAR

a 夏天 提交于 2020-06-10 08:45:04

问题


I have problem with Safari version <= 9. Babel doesn't seem to replace const with var.

I get this error in console:

Unexpected keyword 'const'. Const declarations are not supported in strict mode.

I tried using @babel/preset-stage-0 but babel removed it.

This is my app configuration:

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-transform-object-assign"
  ]
}

webpack.config.js

const path = require("path");
const webpack = require("webpack");
const componentName = "contact-captain";
const publicFolderRelativePath = "../../../../public/js";
const ignorePlugin = new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/);


module.exports = {
    // devtool: "source-map",
    output: {
        path: path.resolve(__dirname, publicFolderRelativePath),
        filename: `${componentName}.js`
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            }
        ]
    },
    plugins: [
        ignorePlugin
    ]
};

回答1:


did you try to configure preset-env ? you can find the browsers list here: https://github.com/browserslist/browserslist

probably need to add Safari 8 in your list...

["@babel/preset-env", {
  "targets": {
    "browsers": ["last 2 versions"],
  }
}]


来源:https://stackoverflow.com/questions/52362780/babel-7-doesnt-change-const-to-var

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!