【发布时间】:2019-10-20 03:53:38
【问题描述】:
我是 babel 的新手,正在尝试转换我的 es6 代码以使用 IE11。但是当我在 IE11 中运行代码时,我收到关于我的 forEach 代码的 js 错误。根据我的阅读,我需要添加预设@babel/preset-env。我将它添加到我的配置文件中,所以我不确定它为什么不转译那些 forEach 调用。
const path = require('path');
module.exports = {
entry: {
setupForm: "./Scripts/es6/setupForm.js",
prelimForm: "./Scripts/es6/prelimForm.js"
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, './Scripts/build'),
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/,
query: {
presets: ['@babel/preset-env']
}
}]
}
}
我想也许我需要另外将 babel polyfill.js 引用为 discussed here 所以我将它添加到我的页面,但是,我收到关于 对象不支持属性或方法的相同错误'forEach'.
这是我的 package.json 文件。
{
"name": "OurSite",
"version": "1.0.0",
"description": "",
"main": "map_embed.js",
"directories": {
"doc": "docs"
},
"scripts": {
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"webpack": "^4.32.2",
"webpack-cli": "^3.3.2"
},
"babel": {
"presets": [
"env"
]
},
"dependencies": {}
}
【问题讨论】:
-
您是否尝试将
entry: ["@babel/polyfill", "./Scripts/es6/setupForm.js"],添加到您的 webpack 入口配置中? -
没有。因为我的条目中有两个文件,我将如何将您拥有的内容转换为每个文件?
-
这取决于两个输出是否在同一页面上。如果它们在不同的页面上,则需要在两个页面上都添加 polyfill:
entry: {setupForm: ['@babel/polyfill','./Scripts/es6/setupForm.js']}等 -
不,他们不会在同一页面上。我尝试这样做 - setupForm: ["@babel-polyfill", "./Scripts/es6/setupForm.js"] - 第一个。但是在运行 npm run build 时,出现错误“Module not found: Error: Can't resolve 'babel-polyfill'” 是否需要将@符号替换为./node_modules?
-
不,你需要保留
@,你确定你已经安装了@babel/polyfill作为一个包吗?