我从堆栈溢出中添加了 Babel 或其他人给出的许多设置,但 ES6 的新功能(例如箭头函数和默认参数)仍然在我的bundle.js中
bundle.js 的内容如下:
var nn = function(e={}) {
const {baseClasses: t, newClasses: n, Component: r} = e;
if (!n)
return t;
const a = At()({}, t);
return Object.keys(n).forEach(e=>{
n[e] && (a[e] = `${t[e]} ${n[e]}`)
}
),
a
};
因此,当我在 IE11 中打开页面时,发生错误 missing ')'
,该错误指向 function(e={})
。
这是我的 webpack.config.js:
const entries = require("./config/pages").reduce(function(map, page) {
map[page] = `./src/${page}/index.js`;
return map;
}, {});
module.exports = {
mode: 'development',
entry: ["@babel/polyfill",...entries],
output: {
path: path.resolve(__dirname,'dist'),
publicPath: '/',
filename: 'myapp/static/js/[name]/bundle.js'
},
devtool: 'source-map',
module: require("./config/loaders"),
devServer:{
open: true,
publicPath: '/',
historyApiFallback: true,
disableHostCheck: true
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
}),
new webpack.ProvidePlugin({
fetch: "isomorphic-fetch",
})
]
};
和模块的./config/loaders
module.exports = {
rules: [
{
test: /\.(js|jsx)$/,
// exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.(css|less)$/,
use: ["style-loader", "css-loader", "less-loader"]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 500, //small than 500 use url, otherwise use base64
outputPath:'inquiry/static/img/'
}
}
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
outputPath: 'myapp/static/fonts/'
}
}]
}
]
};
.babelrc:
{
"presets": [
// ["@babel/env",
// {
// "targets": {
// "browsers": "ie 11"
// },
// "useBuiltIns": "usage",
// "corejs": "3.0.1",
// }
// ],
["@babel/preset-env",
{
"useBuiltIns": "entry",
"corejs": "3.0.1",
}],
"@babel/react"
],
"plugins": [
["@babel/transform-runtime"],
["@babel/plugin-transform-parameters"],
["@babel/plugin-transform-arrow-functions"],
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
],
]
}
另外,我已经在我的index.js中导入了“@babel/polyfill”
PS:我注意到包含 ES6 功能的代码来自 node_modules 中的 Meterial UI lib,因此我删除了 babel loader 规则中的 exclude:/node_modules/,
但它仍然不起作用。
最佳答案
我使用的是 webpack 5,并且在 webpack.config.js
中将目标设置为 es5 解决了我的问题。
module.exports = {
target: 'es5',
...
}
关于reactjs - 使用 babel polyfill 后,bundle.js 仍然包含箭头函数和默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823678/