简体   繁体   English

CommonsChunkPlugin中的webpack错误:在正常模式下运行时,不允许使用非条目块

[英]webpack ERROR in CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk

So When I try to split my application into 1 application.js file and 1 libraries.js file, everything works fine. 所以当我尝试将我的应用程序拆分为1个application.js文件和1个libraries.js文件时,一切正常。 When I try to split it up into 1 application.js file and 2 libraries.js files, I get this error when building: 当我尝试将其拆分为1个application.js文件和2个libraries.js文件时,构建时出现此错误:

ERROR in CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (libraries-react)

Anyone know what might be causing this error? 有谁知道可能导致此错误的原因?

My configuration for webpack is 我对webpack的配置是

var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");

var extractSass = new ExtractTextPlugin('main.css');

module.exports = {
    module: {
        loaders: [{
            test: /\.jsx$/,
            loader: 'babel',
            exclude: ['./node_modules'],
            query: {
                presets: ['react', 'es2015']
            }
        }, {
            test: /\.scss$/,
            loader: extractSass.extract(['css', 'sass'])
        }, {
            test: /\.html$/,
            loader: 'file?name=[name].[ext]'
        }, {
            test: /\/misc\/.*\.js$/,
            loader: 'file?name=/misc/[name].[ext]'
        }, {
            test: /\.(png|jpg|jpeg|)$/,
            loader: 'file?name=/images/[name].[ext]'
        }]
    },
    plugins: [
        extractSass,
        new webpack.optimize.CommonsChunkPlugin('libraries-core', 'libraries-core.js'),
        new webpack.optimize.CommonsChunkPlugin('libraries-react', 'libraries-react.js')
    ],
    entry: {
        //3rd party libraries
        'libraries-core': [
          'lodash',
          'superagent',
          'bluebird',
          'eventemitter3',
          'object-assign',
          'schema-inspector',
          'jsuri',
          'store-cacheable',
          'immutable'
        ],

        'libraries-react': [
          'react',
          'react-dom',
          'react-router',
          'nucleus-react'
        ],

        //application code
        application: './web/app/application.jsx',

        //mocks
        'mocked-api': './web/app/mock/api.js',
        'mocked-local-storage': './web/app/mock/local-storage.js'
    },
    output: {
        path: './web/build',
        publicPath: '/build',
        filename: '[name].js'
    }
}

Following the github issue#1016 , you need to reverse the order of the chunk names in plugin definition regarding to the entry points' definition github问题#1016之后 ,你需要颠倒插件定义中关于入口点定义的块名称的顺序

It seems like a bug to this webpack plugin for the time being... 这个webpack插件暂时似乎是个bug ...

new webpack.optimize.CommonsChunkPlugin('libraries-react', 'libraries-react.js')
new webpack.optimize.CommonsChunkPlugin('libraries-core', 'libraries-core.js')

or 要么

new webpack.optimize.CommonsChunkPlugin({names: ['libraries-react', 'libraries-core'], filename: '[name].js')

Switching from two entries for the CommonChunkPlugin to a single one helped in my case. 在我的案例中,从CommonChunkPlugin的两个条目切换到单个条目。

Before: 之前:

plugins: [
    new webpack.optimize.CommonsChunkPlugin('libraries-core', 'libraries-core.js'),
    new webpack.optimize.CommonsChunkPlugin('libraries-react', 'libraries-react.js')
]

After: 后:

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['libraries-core', 'libraries-react'],
      minChunks: Infinity
   })
]

It's based on the two-explicit-vendor-chunks example. 它基于两个显式供应商块的例子。

The other answers talk about the order of the names in CommonsChunkPlugin which is true. 其他答案讨论了CommonsChunkPlugin中名称的顺序,这是正确的。 But sometimes even after you correct the order (ie reverse of the order given in entry), webpack still might throw the same error. 但有时即使你纠正了订单(即在条目中给出的顺序的反转),webpack仍然可能会抛出相同的错误。

And this is when you're using another CommonsChunkPlugin instance to extract commons - eg. 这是当你使用另一个CommonsChunkPlugin实例来提取公共资源时 - 例如。 explicit-webpack-runtime-chunk, again the order matters - here the order of instances in the config's plugins list. explicit-webpack-runtime-chunk,顺序也很重要 - 这里是config的插件列表中的实例顺序。 Simply, move this instance after the CommonsChunkPlugin for explicit-vendor-chunks. 简单地说,在CommonsChunkPlugin之后将此实例移动到显式供应商块。 For example, 例如,

plugins: [
  // explicit-vendor-chunk
  new CommonsChunkPlugin({
    names: ["vendor-2", "vendor-1"],
    minChunks: Infinity
  }),

  // and the following should go after explicit-vendor-chunk
  // in the list of plugins

  // explicit-webpack-runtime-chunk
  new CommonsChunkPlugin({
    name: "manifest", // something that's not an entry
    minChunks: Infinity
  })
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM