How to optimize packaging for webpack 4.0
Jun 12, 2018 pm 01:36 PMThis time I will bring you how to package and optimize webpack4.0, and what are the precautions for how to package and optimize webpack4.0. The following is a practical case, let's take a look.
The version of the current dependent package
1. Optimize the loader configuration
##1.1 Narrow the file matching range (include/exclude)
Narrow the loader loading search range by excluding files under node_modules. High probability of hitting files1 2 3 4 5 6 7 8 9 10 |
|
1.2 Cache The execution result of the loader (cacheDirectory)
cacheDirectory is a specific option of the loader, and the default value is false. The specified directory (use: 'babel-loader?cacheDirectory=cacheLoader') will be used to cache the execution results of the loader, reducing the Babel recompilation process during webpack build. If set to an empty value (use: 'babel-loader?cacheDirectory') or true (use: 'babel-loader?cacheDirectory=true') the default cache directory (node_modules/.cache/babel-loader) will be used, if in If the node_modules directory is not found in any root directory, it will downgrade and fall back to the operating system's default temporary file directory.1 2 3 4 5 6 7 8 9 10 |
|
2.resolve optimization configuration
2.1 Optimization module search path resolve.modules
Webpack's resolve.modules configures the location of the module library (i.e. node_modules). When import 'vue' appears in js, which is neither a relative nor an 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 ( alias) configuration, the same should be true:1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 |
|
2.2 resolve.alias Configure path alias
Create a path alias for import or require to ensure that module introduction becomes easier. The configuration item maps the original import path to a new import path through aliases. This optimization method will affect the use of Tree-Shaking to remove invalid codeFor example, some common modules located in the src/ folder:
1 2 3 4 |
|
1 |
|
1 |
|
1 2 3 4 5 6 |
|
1 2 3 |
|
1 2 |
|
2.3resolve.extensions
When introduced When the module does not have a file suffix, webpack will automatically parse the determined file suffix according to this configuration- The suffix list should be as small as possible
- The most frequent Preface the
- export statement with the suffix
1 2 3 |
|
3.module.noParse
Modules that use noParse will not be parsed by loaders, so if the library we use is too large and does not contain import require and define calls, we can use this configuration to improve performance. , causing Webpack to ignore recursive parsing of some files that are not modularized.1 2 3 4 5 6 7 8 |
|
4.HappyPack
HappyPack allows webpack to expand the execution process of the loader from a single process form to a multi-process mode, that is Decompose the task to multiple sub-processes for concurrent execution. After the sub-processes complete the processing, the results are sent to the main process. This speeds up code construction and is better used in combination with DLL dynamic link libraries.1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 |
|
5.DLL dynamic link library
在一个动态链接库中可以包含其他模块调用的函数和数据,动态链接库只需被编译一次,在之后的构建过程中被动态链接库包含的模块将不会被重新编译,而是直接使用动态链接库中的代码。
将web应用依赖的基础模块抽离出来,打包到单独的动态链接库中。一个链接库可以包含多个模块。
当需要导入的模块存在于动态链接库,模块不会再次打包,而是去动态链接库中去获取。
页面依赖的所有动态链接库都需要被加载。
5.1 定义DLL配置
依赖的两个内置插件:DllPlugin 和 DllReferencePlugin
5.1.1 创建一个DLL配置文件webpack_dll.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
5.1.2 output.libraryTarget 规定了以哪一种导出你的库 默认以全局变量形式 浏览器支持的形式
具体包括如下:
"var" - 以直接变量输出(默认library方式) var Library = xxx (default)
"this" - 通过设置this的属性输出 this["Library"] = xxx
"commonjs" - 通过设置exports的属性输出 exports["Library"] = xxx
"commonjs2" - 通过设置module.exports的属性输出 module.exports = xxx
"amd" - 以amd方式输出
"umd" - 结合commonjs2/amd/root
5.1.3 打包生成动态链接库
1 |
|
在dist目录下 多出react.dll.js 和 react.manifest.json
react.dll.js 动态链接库 里面包含了 react和react-dom的内容
react.manifest.json 描述链接库(react.dll)中的信息
5.2 在主配置文件中使用动态链接库文件
1 2 3 4 5 6 7 8 |
|
5.3 将动态链接库文件加载到页面中
需要借助两个webpack插件
html-webpack-plugin 产出html文件
html-webpack-include-assets-plugin 将js css资源添加到html中 扩展html插件的功能
1 |
|
配置webpack.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
此时react.dll.js和main.js被自动引入到页面中,并且dll文件在main.js之前加载
6.ParallelUglifyPlugin
这个插件可以帮助有很多入口点的项目加快构建速度。把对JS文件的串行压缩变为开启多个子进程并行进行uglify。
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
执行压缩
1 |
|
7.Tree Shaking
剔除JavaScript中用不上的代码。它依赖静态的ES6模块化语法,例如通过impot和export导入导出
commonJS模块 与 es6模块的区别
commonJS模块:
1.动态加载模块 commonJS 是运行时加载 能够轻松实现懒加载,优化用户体验
2.加载整个模块 commonJS模块中,导出的是整个模块
3.每个模块皆为对象 commonJS模块被视作一个对象
4.值拷贝 commonJS的模块输出和函数的值传递相似,都是值得拷贝
es6模块
1.静态解析 es6模块时 编译时加载 即在解析阶段就确定输出的模块的依赖关系,所以es6模块的import一般写在被引入文件的开头
2.模块不是对象 在es6里,每个模块并不会当做一个对象看待
3.加载的不是整个模块 在es6模块中 一个模块中有好几个export导出
4.模块的引用 es6模块中,导出的并不是模块的值得拷贝,而是这个模块的引用
7.1 保留ES6模块化语法
1 2 3 4 5 6 7 8 9 10 11 12 |
|
7.2 执行生产编译 默认已开启Tree Shaking
1 |
|
什么是Tree Shaking?
有个funs.js 里面有两个函数
1 2 3 |
|
main.js 中依赖funs.js
1 2 3 |
|
在main.js只使用了里面的 sub函数 默认情况下也会将funs.js里面其他没有的函数也打包进来, 如果开启tree shaking 生产编译时
1 |
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to optimize packaging for webpack 4.0. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins
