如何使用 "ModuleConcatenation bailout: Module is not an ECMAScript module" 救助消息修复模块?

How to fix modules with "ModuleConcatenation bailout: Module is not an ECMAScript module" bailout message?

Webpack3 附带 ModuleConcatenation 插件,当与 --display-optimization-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 bailout: Module is not an ECMAScript module。以下是其内容:

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(什么节点使用,require)。 Webpack 接收 CommonJS 模块,但 ModuleConcatenationPlugin 依赖 ES 模块。您可以将 Babel 配置为不使用 modules option.

转换模块
{
  "presets": [
    ["es2015", { "modules": false }]
  ]
}

Webpack 2+ 开箱即用地支持 ES 模块,最好将它们留给 webpack,因为它启用了 Tree Shaking.

等功能

对于那些使用现代@babel/preset-env的人:

{
  "presets": [
    ["@babel/preset-env",{
      "targets": {
        ...
      },
      "modules": false
    }],
    "@babel/preset-react"
  ],
  "plugins": [...
}

但是不好的事情(但不重要)在那之后我不能像以前一样在我的 webpack 配置文件中使用 ES 模块,所以在 webpack.config.babel.js:

import webpack from 'webpack';

应改为:

const webpack = require('webpack');

我最近遇到了与 Ionic 和 Angular 非常相似的问题。我将自定义组件导入 app.module.ts,然后将其插入 entryComponents,后来我决定走另一条路。我删除了导入并忘记了 entryComponents 条目。我也有同样的错误。

ModuleConcatenation bailout: Module is not an ECMAScript module

然后它指向我的variable.scss路径,然后是下面一行

ERROR in Cannot read property 'members' of undefined

所以对于任何有类似问题的人(最有可能使用 Ionic 和/或 Angular),您可能必须确保您没有尝试在实际未导入的页面中使用组件,但是在 page.module.ts entryComponents 中仍然有一个尾随点。