gpt4 book ai didi

webpack - 如何修复带有 "ModuleConcatenation bailout: Module is not an ECMAScript module"救援消息的模块?

转载 作者:行者123 更新时间:2023-12-02 08:54:30 31 4
gpt4 key购买 nike

Webpack3 附带了 ModuleConcatenation 插件,当与 --display-optimization-bailout 标志一起使用时,将为您提供 bailout 的原因。

救援消息不太容易理解,而且很难理解为什么它们会发生在特定模块上。

这是我的项目的非常简化版本上的 webpack 命令的输出:

> webpack --bail --display-optimization-bailout

Hash: 4a9a55dc2883e82a017e
Version: webpack 3.4.1
Child client:
Hash: 4a9a55dc2883e82a017e
Time: 594ms
Asset Size Chunks Chunk Names
a.d3ade61d21d5cb8dd426.bundle.js 712 bytes 0 [emitted] a
a.d3ade61d21d5cb8dd426.bundle.js.map 6.57 kB 0 [emitted] a
manifest.json 102 bytes [emitted]
index.html 299 kB [emitted] [big]
[0] multi ./src/client/bootstrap/ErrorTrap.js 28 bytes {0} [built]
ModuleConcatenation bailout: Module is not an ECMAScript module
[1] ./src/client/bootstrap/ErrorTrap.js 199 bytes {0} [built]
ModuleConcatenation bailout: Module is not an ECMAScript module

我尽可能简化了 ./src/client/bootstrap/ErrorTrap.js 的内容,但我仍然得到 ModuleConcatenation 救助:模块不是 ECMAScript 模块。其内容如下:

class ErrorTrap {
}

export default ErrorTrap;

我研究了如何理解此救助消息,发生这种情况的原因之一是模块没有导入或导出,如 https://github.com/webpack/webpack/blob/93ac8e9c3699bf704068eaccaceec57b3dd45a14/lib/dependencies/HarmonyDetectionParserPlugin.js#L12-L14 所示。 ,但我不知道为什么它不将此模块视为 ECMAScript 模块。

.babelrc

{
"presets": [
"es2015"
]
}

webpack.config.js表示:

{ target: 'web',
devtool: 'source-map',
entry: { a: [ './src/client/bootstrap/ErrorTrap.js' ] },
output:
{ path: '/project/build/client/assets',
filename: '[name].[chunkhash].bundle.js',
chunkFilename: '[name].[chunkhash].chunk.js',
publicPath: '/assets/' },
module: { rules: [ [Object], [Object], [Object], [Object], [Object] ] },
resolve: { alias: { 'lodash.defaults': 'lodash' } },
plugins:
[ ModuleConcatenationPlugin { options: {} },
CommonsChunkPlugin {
chunkNames: [Object],
filenameTemplate: undefined,
minChunks: Infinity,
selectedChunks: undefined,
children: undefined,
async: undefined,
minSize: undefined,
ident: '/project/node_modules/webpack/lib/optimize/CommonsChunkPlugin.js0' },
ManifestPlugin { opts: [Object] },
ChunkManifestPlugin {
manifestFilename: 'chunk-manifest.json',
manifestVariable: 'webpackManifest',
inlineManifest: false },
OccurrenceOrderPlugin { preferEntry: undefined },
DefinePlugin { definitions: [Object] },
VisualizerPlugin { opts: [Object] },
ExtractTextPlugin { filename: '[name].[contenthash].css', id: 1, options: {} },
UglifyJsPlugin { options: [Object] },
LoaderOptionsPlugin { options: [Object] } ],
name: 'client' }

最佳答案

您使用 Babel 来转译 JavaScript 文件,默认情况下,es2015 预设会将 ES 模块(import/export)转换为 CommonJS (Node 使用什么,require)。 Webpack 接收 CommonJS 模块,但 ModuleConcatenationPlugin 依赖于 ES 模块。您可以将 Babel 配置为不使用 modules option 转换模块。 .

{
"presets": [
["es2015", { "modules": false }]
]
}

Webpack 2+ 支持开箱即用的 ES 模块,最好将它们留给 webpack,因为它支持诸如 Tree Shaking 之类的功能。 .

关于webpack - 如何修复带有 "ModuleConcatenation bailout: Module is not an ECMAScript module"救援消息的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384170/

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