运行webpack和babel时,生成的bundle.js仍然包含箭头功能。这在Internet Explorer 10中运行时给出了语法错误。我希望babel用IE可以运行的普通函数替换箭头函数。
我的 package.json 具有以下devDependencies:
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"css-loader": "^0.28.9",
"imports-loader": "^0.7.1",
"style-loader": "^0.19.1",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.2"
}
我的 webpack.config.js 如下所示:
module.exports = {
entry: [
'babel-polyfill',
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
],
},
resolve: {
enforceExtension: false,
extensions: ['.js', '.jsx']
},
devServer: {
host: '0.0.0.0',
port: 5000,
historyApiFallback: true,
contentBase: './'
}
};
我的 .babelrc 看起来像这样:
{
"presets":
[
["env", { "targets": {"browsers": ["last 2 versions"]}, "debug": true }],
"react",
"stage-3"
]
}
要进行转换,我运行命令:
npm run build --production
我在控制台中得到以下输出:
Using targets:
{
"chrome": "62",
"android": "4.4.3",
"edge": "15",
"firefox": "56",
"ie": "10",
"ios": "10.3",
"safari": "10.1"
}
Modules transform: commonjs
Using plugins:
check-es2015-constants {"android":"4.4.3","ie":"10"}
transform-es2015-arrow-functions {"android":"4.4.3","ie":"10"}
transform-es2015-block-scoped-functions {"android":"4.4.3","ie":"10"}
transform-es2015-block-scoping {"android":"4.4.3","ie":"10"}
transform-es2015-classes {"android":"4.4.3","ie":"10"}
transform-es2015-computed-properties {"android":"4.4.3","ie":"10"}
transform-es2015-destructuring {"android":"4.4.3","edge":"15","ie":"10"}
transform-es2015-duplicate-keys {"android":"4.4.3","ie":"10"}
transform-es2015-for-of {"android":"4.4.3","ie":"10"}
transform-es2015-function-name {"android":"4.4.3","edge":"15","ie":"10"}
transform-es2015-literals {"android":"4.4.3","ie":"10"}
transform-es2015-object-super {"android":"4.4.3","ie":"10"}
transform-es2015-parameters {"android":"4.4.3","ie":"10"}
transform-es2015-shorthand-properties {"android":"4.4.3","ie":"10"}
transform-es2015-spread {"android":"4.4.3","ie":"10"}
transform-es2015-sticky-regex {"android":"4.4.3","ie":"10"}
transform-es2015-template-literals {"android":"4.4.3","ie":"10"}
transform-es2015-typeof-symbol {"android":"4.4.3","ie":"10"}
transform-es2015-unicode-regex {"android":"4.4.3","ie":"10"}
transform-regenerator {"android":"4.4.3","ie":"10"}
transform-exponentiation-operator {"android":"4.4.3","ie":"10"}
transform-async-to-generator {"android":"4.4.3","ie":"10"}
syntax-trailing-function-commas {"android":"4.4.3","ie":"10"}
transform-es2015-arrow-functions列为包含,但是当我打开生成的bundle.js时,我可以看到以下内容:
...
function encoderForArrayFormat(options) {
switch (options.arrayFormat) {
case 'index':
return (key, value, index) => {
return value === null ? [
encode(key, options),
...
以上使用了箭头功能,并在Internet Explorer中产生语法错误。其他ES6的东西,如' ...'得到了解释。
我做错了什么?
答案 0 :(得分:29)
我认为问题与query-string有关,因为它是用ES6编写的,而不是转换为ES5。尝试将版本从6降级到5.
yarn add query-string@5.1.1
答案 1 :(得分:0)
它似乎与'transform-es2015-function-name'插件有关。 有关详细信息,请参见https://github.com/babel/babel/issues/4782#issuecomment-271367012。 因此,排除插件即可完成这项工作。
答案 2 :(得分:0)
请尝试使用 preset-env 依赖项。 对我来说很好
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env"]
}
},