search
HomeWeb Front-endJS TutorialHow to implement webpack2 project packaging optimization in vue-cli

Now I will share with you an article about vue-cli webpack2 project packaging optimization, which has a good reference value and I hope it will be helpful to everyone.

Reduce the file search scope

Configure resolve.modules

Webpack’s resolve.modules Configure the location of the module library (i.e. node_modules). When import 'vue' appears in js which is not a relative or absolute path, it will be found in the node_modules directory. However, the default configuration will be found through upward recursive search, but usually there is only one node_modules in the project directory, and it is in the project root directory. In order to reduce the search scope, you can directly specify the full path of node_modules; similarly, for aliases ( The same applies to the configuration of `alias):

function resolve (dir) {
 return path.join(__dirname, '..', dir)
}
module.exports = {
 resolve: {
 extensions: ['.js', '.vue', '.json'],
 modules: [
  resolve('src'),
  resolve('node_modules')
 ],
 alias: {
  'vue$': 'vue/dist/vue.common.js',
  'src': resolve('src'),
  'assets': resolve('src/assets'),
  'components': resolve('src/components'),
  // ...
  'store': resolve('src/store')
 }
 },
 ...
}

Reasonably set test & include & exclude

test: Conditions that must be met (regular expression, do not add quotes , match the files to be processed)

exclude: conditions that cannot be met (exclude directories that are not processed)

include: the path or file array that the imported file will be converted by the loader (to be processed directory included)

This can reduce unnecessary traversal, thereby reducing performance loss.

Replace the code compression tool

The UglifyJS plug-in provided by Webpack by default is slow due to the use of single-thread compression;

webpack-parallel-uglify- The plugin plugin can run the UglifyJS plugin in parallel, making more full and reasonable use of CPU resources, which can greatly reduce the build time;

Of course, this plugin should be used in the production environment rather than the development environment. After installing the plugin, proceed as follows Configuration:

// 删掉webpack提供的UglifyJS插件
// new webpack.optimize.UglifyJsPlugin({
// compress: {
//  warnings: false,
//  drop_console: true
// },
// sourceMap: true
// }),
// 增加 webpack-parallel-uglify-plugin来替换
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
new ParallelUglifyPlugin({
 cacheDir: '.cache/', // 设置缓存路径,不改动的调用缓存,第二次及后面build时提速
 uglifyJS:{
 output: {
  comments: false
 },
 compress: {
  warnings: false
 }
 }
})

I also tried the same type of plug-in webpack-uglify-parallel, but it was not as effective as webpack-parallel-uglify-plugin (it may vary from project to project, you can use it in your project for comparison).

webpack-parallel-uglify-plugin The package produced by the plug-in is slightly larger than that of UglifyJsPlugin (but not obvious); compared with the increased size, I chose to pursue speed (after using it, it dropped from 40 seconds to 40 seconds). to 19 seconds).

Copy static files

Use the copy-webpack-plugin plug-in: copy the files in the specified folder to the specified directory; its configuration is as follows:

var CopyWebpackPlugin = require('copy-webpack-plugin')
plugins: [
 ...
 // copy custom static assets
 new CopyWebpackPlugin([
 {
  from: path.resolve(__dirname, '../static'),
  to: config.build.assetsSubDirectory,
  ignore: ['.*']
 }
 ])
]
DllPlugin & DllReferencePlugin

The concept of Dll should be borrowed from the dll of Windows system. A dll package is a purely dependent library. It cannot run on its own and is used to reference it in your app.

When packaging a dll, Webpack will make an index of all included libraries and write them in a manifest file, and the code that references the dll (dll user) only needs to read this manifest file when packaging. , that’s it.

1. Add the file webpack.dll.conf.js under the project build folder with the following content

var path = require('path')
var webpack = require('webpack')
module.exports = {
 entry: {
 vendor: [ // 这里填写需要的依赖库
  'babel-polyfill',
  'axios',
  'vue/dist/vue.common.js',
  'vue-router',
  'pingpp-js',
  "region-picker"
 ]
 },
 output: {
 path: path.resolve(__dirname, '../static/js'),
 filename: '[name].dll.js',
 library: '[name]_library'
 },
 module: {
 rules: [
  {
  test: /\.vue$/,
  loader: 'vue-loader'
  },
  {
  test: /\.js$/,
  loader: 'babel-loader',
  exclude: /node_modules/
  }
 ]
 },
 plugins: [
 new webpack.optimize.ModuleConcatenationPlugin(),
 new webpack.DllPlugin({
  path: path.join(__dirname, '.', '[name]-manifest.json'),
  libraryTarget: 'commonjs2',
  name: '[name]_library'
 }),
 new webpack.optimize.UglifyJsPlugin({
  compress: {
  warnings: false
  }
 })
 ]
}

2. Add the plugin part in the webpack.prod.conf.js file:

plugins: [
 ...
 // copy custom static assets
  new webpack.DllReferencePlugin({
    context: path.resolve(__dirname, '..'),
    manifest: require('./vendor-manifest.json')
  })
]

3. Add the following in the index.html file in the project root directory:

<body>
  <p id="app"></p>
  <!-- built files will be auto injected -->
  <script src="<%= webpackConfig.output.publicPath %>spa/js/vendor.dll.js"></script>   //添加这句,路径可根据所需修改
</body>

4. Add the command to package the dll in package.json

"build:dll": "webpack --config build/webpack.dll.conf.js"

5 , Command sequence

npm run build:dll //打包一次之后依赖库无变动不需要执行
npm run build

Advantages

Dll exists independently after being packaged, as long as the library it contains does not increase The hash will not change even if it is reduced or upgraded, so the online dll code does not need to be updated frequently with version releases.

After the App part code is modified, you only need to compile the app part code and the dll part. As long as the included libraries are not increased, decreased, or upgraded, there is no need to repackage. This also greatly improves the speed of each compilation.

Suppose you have multiple projects that use the same dependent libraries, they can share a dll.

19s->15s

Set babel's cacheDirectory to true

Modify babel-loader in webpack.base.conf.js:

loader: &#39;babel-loader?cacheDirectory=true&#39;,

15s->14s

Set noParse

If you are sure that there are no other new dependencies in a module, you can configure this, Webpack will no longer scan the dependencies in this file. This will improve performance for relatively large class libraries. For details, please see the following configuration:

module: {
 noParse: /node_modules\/(element-ui\.js)/,
 rules: [
  {
   ...
  }
}
happypack

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. helpful.

Related articles:

User permission control in Vue2.0

WeChat payment through vue.js

Detailed explanation of how to implement vuex (detailed tutorial)

The above is the detailed content of How to implement webpack2 project packaging optimization in vue-cli. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
分享PyCharm项目打包的简易方法分享PyCharm项目打包的简易方法Dec 30, 2023 am 09:34 AM

简单易懂的PyCharm项目打包方法分享随着Python的流行,越来越多的开发者使用PyCharm作为Python开发的主要工具。PyCharm是功能强大的集成开发环境,它提供了许多方便的功能来帮助我们提高开发效率。其中一个重要的功能就是项目的打包。本文将介绍如何在PyCharm中简单易懂地打包项目,并提供具体的代码示例。为什么要打包项目?在Python开发

如何使用Python正则表达式进行代码打包和分发如何使用Python正则表达式进行代码打包和分发Jun 23, 2023 am 09:31 AM

随着Python编程语言的日益流行,越来越多的开发者开始使用Python编写代码。但是在实际使用中,我们常常需要将这些代码打包并分发给其他人使用。本文将介绍如何使用Python正则表达式进行代码打包和分发。一、Python代码打包在Python中,我们可以使用setuptools和distutils等工具来打包我们的代码。这些工具可以将Python文件、模块

怎么使用pkg将Node.js项目打包为可执行文件?怎么使用pkg将Node.js项目打包为可执行文件?Jul 26, 2022 pm 07:33 PM

如何用pkg打包nodejs可执行文件?下面本篇文章给大家介绍一下使用pkg将Node.js项目打包为可执行文件的方法,希望对大家有所帮助!

linux打包是什么意思linux打包是什么意思Feb 23, 2023 pm 06:30 PM

在linux中,打包指的是一个文件或目录的集合,而这个集合被存储在一个文件中;简单来说,打包是指将一大堆文件或目录变成一个总的文件。打包文件没有经过压缩,因此它占用的空间是其中所有文件和目录的总和。

Python 应用的终极进化:PyInstaller 的破茧成蝶Python 应用的终极进化:PyInstaller 的破茧成蝶Feb 19, 2024 pm 03:27 PM

PyInstaller是一个革命性的工具,它赋予python应用程序以超越其原始脚本形态的能力。通过将Python代码编译成独立的可执行文件,PyInstaller解锁了代码分发、部署和维护的新境界。从单一脚本到强大应用程序以往,Python脚本只存在于特定的Python环境中。分发这样的脚本需要用户安装Python和必要的库,这是一个费时且繁琐的过程。PyInstaller引入了打包的概念,将Python代码与所有必需的依赖项组合成一个单独的可执行文件。代码打包的艺术PyInstaller的工

Python 应用的独立宣言:PyInstaller 的自由之路Python 应用的独立宣言:PyInstaller 的自由之路Feb 20, 2024 am 09:27 AM

PyInstaller:Python应用的独立化PyInstaller是一款开源的python打包工具,它将Python应用程序及其依赖项打包为一个独立的可执行文件。这一过程消除了对Python解释器的依赖,同时允许应用程序在各种平台上运行,包括windows、MacOS和linux。打包过程PyInstaller的打包过程相对简单,涉及以下步骤:pipinstallpyinstallerpyinstaller--onefile--windowedmain.py--onefile选项创建一个单一

PyCharm教程:如何将Python代码打包成EXE文件PyCharm教程:如何将Python代码打包成EXE文件Feb 21, 2024 pm 12:12 PM

在本文中,我们将介绍PyCharm中的一种常用方法,通过使用PyInstaller将Python代码打包成可执行的EXE文件。PyInstaller是一个用于将Python应用程序转换为独立的可执行文件的工具,它可以将Python代码打包成EXE、APP、Linux等格式,方便用户在没有安装Python解释器的环境中运行Python程序。步骤一:安装PyIn

Python 代码变身独立应用:PyInstaller 的炼金术Python 代码变身独立应用:PyInstaller 的炼金术Feb 19, 2024 pm 01:27 PM

PyInstaller是一个开源库,允许开发者将python代码编译为平台无关的自包含可执行文件(.exe或.app)。它通过将Python代码、依赖项和支持文件打包在一起来实现这一目标,从而创建独立应用程序,无需安装Python解释器即可运行。PyInstaller的优势在于它消除了对Python环境的依赖性,使应用程序可以轻松分发和部署给最终用户。它还提供了构建器模式,使用户可以自定义应用程序的设置、图标、资源文件和环境变量。使用PyInstaller打包Python代码安装PyInstal

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),