We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
公司的项目开发到现在2019.12.9已经过去了四个月,项目我一个人开发一个人维护,项目迭代也越来越快,每次上线打包速度也越来越慢,是时候想办法优化一下打包速度了,免得每次都要等那么久。这次优化想到是把项目中安装的第三方模块提取出来,进行预编译,这样下次打包的时候就不用再去打包了。用到的插件DllPlugin、DllReferencePlugin
npm i clean-webpack-plugin webpack-cli add-asset-html-webpack-plugin --dev
const path = require('path') const webpack = require('webpack') const { CleanWebpackPlugin } = require('clean-webpack-plugin') // dll文件存放的目录 const dllPath = 'public/vendor' module.exports = { entry: { // 需要提取的库文件 // 这里像vue,vue-router 这些库我项目中打包的时候用的cdn插入方式,这里就没有放进来了。 vendor: [ 'echarts', 'path-to-regexp', 'register-service-worker', 'vue-class-component', 'vue-property-decorator', 'vuex-class', 'vuex-module-decorators' ] }, output: { path: path.join(__dirname, dllPath), filename: '[name].dll.js', // vendor.dll.js中暴露出的全局变量名 // 保持与 webpack.DllPlugin 中名称一致 library: '[name]_[hash]' }, plugins: [ // 清除之前的dll文件 new CleanWebpackPlugin(), // manifest.json 描述动态链接库包含了哪些内容 new webpack.DllPlugin({ path: path.join(__dirname, dllPath, '[name]-manifest.json'), // 保持与 output.library 中名称一致 name: '[name]_[hash]', context: process.cwd() }) ] }
"scripts": { ... "dll": "webpack -p --progress --config ./webpack.dll.conf.js" },
npm run dll
const webpack = require('webpack') module.exports = { ... configureWebpack: { plugins: [ new webpack.DllReferencePlugin({ context: process.cwd(), manifest: require('./public/vendor/vendor-manifest.json') }) ] } }
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' }) ] } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
公司的项目开发到现在2019.12.9已经过去了四个月,项目我一个人开发一个人维护,项目迭代也越来越快,每次上线打包速度也越来越慢,是时候想办法优化一下打包速度了,免得每次都要等那么久。这次优化想到是把项目中安装的第三方模块提取出来,进行预编译,这样下次打包的时候就不用再去打包了。用到的插件DllPlugin、DllReferencePlugin
在项目的根目录下创建 webpack.dll.conf.js,加入如下内容
在package.json配置命令
到目前为止我们只是把公共库打包提取出来了,还没有真正和我们的代码关联起来,我们需要告诉webpack哪些库已经编译好了,不需要再编译直接引用就可以了,
dll 文件已经生成,在vue.config.js 我们通过DllReferencePlugin忽略了已经打包过的库,所以我们需要在打包完后引入dll 文件,这里我们通过add-asset-html-webpack-plugin来做这件事情
参考资料
The text was updated successfully, but these errors were encountered: