【发布时间】:2018-11-05 13:02:06
【问题描述】:
我的 babel+webpack 配置工作正常,但生成的包在 IE11 中无法运行,因为它包含 const 声明。我认为拥有es2015 预设足以解决这个问题?运行 $(npm bin)/babel test/some-es2015.js 会产生严格的 ES5.1 代码,因此 Babel 似乎可以工作,但在 IE11 中运行的实际代码是在从 node_modules 导入的模块中。
当在我的结果包中 grepping 'const ' 时,我会得到这样的某些行(eval 是由于 eval 源映射顺便说一句):
eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst validator = __webpack_require__(/*! validator */ \"./node_modules/tcomb-additional-types/node_modules/validator/index.js\");\nconst t = __webpack_require__(/*! tcomb */ \"./node_modules/tcomb/index.js\");\nconst IP = t.refinement(t.String, validator.isIP);\nexports.IP = IP;\nexports.default = IP;\n//# sourceMappingURL=ip.js.map\n\n//# sourceURL=webpack:///./node_modules/tcomb-additional-types/lib/string/ip.js?");
需要注意的重要部分是const validator = 之类的东西。这不是 ES5.1 语法。我自己的代码似乎已经被转译为 ES5 就好了。我可以在/node_modules/tcomb-additional-types/lib/string/ip.js 中看到这个文件,他们使用const,所以这不是Babel 添加consts,而是包含它们的源。大多数其他软件包都是 ES5。
到目前为止,我发现大多数consts 来自material-ui 和tcomb-additional-types。
Babel .babelrc:
{
"compact": false,
"presets": [
"es2015",
"es2017"
],
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}],
"transform-class-properties",
"transform-react-jsx",
"transform-object-rest-spread"
]
}
Webpack 配置:
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
/** @returns {String} an absolute path */
function toRoot(rootRelativeDir) {
return path.resolve(__dirname, '..', rootRelativeDir);
}
module.exports = {
entry: ['./src/app.js', './styles/flex.less'].map(toRoot),
output: {
filename: 'bundle.js',
path: toRoot('.webpack/dist')
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {}
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
/* General options are read using .babelrc - only webpack loader specific here */
cacheDirectory: toRoot('.webpack/babel_cache')
}
}
]
}
]
},
plugins: [new CopyWebpackPlugin([toRoot('public')])]
};
【问题讨论】:
标签: javascript webpack babeljs