kas*_*sia 5 webpack jestjs babeljs
我对 Babel 7、Webpack 4 和 Jest 的配置有问题。当我运行测试时,我收到以下错误:
语法错误:不能在模块外使用导入语句
包.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",
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
entry: {
app: ["./src/index.js"]
},
output: {
path: path.resolve('../', 'static/js/'),
filename: "[name].js",
publicPath: '/static/js/',
chunkFilename: '[name].chunk.js'
},
Run Code Online (Sandbox Code Playgroud)
.babelrc - 我认为问题出在 module: false 但如果我不包含它,webpack 不会分块我的文件。
{
"presets": [
["@babel/preset-env", {"modules": false}, "jest" ]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import"
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"],
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我删除 module: false 测试正在运行时,是否有机会不将 module: false 包含到测试中?
来自Babel 选项文档:
注意:env[envKey] 选项将合并到根对象中指定的选项之上。
因此,您可以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"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11667 次 |
| 最近记录: |