如何从Webpack中的splitChunks中分离出webpack-dev-server?

时间:2018-09-24 22:15:54

标签: javascript webpack webpack-dev-server html-webpack-plugin

我正在尝试使用webpackDevServer获得多个入口点。

一个入口点需要我的整个node_modules文件夹。另一个只需要一个文件,其中就包含一个console.log(入口点文件)。

由于某种原因,带有单个console.log的单个文件无法运行。 See this question as well.

我正在WebpackDevServer中测试此设置,所以我怀疑所有文件至少都需要WebpackDevServer才能起作用。因此,我将optimization.splitChunks更改为based off the example on the webpack docs的样子:

optimization: {
    splitChunks: {
        cacheGroups: {
            commons: {
                test: /[\\/]node_modules[\\/]/,
                name: 'vendor',
                chunks: 'all'
            },
            vendor: {
                test: /[\\/]node_modules[\\/](webpack|webpack-dev-server)[\\/]/,
                name: 'webpack',
                chunks: 'all',
            }
        }
    },
},

我希望会有一个“供应商”捆绑包和一个“ webpack”捆绑包。只有“供应商”(和我的入口点):

                                         app.js   6.92 MiB            app  [emitted]  app
                               resetPassword.js   35.2 KiB  resetPassword  [emitted]  resetPassword
                                      vendor.js   14.4 MiB         vendor  [emitted]  vendor

如何将webpack-dev-server放入其自己的捆绑包,然后可以将其包含到HtmlWebpackPlugin中,以测试是否需要运行console.log(或其他node_modules){ {1}}?

Webpack配置

module.exports = {
    entry: {
        app: './public/js/ide.js',
        resetPassword: './public/js/reset_password.js'
    },
    output: {
        path: path.resolve(__dirname, '../build'),
        filename: '[name].js',
        publicPath: '/'
    },
    ...
optimization: {
    splitChunks: {
        cacheGroups: {
            commons: {
                test: /[\\/]node_modules[\\/]/,
                name: 'vendor',
                chunks: 'all'
            },
            vendor: {
                test: /[\\/]node_modules[\\/](webpack|webpack-dev-server)[\\/]/,
                name: 'webpack',
                chunks: 'all',
            }
        }
    },
},
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'public/html/ide.html',
            inject: true,
            chunks: ['app', 'vendor']
        }),
        new HtmlWebpackPlugin({
            filename: 'reset_password.html',
            template: 'public/html/reset_password.html',
            inject: true,
            chunks: ['resetPassword'] // this does not work
            //chunks: ['resetPassword', 'vendor'] //this works
        }),
    ],
}

reset_password.js

console.log('hello') 

webpack开发服务器配置

devServer: {
    clientLogLevel: 'warning',
    historyApiFallback: true,
    hot: true,
    compress: true,
    host: HOST,
    port: PORT,
    open: config.dev.autoOpenBrowser,
    overlay: false,
    publicPath: '/',
    contentBase: [
        path.join(__dirname, "../../public"), 
        path.join(__dirname, "../../public/js")],
    watchOptions: {
        poll: config.dev.poll,
    },
    disableHostCheck: true,
    https: true,
    noInfo: false,
},

1 个答案:

答案 0 :(得分:0)

向每个块添加一个优先级属性。来自docs

  

splitChunks.cacheGroups.priority

     

号码

     

一个模块可以属于多个缓存组。优化将优先考虑具有更高优先级的缓存组。默认组的优先级为负,以允许自定义组获得更高的优先级(自定义组的默认值为0)。

所以您的代码将是这样的。请注意,优先级是最高的数字值,而不是排名值。

optimization: {
  splitChunks: {
    cacheGroups: {
      commons: {
        test: /[\\/]node_modules[\\/]/,
        name: 'vendor',
        chunks: 'all',
        priority: 1
      },
      vendor: {
        test: /[\\/]node_modules[\\/](webpack|webpack-dev-server)[\\/]/,
        name: 'webpack',
        chunks: 'all',
        priority: 2
      }
    }
  },
},