上一章讲的是CommonChunkPlugin
,和这一章依旧没有丝毫关系,这一章讲的是DllPlugin
和DllReferencePlugin
。node
这个插件啊,用来预打包一些第三方库,由于他们不常常修改,而每次咱们引用他们以后都要将他们不断的打包一次又一次,不但浪费了调试编译的时间,还浪费了....时间。jquery
初始化项目webpack
$ midkir 0x007-dll $ cd 0x007-dll $ cnpm init -y $ cnpm install angular lodash jquery
此次须要两个配置文件,一个用于打包dll
,一个用于打包dll-user
,先配置用来打包dll
的webpack.dll.config.js
git
$ vim ./webpack.dll.config.js // ./webpack.dll.config.js const path = require('path'); const webpack = require('webpack'); module.exports = { entry: { vendor: ['angular', 'jquery','lodash']// 用这三个库打包成dll作测试 }, output: { path: path.join(__dirname, 'dist'), filename: '[name].dll.js', library: '[name]_library' }, plugins: [ new webpack.DllPlugin({ path: path.join(__dirname, 'dist', '[name]-manifest.json'), name: '[name]_library' // 须要与output.library相同 }) ] };
打包dll,将会在./dist
目录下生成vendor-minifest.json
、vendor.dll.js
github
$ webpack --config webpack.dll.config.js
配置dll-user
web
$ vim ./webpack.config.js # ./webpack.config.js const path = require('path'); const webpack = require('webpack'); module.exports = { entry: { 'dll-user': ['./src/index.js'] }, output: { path: path.join(__dirname, 'dist'), filename: '[name].bundle.js' }, plugins: [ new webpack.DllReferencePlugin({ context: __dirname, manifest: require('./dist/vendor-manifest.json') }) ] };
添加入口文件npm
$ vim ./src/index.js // ./src/index.js var angular = require('angular'); console.log(angular);
打包dll-user
json
$ webpack Hash: 1aa3feec9d1827de7d5a Version: webpack 3.8.1 Time: 70ms Asset Size Chunks Chunk Names dll-user.bundle.js 2.88 kB 0 [emitted] dll-user [0] multi ./src/index.js 28 bytes {0} [built] [2] ./src/index.js 56 bytes {0} [built] // 注意这行 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ [3] delegated ./node_modules/_angular@1.6.6@angular/index.js from dll-reference vendor_library 42 bytes {0} [built] + 1 hidden module
注意:这里咱们引用了angular
可是在打包完的index.js
中,并不存在angular
,由于咱们经过引用dll
来引入angular
,在打包的信息输出中,对于angular的处理也变成了delegated
,vim