vue-cli-plugin-dll 模块预编译原理
本质是将大量复用模块且不会频繁更新的库进行预编译,且只需要编译一次,编译完成后产出指定文件(可以称为动态链接库)。在之后的构建过程中不会再对这些模块进行编译,而是直接使用DllReferencePlugin来引用动态链接库的代码,因此可以提高构建速度。一般可以将第三方模块进行预编译,如 vue、vue-router、vuex等,只要这些依赖模块不更新,就不需要再重新编译。
1. 安装 vue-cli-plugin-dll 插件
npm i vue-cli-plugin-dll
2. 新建 vue.config.js 文件来进行相关的配置
const path = require('path')
module.exports = {
pluginOptions: {
dll: {
// 入口配置
entry: ['vue','vuex','vue-router','v-viewer','moment','mint-ui','lodash','element-ui','core-js'],
// 输出目录
output: path.join(__dirname, './public/dll'),
// 是否开启 DllReferencePlugin,
open: true,
// 在执行 `dev` , `build` 等其他指令时,程序会自动将 `dll` 指令生成的 `*.dll.js` 等文件自动注入到 index.html 中。
inject: true,
}
}
}
3. 生成dll文件
npx vue-cli-service dll
4. 启动、打包项目
5. 多入口配置
const path = require('path')
module.exports = {
pluginOptions: {
dll: {
// 入口配置
entry: {
vue: ["vue", "vue-router", "vuex", 'vue-kinesis'],
plugin: ["v-viewer",'moment','lodash','bignumber','core-js'],
ui: ["mint-ui",'element-ui'],
},
// 输出目录
output: {
path: path.join(__dirname, 'public/dll'),
filename: '[name].dll.js',
// vendor.dll.js中暴露出的全局变量名
// 保持与 webpack.DllPlugin 中名称一致
library: '[name]_[hash]'
},
// 是否开启 DllReferencePlugin,
open: true,
// 在执行 `dev` , `build` 等其他指令时,程序会自动将 `dll` 指令生成的 `*.dll.js` 等文件自动注入到 index.html 中。
inject: true,
}
}
}
注意事项:
- 每当生成过dll中存在依赖需进行升级或更新的时候,都要执行 生成dll文件( npx vue-cli-service dll ) 的命令来生成新的文件
- npx vue-cli-service dll 读取的是 production 环境配置
- vue-cli-plugin-dll 会与代码切割 splitChunks 有冲突【还在解决中】