webapck 启动报错 SyntaxError: Support for the experimental syntax 'optionalChainingAssign' isn't currently enabled (103:13):
已经引入了@babel/plugin-proposal-optional-chaining
还是报错,这种怎么修改?
报错代码 common.js 在 主入口文件中引入了
function scrollToCompatibleIE(element, top, left) {
if (element && top && left) {
if (typeof element.scrollTo !== 'function') {
element?.scrollTop = top; // 这里报错
element?.scrollLeft = left; // 这里报错
} else {
element?.scrollTo({
top: top,
left: left,
});
}
}
}
.babelrc 配置
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": [
"ie >=9",
"chrome >= 48",
"android >= 4.4",
"ios >= 9",
"safari >= 7"
]
},
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-transform-runtime"
]
}
webapck.config.js 配置
rules: [
{
test: /\.m?js$/, // 匹配 .js 或 .mjs 文件
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
configFile: path.resolve(__dirname, '.babelrc') // 指定Babel配置文件
}
},
},
package.json 配置
{
"scripts": {
"start:test": "npx cross-env envMode=test webpack serve --open --mode development"
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/plugin-transform-runtime": "^7.24.3",
"@babel/preset-env": "^7.24.4",
"@vue/compiler-sfc": "^3.4.25",
"autoprefixer": "^10.4.19",
"babel-loader": "^9.1.3",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^7.1.1",
"dotenv": "^16.4.5",
"html-webpack-plugin": "^5.6.0",
"image-minimizer-webpack-plugin": "^4.0.0",
"imagemin": "^8.0.1",
"mini-css-extract-plugin": "^2.9.0",
"postcss": "^8.4.38",
"postcss-loader": "^8.1.1",
"postcss-px-to-viewport": "^1.1.1",
"sass": "^1.75.0",
"sass-loader": "^14.2.1",
"style-loader": "^4.0.0",
"vue-loader": "^17.3.1",
"webpack": "^5.91.0",
"webpack-bundle-analyzer": "^4.10.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"dependencies": {
"axios": "^1.4.0",
"core-js": "^3.37.0",
"default-passive-events": "^2.0.0",
"jquery": "^3.7.1",
"regenerator-runtime": "^0.14.1",
"vconsole": "^3.15.1",
"vue": "^3.4.25",
"weixin-js-sdk": "^1.6.5"
}
}
Babel 插件
@babel/plugin-proposal-optional-chaining
是用来支持可选链(Optional Chaining)
语法的,而不是可选链赋值(Optional Chaining Assignment)