gpt4 book ai didi

webpack - "SyntaxError: Cannot use import statement outside a module"与 Babel、Jest 和 webpack

转载 作者:行者123 更新时间:2023-12-03 16:36:52 25 4
gpt4 key购买 nike

我对 Babel 7、Webpack 4 和 Jest 的配置有疑问。
当我运行测试时,我收到以下错误:

语法错误:无法在模块外使用 import 语句

包.json

 "@babel/core": "^7.7.5",
"@babel/highlight": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^7.1.5",
"@babel/plugin-transform-runtime": "^7.7.4",
"@babel/preset-react": "^7.7.4",
"@babel/runtime": "^7.8.4",
"babel-jest": "^24.9.0",
"jest-watch-typeahead": "^0.4.2",
"vue-jest": "^3.0.5",
"jest": "^24.9.0",
"jest-serializer-vue": "^2.0.2",
"jest-transform-stub": "^2.0.0",

webpack.config.js
  entry: {
app: ["./src/index.js"]
},
output: {
path: path.resolve('../', 'static/js/'),
filename: "[name].js",
publicPath: '/static/js/',
chunkFilename: '[name].chunk.js'
},

.babelrc - 我认为问题出在 module: false 但如果我不包含它,webpack 不会分 block 我的文件。

{
"presets": [
["@babel/preset-env", {"modules": false}, "jest" ]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import"
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"],
}
}
}

当我删除 module: false 测试正在运行时,是否有机会不将 module: false 包含到测试中?

最佳答案

来自 Babel Options documentation :

Note: env[envKey] options will be merged on top of the options specified in the root object.



所以你可以申请 modules: "auto"在测试期间通过重新声明 env.test 中的插件目的:
{
"presets": [
[
"@babel/preset-env",
{
"modules": false
},
"jest"
]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import"
],
"env": {
"test": {
"presets": [
[
"@babel/preset-env",
{
"modules": "auto"
},
"jest"
]
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
}
}

关于webpack - "SyntaxError: Cannot use import statement outside a module"与 Babel、Jest 和 webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60337983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com