当前位置: 移动技术网 > IT编程>开发语言>JavaScript > webpack代码分离CommonsChunkPlugin插件的使用(防止重复)

webpack代码分离CommonsChunkPlugin插件的使用(防止重复)

2019年11月26日  | 移动技术网IT编程  | 我要评论

 1.webpack.config.js中添加:


const path = require('path'); + const webpack = require('webpack'); const htmlwebpackplugin = require('html-webpack-plugin'); module.exports = { entry: { index: './src/index.js', another: './src/another-module.js' }, plugins: [ new htmlwebpackplugin({ title: 'code splitting' - }) + }), + new webpack.optimize.commonschunkplugin({ + name: 'common' // 指定公共 bundle 的名称。 + }) ], output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist') } };

2.然后就遇到了一个问题,还给出了一个解决方案,需要去查看文档中的插件章节

 

 

//optimization与plugins同级
optimization: {
    splitchunks: {
        cachegroups: {
            commons: {
                name: "commons",
                chunks: "initial",
                minchunks: 2
            }
        }
    }
},

 

3.运行npm run build,如果有公共部分可得到common.bundle.js文件,如果没有则不会生成这个文件

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网