vue 开发过程当中,保存一次就会编译一次,若是可以减小编译的时间,哪怕是一丁点,也能节省很多时间。开发过程当中我的编写的源文件才会频繁变更,而一些库文件通常咱们不会去改动。若是能把库文件提取出来,就能达到减小打包体积,加快编译速度。本文主要讲述在 vue-cli3 中利用 DllPlugin 来进行预编译。html
一、安装相关插件vue
yarn add webpack-cli@^3.2.3 add-asset-html-webpack-plugin@^3.1.3 clean-webpack-plugin@^1.0.1 --dev
复制代码
二、编写配置文件 webpack.dll.conf.jswebpack
在项目根目录下新建 webpack.dll.conf.js,输入如下内容。ios
const path = require('path')
const webpack = require('webpack')
const CleanWebpackPlugin = require('clean-webpack-plugin')
// "clean-webpack-plugin": "^3.0.0"
// const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// dll文件存放的目录
const dllPath = 'public/vendor'
module.exports = {
entry: {
// 须要提取的库文件
vendor: ['vue', 'vue-router', 'vuex', 'axios', 'element-ui']
},
output: {
path: path.join(__dirname, dllPath),
filename: '[name].dll.js',
// vendor.dll.js中暴露出的全局变量名
// 保持与 webpack.DllPlugin 中名称一致
library: '[name]_[hash]'
},
plugins: [
// 清除以前的dll文件
new CleanWebpackPlugin(['*.*'], {
root: path.join(__dirname, dllPath)
}),
// "clean-webpack-plugin": "^3.0.0"
// new CleanWebpackPlugin(),
// 设置环境变量
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: 'production'
}
}),
// manifest.json 描述动态连接库包含了哪些内容
new webpack.DllPlugin({
path: path.join(__dirname, dllPath, '[name]-manifest.json'),
// 保持与 output.library 中名称一致
name: '[name]_[hash]',
context: process.cwd()
})
]
}
复制代码
三、生成 dllweb
在 package.json 中加入以下命令vue-router
"scripts": {
...
"dll": "webpack -p --progress --config ./webpack.dll.conf.js"
},
复制代码
控制台运行vuex
yarn run dll
复制代码
四、忽略已编译文件vue-cli
为了节约编译的时间,这时间咱们须要告诉 webpack 公共库文件已经编译好了,减小 webpack 对公共库的编译时间。在项目根目录下找到 vue.config.js ( 没有则新建 ),配置以下:element-ui
const webpack = require('webpack')
module.exports = {
...
configureWebpack: {
plugins: [
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/vendor-manifest.json')
})
]
}
}
复制代码
五、index.html 中加载生成的 dll 文件json
通过上面的配置,公共库提取出来了,编译速度快了,但若是不引用生成的 dll 文件,网页是不能正常工做的。
打开 public/index.html,插入 script 标签。
...
<script src="./vendor/vendor.dll.js"></script>
复制代码
到此公用库提取完成,但总以为最后一部手工插入 script 有点不太优雅,下面介绍下如何自动引入生成的 dll 文件。
打开 vue.config.js 在 configureWebpack.plugins 节点下,配置 add-asset-html-webpack-plugin
const path = require('path')
const webpack = require('webpack')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
module.exports = {
...
configureWebpack: {
plugins: [
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/vendor-manifest.json')
}),
// 将 dll 注入到 生成的 html 模板中
new AddAssetHtmlPlugin({
// dll文件位置
filepath: path.resolve(__dirname, './public/vendor/*.js'),
// dll 引用路径
publicPath: './vendor',
// dll最终输出的目录
outputPath: './vendor'
})
]
}
}
复制代码