基本使用
Webpack
是一个静态资源打包工具。
它会以一个或多个文件作为打包的入口,将我们整个项目所有文件编译组合成一个或多个文件输出出去。
输出的文件就是编译好的文件,就可以在浏览器段运行了。
我们将 Webpack
输出的文件叫做 bundle
。
功能介绍
Webpack 本身功能是有限的:
开发模式:仅能编译 JS 中的 ES Module
语法
生产模式:能编译 JS 中的 ES Module
语法,还能压缩 JS 代码
开始使用
资源目录
1 2 3 4 5 6 webpack_code # 项目根目录(所有指令必须在这个目录运行) └── src # 项目源码目录 ├── js # js文件目录 │ ├── count.js │ └── sum.js └── main.js # 项目主文件
创建文件
1 2 3 export default function count (x, y ) { return x - y; }
1 2 3 export default function sum (...args ) { return args.reduce ((p, c ) => p + c, 0 ); }
1 2 3 4 5 import count from "./js/count" ;import sum from "./js/sum" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
下载依赖
打开终端,来到项目根目录。运行以下指令:
此时会生成一个基础的 package.json
文件。
需要注意的是 package.json
中 name
字段不能叫做 webpack
, 否则下一步会报错
1 npm i webpack webpack-cli -D
启用 Webpack
1 npx webpack ./src/main.js --mode=development
1 npx webpack ./src/main.js --mode=production
npx webpack
: 是用来运行本地安装 Webpack
包的。
./src/main.js
: 指定 Webpack
从 main.js
文件开始打包,不但会打包 main.js
,还会将其依赖也一起打包进来。
--mode=xxx
:指定模式(环境)。
观察输出文件
默认 Webpack
会将文件打包输出到 dist
目录下,我们查看 dist
目录下文件情况就好了
小结
Webpack
本身功能比较少,只能处理 js
资源,一旦遇到 css
等其他资源就会报错。
所以我们学习 Webpack
,就是主要学习如何处理其他资源。
基本配置
在开始使用 Webpack
之前,我们需要对 Webpack
的配置有一定的认识。
5 大核心概念
entry(入口)
指示 Webpack 从哪个文件开始打包
output(输出)
指示 Webpack 打包完的文件输出到哪里去,如何命名等
loader(加载器)
webpack 本身只能处理 js、json 等资源,其他资源需要借助 loader,Webpack 才能解析
plugins(插件)
扩展 Webpack 的功能
mode(模式)
主要由两种模式:
开发模式:development
生产模式:production
准备 Webpack 配置文件
在项目根目录下新建文件:webpack.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 module .exports = { entry : "" , output : {}, module : { rules : [], }, plugins : [], mode : "" , };
Webpack 是基于 Node.js 运行的,所以采用 Common.js 模块化规范
修改配置文件
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ ], }, plugins : [ ], mode : "development" , };
运行指令
此时功能和之前一样,也不能处理样式资源
小结
Webpack 将来都通过 webpack.config.js
文件进行配置,来增强 Webpack 的功能
我们后面会以两个模式来分别搭建 Webpack 的配置,先进行开发模式,再完成生产模式
开发模式介绍
开发模式顾名思义就是我们开发代码时使用的模式。
这个模式下我们主要做两件事:
编译代码,使浏览器能识别运行
开发时我们有样式资源、字体图标、图片资源、html 资源等,webpack 默认都不能处理这些资源,所以我们要加载配置来编译这些资源
代码质量检查,树立代码规范
提前检查代码的一些隐患,让代码运行时能更加健壮。
提前检查代码规范和格式,统一团队编码风格,让代码更优雅美观。
处理样式资源
本章节我们学习使用 Webpack 如何处理 Css、Less、Sass、Scss、Styl 样式资源
介绍
Webpack 本身是不能识别样式资源的,所以我们需要借助 Loader 来帮助 Webpack 解析样式资源
我们找 Loader 都应该去官方文档中找到对应的 Loader,然后使用
官方文档找不到的话,可以从社区 Github 中搜索查询
Webpack 官方 Loader 文档
处理 Css 资源
下载包
1 npm i css-loader style-loader -D
注意:需要下载两个 loader
功能介绍
css-loader
:负责将 Css 文件编译成 Webpack 能识别的模块
style-loader
:会动态创建一个 Style 标签,里面放置 Webpack 中 Css 模块内容
此时样式就会以 Style 标签的形式在页面上生效
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : [ "style-loader" , "css-loader" , ], }, ], }, plugins : [], mode : "development" , };
添加 Css 资源
1 2 3 4 5 .box1 { width : 100px ; height : 100px ; background-color : pink; }
1 2 3 4 5 6 7 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/index.css" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <script src ="../dist/main.js" > </script > </body > </html >
运行指令
打开 index.html 页面查看效果
处理 Less 资源
下载包
功能介绍
less-loader
:负责将 Less 文件编译成 Css 文件
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, ], }, plugins : [], mode : "development" , };
添加 Less 资源
1 2 3 4 5 .box2 { width : 100px ; height : 100px ; background-color : deeppink; }
1 2 3 4 5 6 7 8 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/index.css" ;import "./less/index.less" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <script src ="../dist/main.js" > </script > </body > </html >
运行指令
打开 index.html 页面查看效果
处理 Sass 和 Scss 资源
下载包
1 npm i sass-loader sass -D
注意:需要下载两个
功能介绍
sass-loader
:负责将 Sass 文件编译成 css 文件
sass
:sass-loader
依赖 sass
进行编译
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, ], }, plugins : [], mode : "development" , };
添加 Sass 资源
1 2 3 4 5 .box3 width : 100px height: 100px background-color: hotpink
1 2 3 4 5 .box4 { width : 100px ; height : 100px ; background-color : lightpink; }
1 2 3 4 5 6 7 8 9 10 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <div class ="box3" > </div > <div class ="box4" > </div > <script src ="../dist/main.js" > </script > </body > </html >
运行指令
打开 index.html 页面查看效果
处理 Styl 资源
下载包
功能介绍
stylus-loader
:负责将 Styl 文件编译成 Css 文件
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, ], }, plugins : [], mode : "development" , };
添加 Styl 资源
1 2 3 4 5 .box width 100px height 100px background-color pink
1 2 3 4 5 6 7 8 9 10 11 12 import { add } from "./math" ;import count from "./js/count" ;import sum from "./js/sum" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <div class ="box3" > </div > <div class ="box4" > </div > <div class ="box5" > </div > <script src ="../dist/main.js" > </script > </body > </html >
运行指令
打开 index.html 页面查看效果
处理图片资源
过去在 Webpack4 时,我们处理图片资源通过 file-loader
和 url-loader
进行处理
现在 Webpack5 已经将两个 Loader 功能内置到 Webpack 里了,我们只需要简单配置即可处理图片资源
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , }, ], }, plugins : [], mode : "development" , };
添加图片资源
src/images/1.jpeg
src/images/2.png
src/images/3.gif
使用图片资源
1 2 3 4 5 6 .box2 { width : 100px ; height : 100px ; background-image : url ("../images/1.jpeg" ); background-size : cover; }
1 2 3 4 5 .box3 width : 100px height: 100px background-image: url ("../images/2.png" ) background-size: cover
1 2 3 4 5 .box5 width 100px height 100px background-image url("../images/3 .gif ") background-size cover
运行指令
打开 index.html 页面查看效果
输出资源情况
此时如果查看 dist 目录的话,会发现多了三张图片资源
因为 Webpack 会将所有打包好的资源输出到 dist 目录下
因为经过 style-loader
的处理,样式资源打包到 main.js 里面去了,所以没有额外输出出来
对图片资源进行优化
将小于某个大小的图片转化成 data URI 形式(Base64 格式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 } } }, ], }, plugins : [], mode : "development" , };
此时输出的图片文件就只有两张,有一张图片以 data URI 形式内置到 js 中了 (注意:需要将上次打包生成的文件清空,再重新打包才有效果)
修改输出资源的名称和路径
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, ], }, plugins : [], mode : "development" , };
修改 index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <div class ="box3" > </div > <div class ="box4" > </div > <div class ="box5" > </div > <script src ="../dist/static/js/main.js" > </script > </body > </html >
运行指令
(注意:需要将上次打包生成的文件清空,再重新打包才有效果)
1 2 3 4 5 6 ├── dist └── static ├── imgs │ └── 7003350e.png └── js └── main.js
自动清空上次打包资源
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 40 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, ], }, plugins : [], mode : "development" , };
运行指令
观察 dist 目录资源情况
处理字体图标资源
下载字体图标文件
打开阿里巴巴矢量图标库
选择想要的图标添加到购物车,统一下载到本地
添加字体图标资源
src/fonts/iconfont.ttf
src/fonts/iconfont.woff
src/fonts/iconfont.woff2
src/css/iconfont.css
src/main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 import { add } from "./math" ;import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;console .log (count (2 , 1 ));console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <div class ="box3" > </div > <div class ="box4" > </div > <div class ="box5" > </div > <i class ="iconfont icon-arrow-down" > </i > <i class ="iconfont icon-ashbin" > </i > <i class ="iconfont icon-browse" > </i > <script src ="../dist/static/js/main.js" > </script > </body > </html >
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, ], }, plugins : [], mode : "development" , };
type: "asset/resource"
和type: "asset"
的区别:
type: "asset/resource"
相当于file-loader
, 将文件转化成 Webpack 能识别的资源,其他不做处理
type: "asset"
相当于url-loader
, 将文件转化成 Webpack 能识别的资源,同时小于某个大小的资源会处理成 data URI 形式
运行指令
打开 index.html 页面查看效果
处理其他资源
开发中可能还存在一些其他资源,如音视频等,我们也一起处理了
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 const path = require ("path" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?|map4|map3|avi)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, ], }, plugins : [], mode : "development" , };
就是在处理字体图标资源基础上增加其他文件类型,统一处理即可
运行指令
打开 index.html 页面查看效果
处理 js 资源
有人可能会问,js 资源 Webpack 不能已经处理了吗,为什么我们还要处理呢?
原因是 Webpack 对 js 处理是有限的,只能编译 js 中 ES 模块化语法,不能编译其他语法,导致 js 不能在 IE 等浏览器运行,所以我们希望做一些兼容性处理。
其次开发中,团队对代码格式是有严格要求的,我们不能由肉眼去检测代码格式,需要使用专业的工具来检测。
针对 js 兼容性处理,我们使用 Babel 来完成
针对代码格式,我们使用 Eslint 来完成
我们先完成 Eslint,检测代码格式无误后,在由 Babel 做代码兼容性处理
Eslint
可组装的 JavaScript 和 JSX 检查工具。
这句话意思就是:它是用来检测 js 和 jsx 语法的工具,可以配置各项功能
我们使用 Eslint,关键是写 Eslint 配置文件,里面写上各种 rules 规则,将来运行 Eslint 时就会以写的规则对代码进行检查
配置文件
配置文件由很多种写法:
.eslintrc.*
:新建文件,位于项目根目录
.eslintrc
.eslintrc.js
.eslintrc.json
区别在于配置格式不一样
package.json
中 eslintConfig
:不需要创建文件,在原有文件基础上写
ESLint 会查找和自动读取它们,所以以上配置文件只需要存在一个即可
具体配置
我们以 .eslintrc.js
配置文件为例:
1 2 3 4 5 6 7 8 9 10 module .exports = { parserOptions : {}, rules : {}, extends : [], };
parserOptions 解析选项
1 2 3 4 5 6 7 parserOptions : { ecmaVersion : 6 , sourceType : "module" , ecmaFeatures : { jsx : true } }
rules 具体规则
"off"
或 0
- 关闭规则
"warn"
或 1
- 开启规则,使用警告级别的错误:warn
(不会导致程序退出)
"error"
或 2
- 开启规则,使用错误级别的错误:error
(当被触发的时候,程序会退出)
1 2 3 4 5 6 7 8 9 10 11 12 rules : { semi : "error" , 'array-callback-return' : 'warn' , 'default-case' : [ 'warn' , { commentPattern : '^no default$' } ], eqeqeq : [ 'warn' , 'smart' ], }
更多规则详见:规则文档
extends 继承
开发中一点点写 rules 规则太费劲了,所以有更好的办法,继承现有的规则。
现有以下较为有名的规则:
1 2 3 4 5 6 7 8 9 module .exports = { extends : ["react-app" ], rules : { eqeqeq : ["warn" , "smart" ], }, };
在 Webpack 中使用
下载包
1 npm i eslint-webpack-plugin eslint -D
定义 Eslint 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 module .exports = { extends : ["eslint:recommended" ], env : { node : true , browser : true , }, parserOptions : { ecmaVersion : 6 , sourceType : "module" , }, rules : { "no-var" : 2 , }, };
修改 js 文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;var result1 = count (2 , 1 );console .log (result1);var result2 = sum (1 , 2 , 3 , 4 );console .log (result2);
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "src" ), }), ], mode : "development" , };
运行指令
在控制台查看 Eslint 检查效果
VSCode Eslint 插件
打开 VSCode,下载 Eslint 插件,即可不用编译就能看到错误,可以提前解决
但是此时就会对项目所有文件默认进行 Eslint 检查了,我们 dist 目录下的打包后文件就会报错。但是我们只需要检查 src 下面的文件,不需要检查 dist 下面的文件。
所以可以使用 Eslint 忽略文件解决。在项目根目录新建下面文件:
Babel
JavaScript 编译器。
主要用于将 ES6 语法编写的代码转换为向后兼容的 JavaScript 语法,以便能够运行在当前和旧版本的浏览器或其他环境中
配置文件
配置文件由很多种写法:
Babel 会查找和自动读取它们,所以以上配置文件只需要存在一个即可
具体配置
我们以 babel.config.js
配置文件为例:
1 2 3 4 module .exports = { presets : [], };
presets 预设
简单理解:就是一组 Babel 插件, 扩展 Babel 功能
@babel/preset-env
: 一个智能预设,允许您使用最新的 JavaScript。
@babel/preset-react
:一个用来编译 React jsx 语法的预设
@babel/preset-typescript
:一个用来编译 TypeScript 语法的预设
在 Webpack 中使用
下载包
1 npm i babel-loader @babel/core @babel/preset-env -D
定义 Babel 配置文件
1 2 3 module .exports = { presets : ["@babel/preset-env" ], };
修改 js 文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "src" ), }), ], mode : "development" , };
运行指令
打开打包后的 dist/static/js/main.js
文件查看,会发现箭头函数等 ES6 语法已经转换了
处理 Html 资源
下载包
1 npm i html-webpack-plugin -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "public/index.html" ), }), ], mode : "development" , };
修改 index.html
去掉引入的 js 文件,因为 HtmlWebpackPlugin 会自动引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > webpack5</title > </head > <body > <h1 > Hello Webpack5</h1 > <div class ="box1" > </div > <div class ="box2" > </div > <div class ="box3" > </div > <div class ="box4" > </div > <div class ="box5" > </div > <i class ="iconfont icon-arrow-down" > </i > <i class ="iconfont icon-ashbin" > </i > <i class ="iconfont icon-browse" > </i > </body > </html >
运行指令
此时 dist 目录就会输出一个 index.html 文件
开发服务器&自动化
每次写完代码都需要手动输入指令才能编译代码,太麻烦了,我们希望一切自动化
下载包
1 npm i webpack-dev-server -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "public/index.html" ), }), ], devServer : { host : "localhost" , port : "3000" , open : true , }, mode : "development" , };
运行指令
注意运行指令发生了变化
并且当你使用开发服务器时,所有代码都会在内存中编译打包,并不会输出到 dist 目录下。
开发时我们只关心代码能运行,有效果即可,至于代码被编译成什么样子,我们并不需要知道。
生产模式介绍
生产模式是开发完成代码后,我们需要得到代码将来部署上线。
这个模式下我们主要对代码进行优化,让其运行性能更好。
优化主要从两个角度出发:
优化代码运行性能
优化代码打包速度
生产模式准备
我们分别准备两个配置文件来放不同的配置
文件目录
1 2 3 4 5 6 7 8 9 10 11 12 ├── webpack-test (项目根目录) ├── config (Webpack配置文件目录) │ ├── webpack.dev.js(开发模式配置文件) │ └── webpack.prod.js(生产模式配置文件) ├── node_modules (下载包存放目录) ├── src (项目源码目录,除了html其他都在src里面) │ └── 略 ├── public (项目html文件) │ └── index.html ├── .eslintrc.js(Eslint配置文件) ├── babel.config.js(Babel配置文件) └── package.json (包的依赖管理配置文件)
修改 webpack.dev.js
因为文件目录变了,所以所有绝对路径需要回退一层目录才能找到对应的文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : undefined , filename : "static/js/main.js" , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), ], devServer : { host : "localhost" , port : "3000" , open : true , }, mode : "development" , };
运行开发模式的指令:
1 npx webpack serve --config ./config/webpack.dev.js
修改 webpack.prod.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), ], mode : "production" , };
运行生产模式的指令:
1 npx webpack --config ./config/webpack.prod.js
配置运行指令
为了方便运行不同模式的指令,我们将指令定义在 package.json 中 scripts 里面
1 2 3 4 5 6 7 8 9 { "scripts" : { "start" : "npm run dev" , "dev" : "npx webpack serve --config ./config/webpack.dev.js" , "build" : "npx webpack --config ./config/webpack.prod.js" } }
以后启动指令:
开发模式:npm start
或 npm run dev
生产模式:npm run build
Css 处理
提取 Css 成单独文件
Css 文件目前被打包到 js 文件中,当 js 文件加载时,会创建一个 style 标签来生成样式
这样对于网站来说,会出现闪屏现象,用户体验不好
我们应该是单独的 Css 文件,通过 link 标签加载性能才好
下载包
1 npm i mini-css-extract-plugin -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : [MiniCssExtractPlugin .loader , "css-loader" ], }, { test : /\.less$/ , use : [MiniCssExtractPlugin .loader , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : [MiniCssExtractPlugin .loader , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : [MiniCssExtractPlugin .loader , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], mode : "production" , };
运行指令
Css 兼容性处理
下载包
1 npm i postcss-loader postcss postcss-preset-env -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, ], }, { test : /\.less$/ , use : [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, "less-loader" , ], }, { test : /\.s[ac]ss$/ , use : [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, "sass-loader" , ], }, { test : /\.styl$/ , use : [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, "stylus-loader" , ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], mode : "production" , };
控制兼容性
我们可以在 package.json
文件中添加 browserslist
来控制样式的兼容性做到什么程度。
1 2 3 4 { "browserslist" : [ "ie >= 8" ] }
想要知道更多的 browserslist
配置,查看browserslist 文档
以上为了测试兼容性所以设置兼容浏览器 ie8 以上。
实际开发中我们一般不考虑旧版本浏览器了,所以我们可以这样设置:
1 2 3 4 5 6 7 8 { "browserslist" : [ "last 2 version" , "> 1%" , "not dead" ] }
合并配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], mode : "production" , };
运行指令
Css 压缩
下载包
1 npm i css-minimizer-webpack-plugin -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), new CssMinimizerPlugin (), ], mode : "production" , };
运行指令
html 压缩
默认生产模式已经开启了:html 压缩和 js 压缩
不需要额外进行配置
总结
我们学会了 Webpack 基本使用,掌握了以下功能:
两种开发模式
开发模式:代码能编译自动化运行
生产模式:代码编译优化输出
Webpack 基本功能
开发模式:可以编译 ES Module 语法
生产模式:可以编译 ES Module 语法,压缩 js 代码
Webpack 配置文件
5 个核心概念
entry
output
loader
plugins
mode
devServer 配置
Webpack 脚本指令用法
webpack
直接打包输出
webpack serve
启动开发服务器,内存编译打包没有输出
Webpack5高级优化
介绍
主要介绍 Webpack 高级配置。
所谓高级配置其实就是进行 Webpack 优化,让我们代码在编译/运行时性能更好~
我们会从以下角度来进行优化:
提升开发体验
提升打包构建速度
减少代码体积
优化代码运行性能
提升开发体验
SourceMap
为什么
开发时我们运行的代码是经过 webpack 编译后的,例如下面这个样子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 (() => { "use strict" ; var __webpack_modules__ = ({ "./node_modules/css-loader/dist/cjs.js!./node_modules/less-loader/dist/cjs.js!./src/less/index.less" : ((module , __webpack_exports__, __webpack_require__ ) => { eval ("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../node_modules/css-loader/dist/runtime/noSourceMaps.js */ \"./node_modules/css-loader/dist/runtime/noSourceMaps.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../node_modules/css-loader/dist/runtime/api.js */ \"./node_modules/css-loader/dist/runtime/api.js\");\n/* harmony import */ var _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1__);\n// Imports\n\n\nvar ___CSS_LOADER_EXPORT___ = _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default()((_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default()));\n// Module\n___CSS_LOADER_EXPORT___.push([module.id, \".box2 {\\n width: 100px;\\n height: 100px;\\n background-color: deeppink;\\n}\\n\", \"\"]);\n// Exports\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (___CSS_LOADER_EXPORT___);\n\n\n//# sourceURL=webpack://webpack5/./src/less/index.less?./node_modules/css-loader/dist/cjs.js!./node_modules/less-loader/dist/cjs.js" ); }),
所有 css 和 js 合并成了一个文件,并且多了其他代码。此时如果代码运行出错那么提示代码错误位置我们是看不懂的。一旦将来开发代码文件很多,那么很难去发现错误出现在哪里。
所以我们需要更加准确的错误提示,来帮助我们更好的开发代码。
是什么
SourceMap(源代码映射)是一个用来生成源代码与构建后代码一一映射的文件的方案。
它会生成一个 xxx.map 文件,里面包含源代码和构建后代码每一行、每一列的映射关系。当构建后代码出错了,会通过 xxx.map 文件,从构建后代码出错位置找到映射后源代码出错位置,从而让浏览器提示源代码文件出错位置,帮助我们更快的找到错误根源。
怎么用
通过查看Webpack DevTool 文档 可知,SourceMap 的值有很多种情况.
但实际开发时我们只需要关注两种情况即可:
开发模式:cheap-module-source-map
优点:打包编译速度快,只包含行映射
缺点:没有列映射
1 2 3 4 5 module .exports = { mode : "development" , devtool : "cheap-module-source-map" , };
1 2 3 4 5 module .exports = { mode : "production" , devtool : "source-map" , };
提升打包构建速度
HotModuleReplacement
为什么
开发时我们修改了其中一个模块代码,Webpack 默认会将所有模块全部重新打包编译,速度很慢。
所以我们需要做到修改某个模块代码,就只有这个模块代码需要重新打包编译,其他模块不变,这样打包速度就能很快。
是什么
HotModuleReplacement(HMR/热模块替换):在程序运行中,替换、添加或删除模块,而无需重新加载整个页面。
怎么用
基本配置
1 2 3 4 5 6 7 8 9 module .exports = { devServer : { host : "localhost" , port : "3000" , open : true , hot : true , }, };
此时 css 样式经过 style-loader 处理,已经具备 HMR 功能了。 但是 js 还不行。
JS 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);if (module .hot ) { module .hot .accept ("./js/count.js" , function (count ) { const result1 = count (2 , 1 ); console .log (result1); }); module .hot .accept ("./js/sum.js" , function (sum ) { const result2 = sum (1 , 2 , 3 , 4 ); console .log (result2); }); }
上面这样写会很麻烦,所以实际开发我们会使用其他 loader 来解决。
比如:vue-loader , react-hot-loader 。
OneOf
为什么
打包时每个文件都会经过所有 loader 处理,虽然因为 test
正则原因实际没有处理上,但是都要过一遍。比较慢。
是什么
顾名思义就是只能匹配上一个 loader, 剩下的就不匹配了。
怎么用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : undefined , filename : "static/js/main.js" , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , exclude : /node_modules/ , loader : "babel-loader" , }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), ], devServer : { host : "localhost" , port : "3000" , open : true , hot : true , }, mode : "development" , devtool : "cheap-module-source-map" , };
生产模式也是如此配置。
Include/Exclude
为什么
开发时我们需要使用第三方的库或插件,所有文件都下载到 node_modules 中了。而这些文件是不需要编译可以直接使用的。
所以我们在对 js 文件处理时,要排除 node_modules 下面的文件。
是什么
包含,只处理 xxx 文件
排除,除了 xxx 文件以外其他文件都处理
怎么用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : undefined , filename : "static/js/main.js" , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), loader : "babel-loader" , }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), ], devServer : { host : "localhost" , port : "3000" , open : true , hot : true , }, mode : "development" , devtool : "cheap-module-source-map" , };
生产模式也是如此配置。
Cache
为什么
每次打包时 js 文件都要经过 Eslint 检查 和 Babel 编译,速度比较慢。
我们可以缓存之前的 Eslint 检查 和 Babel 编译结果,这样第二次打包时速度就会更快了。
是什么
对 Eslint 检查 和 Babel 编译结果进行缓存。
怎么用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : undefined , filename : "static/js/main.js" , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : ["style-loader" , "css-loader" ], }, { test : /\.less$/ , use : ["style-loader" , "css-loader" , "less-loader" ], }, { test : /\.s[ac]ss$/ , use : ["style-loader" , "css-loader" , "sass-loader" ], }, { test : /\.styl$/ , use : ["style-loader" , "css-loader" , "stylus-loader" ], }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , }, }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), ], devServer : { host : "localhost" , port : "3000" , open : true , hot : true , }, mode : "development" , devtool : "cheap-module-source-map" , };
Thead
为什么
当项目越来越庞大时,打包速度越来越慢,甚至于需要一个下午才能打包出来代码。这个速度是比较慢的。
我们想要继续提升打包速度,其实就是要提升 js 的打包速度,因为其他文件都比较少。
而对 js 文件处理主要就是 eslint 、babel、Terser 三个工具,所以我们要提升它们的运行速度。
我们可以开启多进程同时处理 js 文件,这样速度就比之前的单进程打包更快了。
是什么
多进程打包:开启电脑的多个进程同时干一件事,速度更快。
需要注意:请仅在特别耗时的操作中使用,因为每个进程启动就有大约为 600ms 左右开销。
怎么用
我们启动进程的数量就是我们 CPU 的核数。
如何获取 CPU 的核数,因为每个电脑都不一样。
1 2 3 4 const os = require ("os" );const threads = os.cpus ().length ;
下载包
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], optimization : { minimize : true , minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads }) ], }, mode : "production" , devtool : "source-map" , };
我们目前打包的内容都很少,所以因为启动进程开销原因,使用多进程打包实际上会显著的让我们打包时间变得很长。
减少代码体积
Tree Shaking
为什么
开发时我们定义了一些工具函数库,或者引用第三方工具函数库或组件库。
如果没有特殊处理的话我们打包时会引入整个库,但是实际上可能我们可能只用上极小部分的功能。
这样将整个库都打包进来,体积就太大了。
是什么
Tree Shaking
是一个术语,通常用于描述移除 JavaScript 中的没有使用上的代码。
注意:它依赖 ES Module
。
怎么用
Webpack 已经默认开启了这个功能,无需其他配置。
Babel
为什么
Babel 为编译的每个文件都插入了辅助代码,使代码体积过大!
Babel 对一些公共方法使用了非常小的辅助代码,比如 _extend
。默认情况下会被添加到每一个需要它的文件中。
你可以将这些辅助代码作为一个独立模块,来避免重复引入。
是什么
@babel/plugin-transform-runtime
: 禁用了 Babel 自动对每个文件的 runtime 注入,而是引入 @babel/plugin-transform-runtime
并且使所有辅助代码从这里引用。
怎么用
下载包
1 npm i @babel/plugin-transform-runtime -D
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|webp)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), ] ], mode : "production" , devtool : "source-map" , };
Image Minimizer
为什么
开发如果项目中引用了较多图片,那么图片体积会比较大,将来请求速度比较慢。
我们可以对图片进行压缩,减少图片体积。
注意:如果项目中图片都是在线链接,那么就不需要了。本地项目静态图片才需要进行压缩。
是什么
image-minimizer-webpack-plugin
: 用来压缩图片的插件
怎么用
下载包
1 npm i image-minimizer-webpack-plugin imagemin -D
还有剩下包需要下载,有两种模式:
1 npm install imagemin-gifsicle imagemin-jpegtran imagemin-optipng imagemin-svgo -D
1 npm install imagemin-gifsicle imagemin-mozjpeg imagemin-pngquant imagemin-svgo -D
有损/无损压缩的区别
配置
我们以无损压缩配置为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], }, mode : "production" , devtool : "source-map" , };
优化代码运行性能
Code Split
为什么
打包代码时会将所有 js 文件打包到一个文件中,体积太大了。我们如果只要渲染首页,就应该只加载首页的 js 文件,其他文件不应该加载。
所以我们需要将打包生成的文件进行代码分割,生成多个 js 文件,渲染哪个页面就只加载某个 js 文件,这样加载的资源就少,速度就更快。
是什么
代码分割(Code Split)主要做了两件事:
分割文件:将打包生成的文件进行分割,生成多个 js 文件。
按需加载:需要哪个文件就加载哪个文件。
怎么用
代码分割实现方式有不同的方式,为了更加方便体现它们之间的差异,我们会分别创建新的文件来演示
1. 多入口
文件目录
1 2 3 4 5 6 ├── public ├── src | ├── app.js | └── main.js ├── package.json └── webpack.config.js
下载包
1 npm i webpack webpack-cli html-webpack-plugin -D
新建文件
内容无关紧要,主要观察打包输出的结果
1 console .log ("hello app" );
1 console .log ("hello main" );
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 const path = require ("path" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : { main : "./src/main.js" , app : "./src/app.js" , }, output : { path : path.resolve (__dirname, "./dist" ), filename : "js/[name].js" , clear : true , }, plugins : [ new HtmlWebpackPlugin ({ template : "./public/index.html" , }), ], mode : "production" , };
运行指令
此时在 dist 目录我们能看到输出了两个 js 文件。
总结:配置了几个入口,至少输出几个 js 文件。
2. 提取重复代码
如果多入口文件中都引用了同一份代码,我们不希望这份代码被打包到两个文件中,导致代码重复,体积更大。
我们需要提取多入口的重复代码,只打包生成一个 js 文件,其他文件引用它就好。
修改文件
1 2 3 4 import { sum } from "./math" ;console .log ("hello app" );console .log (sum (1 , 2 , 3 , 4 ));
1 2 3 4 import { sum } from "./math" ;console .log ("hello main" );console .log (sum (1 , 2 , 3 , 4 , 5 ));
1 2 3 export const sum = (...args ) => { return args.reduce ((p, c ) => p + c, 0 ); };
修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 const path = require ("path" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : { main : "./src/main.js" , app : "./src/app.js" , }, output : { path : path.resolve (__dirname, "./dist" ), filename : "js/[name].js" , clean : true , }, plugins : [ new HtmlWebpackPlugin ({ template : "./public/index.html" , }), ], mode : "production" , optimization : { splitChunks : { chunks : "all" , cacheGroups : { default : { minSize : 0 , minChunks : 2 , priority : -20 , reuseExistingChunk : true , }, }, }, }, };
运行指令
此时我们会发现生成 3 个 js 文件,其中有一个就是提取的公共模块。
按需加载,动态导入
想要实现按需加载,动态导入模块。还需要额外配置:
修改文件
1 2 3 4 5 6 7 8 9 console .log ("hello main" );document .getElementById ("btn" ).onclick = function ( ) { import ("./math.js" ).then (({ sum } ) => { alert (sum (1 , 2 , 3 , 4 , 5 )); }); };
1 console .log ("hello app" );
1 2 3 4 5 6 7 8 9 10 11 12 13 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta http-equiv ="X-UA-Compatible" content ="IE=edge" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Code Split</title > </head > <body > <h1 > hello webpack</h1 > <button id ="btn" > 计算</button > </body > </html >
运行指令
我们可以发现,一旦通过 import 动态导入语法导入模块,模块就被代码分割,同时也能按需加载了。
单入口
开发时我们可能是单页面应用(SPA),只有一个入口(单入口)。那么我们需要这样配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 const path = require ("path" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "./dist" ), filename : "js/[name].js" , clean : true , }, plugins : [ new HtmlWebpackPlugin ({ template : "./public/index.html" , }), ], mode : "production" , optimization : { splitChunks : { chunks : "all" , }, };
更新配置
最终我们会使用单入口+代码分割+动态导入方式来进行配置。更新之前的配置文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/main.js" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, generator : { filename : "static/imgs/[hash:8][ext][query]" , }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , generator : { filename : "static/media/[hash:8][ext][query]" , }, }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/main.css" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, }, mode : "production" , devtool : "source-map" , };
给动态导入文件取名称
修改文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);if (module .hot ) { module .hot .accept ("./js/sum.js" , function (sum ) { const result2 = sum (1 , 2 , 3 , 4 ); console .log (result2); }); } document .getElementById ("btn" ).onClick = function ( ) { import ( "./js/math.js" ).then (({ count } ) => { console .log (count (2 , 1 )); }); };
eslint 配置
1 npm i eslint-plugin-import -D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 module .exports = { extends : ["eslint:recommended" ], env : { node : true , browser : true , }, plugins : ["import" ], parserOptions : { ecmaVersion : 6 , sourceType : "module" , }, rules : { "no-var" : 2 , }, };
统一命名配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/[name].js" , chunkFilename : "static/js/[name].chunk.js" , assetModuleFilename : "static/media/[name].[hash][ext]" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/[name].css" , chunkFilename : "static/css/[name].chunk.css" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, }, mode : "production" , devtool : "source-map" , };
运行指令
观察打包输出 js 文件名称。
Preload / Prefetch
为什么
我们前面已经做了代码分割,同时会使用 import 动态导入语法来进行代码按需加载(我们也叫懒加载,比如路由懒加载就是这样实现的)。
但是加载速度还不够好,比如:是用户点击按钮时才加载这个资源的,如果资源体积很大,那么用户会感觉到明显卡顿效果。
我们想在浏览器空闲时间,加载后续需要使用的资源。我们就需要用上 Preload
或 Prefetch
技术。
是什么
Preload
:告诉浏览器立即加载资源。
Prefetch
:告诉浏览器在空闲时才开始加载资源。
它们共同点:
它们区别:
Preload
加载优先级高,Prefetch
加载优先级低。
Preload
只能加载当前页面需要使用的资源,Prefetch
可以加载当前页面资源,也可以加载下一个页面需要使用的资源。
总结:
当前页面优先级高的资源用 Preload
加载。
下一个页面需要使用的资源用 Prefetch
加载。
它们的问题:兼容性较差。
我们可以去 Can I Use 网站查询 API 的兼容性问题。
Preload
相对于 Prefetch
兼容性好一点。
怎么用
下载包
1 npm i @vue/preload-webpack-plugin -D
配置 webpack.prod.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const PreloadWebpackPlugin = require ("@vue/preload-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/[name].js" , chunkFilename : "static/js/[name].chunk.js" , assetModuleFilename : "static/media/[name].[hash][ext]" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/[name].css" , chunkFilename : "static/css/[name].chunk.css" , }), new PreloadWebpackPlugin ({ rel : "preload" , as : "script" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, }, mode : "production" , devtool : "source-map" , };
Network Cache
为什么
将来开发时我们对静态资源会使用缓存来优化,这样浏览器第二次请求资源就能读取缓存了,速度很快。
但是这样的话就会有一个问题, 因为前后输出的文件名是一样的,都叫 main.js,一旦将来发布新版本,因为文件名没有变化导致浏览器会直接读取缓存,不会加载新资源,项目也就没法更新了。
所以我们从文件名入手,确保更新前后文件名不一样,这样就可以做缓存了。
是什么
它们都会生成一个唯一的 hash 值。
fullhash(webpack4 是 hash)
每次修改任何一个文件,所有文件名的 hash 值都将改变。所以一旦修改了任何一个文件,整个项目的文件缓存都将失效。
chunkhash
根据不同的入口文件(Entry)进行依赖文件解析、构建对应的 chunk,生成对应的哈希值。我们 js 和 css 是同一个引入,会共享一个 hash 值。
contenthash
根据文件内容生成 hash 值,只有文件内容变化了,hash 值才会变化。所有文件 hash 值是独享且不同的。
怎么用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const PreloadWebpackPlugin = require ("@vue/preload-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/[name].[contenthash:8].js" , chunkFilename : "static/js/[name].[contenthash:8].chunk.js" , assetModuleFilename : "static/media/[name].[hash][ext]" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/[name].[contenthash:8].css" , chunkFilename : "static/css/[name].[contenthash:8].chunk.css" , }), new PreloadWebpackPlugin ({ rel : "preload" , as : "script" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, }, mode : "production" , devtool : "source-map" , };
当我们修改 math.js 文件再重新打包的时候,因为 contenthash 原因,math.js 文件 hash 值发生了变化(这是正常的)。
但是 main.js 文件的 hash 值也发生了变化,这会导致 main.js 的缓存失效。明明我们只修改 math.js, 为什么 main.js 也会变身变化呢?
原因:
更新前:math.xxx.js, main.js 引用的 math.xxx.js
更新后:math.yyy.js, main.js 引用的 math.yyy.js, 文件名发生了变化,间接导致 main.js 也发生了变化
解决:
将 hash 值单独保管在一个 runtime 文件中。
我们最终输出三个文件:main、math、runtime。当 math 文件发送变化,变化的是 math 和 runtime 文件,main 不变。
runtime 文件只保存文件的 hash 值和它们与文件关系,整个文件体积就比较小,所以变化重新请求的代价也小。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const PreloadWebpackPlugin = require ("@vue/preload-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/[name].[contenthash:8].js" , chunkFilename : "static/js/[name].[contenthash:8].chunk.js" , assetModuleFilename : "static/media/[name].[hash][ext]" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/[name].[contenthash:8].css" , chunkFilename : "static/css/[name].[contenthash:8].chunk.css" , }), new PreloadWebpackPlugin ({ rel : "preload" , as : "script" , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, runtimeChunk : { name : (entrypoint ) => `runtime~${entrypoint.name} ` , }, }, mode : "production" , devtool : "source-map" , };
Core-js
为什么
过去我们使用 babel 对 js 代码进行了兼容性处理,其中使用@babel/preset-env 智能预设来处理兼容性问题。
它能将 ES6 的一些语法进行编译转换,比如箭头函数、点点点运算符等。但是如果是 async 函数、promise 对象、数组的一些方法(includes)等,它没办法处理。
所以此时我们 js 代码仍然存在兼容性问题,一旦遇到低版本浏览器会直接报错。所以我们想要将 js 兼容性问题彻底解决
是什么
core-js
是专门用来做 ES6 以及以上 API 的 polyfill
。
polyfill
翻译过来叫做垫片/补丁。就是用社区上提供的一段代码,让我们在不兼容某些新特性的浏览器上,使用该新特性。
怎么用
修改 main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);const promise = Promise .resolve ();promise.then (() => { console .log ("hello promise" ); });
此时 Eslint 会对 Promise 报错。
修改配置文件
1 npm i @babel/eslint-parser -D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 module .exports = { extends : ["eslint:recommended" ], parser : "@babel/eslint-parser" , env : { node : true , browser : true , }, plugins : ["import" ], parserOptions : { ecmaVersion : 6 , sourceType : "module" , }, rules : { "no-var" : 2 , }, };
运行指令
此时观察打包输出的 js 文件,我们发现 Promise 语法并没有编译转换,所以我们需要使用 core-js
来进行 polyfill
。
使用core-js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import "core-js" ;import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);const promise = Promise .resolve ();promise.then (() => { console .log ("hello promise" ); });
这样引入会将所有兼容性代码全部引入,体积太大了。我们只想引入 promise 的 polyfill
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import "core-js/es/promise" ;import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);const promise = Promise .resolve ();promise.then (() => { console .log ("hello promise" ); });
只引入打包 promise 的 polyfill
,打包体积更小。但是将来如果还想使用其他语法,我需要手动引入库很麻烦。
自动按需引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);const promise = Promise .resolve ();promise.then (() => { console .log ("hello promise" ); });
1 2 3 4 5 6 7 8 9 10 module .exports = { presets : [ [ "@babel/preset-env" , { useBuiltIns : "usage" , corejs : { version : "3" , proposals : true } }, ], ], };
此时就会自动根据我们代码中使用的语法,来按需加载相应的 polyfill
了。
PWA
为什么
开发 Web App 项目,项目一旦处于网络离线情况,就没法访问了。
我们希望给项目提供离线体验。
是什么
渐进式网络应用程序(progressive web application - PWA):是一种可以提供类似于 native app(原生应用程序) 体验的 Web App 的技术。
其中最重要的是,在 离线(offline) 时应用程序能够继续运行功能。
内部通过 Service Workers 技术实现的。
怎么用
下载包
1 npm i workbox-webpack-plugin -D
修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 const os = require ("os" );const path = require ("path" );const ESLintWebpackPlugin = require ("eslint-webpack-plugin" );const HtmlWebpackPlugin = require ("html-webpack-plugin" );const MiniCssExtractPlugin = require ("mini-css-extract-plugin" );const CssMinimizerPlugin = require ("css-minimizer-webpack-plugin" );const TerserPlugin = require ("terser-webpack-plugin" );const ImageMinimizerPlugin = require ("image-minimizer-webpack-plugin" );const PreloadWebpackPlugin = require ("@vue/preload-webpack-plugin" );const WorkboxPlugin = require ("workbox-webpack-plugin" );const threads = os.cpus ().length ;const getStyleLoaders = (preProcessor ) => { return [ MiniCssExtractPlugin .loader , "css-loader" , { loader : "postcss-loader" , options : { postcssOptions : { plugins : [ "postcss-preset-env" , ], }, }, }, preProcessor, ].filter (Boolean ); }; module .exports = { entry : "./src/main.js" , output : { path : path.resolve (__dirname, "../dist" ), filename : "static/js/[name].[contenthash:8].js" , chunkFilename : "static/js/[name].[contenthash:8].chunk.js" , assetModuleFilename : "static/media/[name].[hash][ext]" , clean : true , }, module : { rules : [ { oneOf : [ { test : /\.css$/ , use : getStyleLoaders (), }, { test : /\.less$/ , use : getStyleLoaders ("less-loader" ), }, { test : /\.s[ac]ss$/ , use : getStyleLoaders ("sass-loader" ), }, { test : /\.styl$/ , use : getStyleLoaders ("stylus-loader" ), }, { test : /\.(png|jpe?g|gif|svg)$/ , type : "asset" , parser : { dataUrlCondition : { maxSize : 10 * 1024 , }, }, }, { test : /\.(ttf|woff2?)$/ , type : "asset/resource" , }, { test : /\.js$/ , include : path.resolve (__dirname, "../src" ), use : [ { loader : "thread-loader" , options : { workers : threads, }, }, { loader : "babel-loader" , options : { cacheDirectory : true , cacheCompression : false , plugins : ["@babel/plugin-transform-runtime" ], }, }, ], }, ], }, ], }, plugins : [ new ESLintWebpackPlugin ({ context : path.resolve (__dirname, "../src" ), exclude : "node_modules" , cache : true , cacheLocation : path.resolve ( __dirname, "../node_modules/.cache/.eslintcache" ), threads, }), new HtmlWebpackPlugin ({ template : path.resolve (__dirname, "../public/index.html" ), }), new MiniCssExtractPlugin ({ filename : "static/css/[name].[contenthash:8].css" , chunkFilename : "static/css/[name].[contenthash:8].chunk.css" , }), new PreloadWebpackPlugin ({ rel : "preload" , as : "script" , }), new WorkboxPlugin .GenerateSW ({ clientsClaim : true , skipWaiting : true , }), ], optimization : { minimizer : [ new CssMinimizerPlugin (), new TerserPlugin ({ parallel : threads, }), new ImageMinimizerPlugin ({ minimizer : { implementation : ImageMinimizerPlugin .imageminGenerate , options : { plugins : [ ["gifsicle" , { interlaced : true }], ["jpegtran" , { progressive : true }], ["optipng" , { optimizationLevel : 5 }], [ "svgo" , { plugins : [ "preset-default" , "prefixIds" , { name : "sortAttrs" , params : { xmlnsOrder : "alphabetical" , }, }, ], }, ], ], }, }, }), ], splitChunks : { chunks : "all" , }, }, mode : "production" , devtool : "source-map" , };
修改 main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import count from "./js/count" ;import sum from "./js/sum" ;import "./css/iconfont.css" ;import "./css/index.css" ;import "./less/index.less" ;import "./sass/index.sass" ;import "./sass/index.scss" ;import "./styl/index.styl" ;const result1 = count (2 , 1 );console .log (result1);const result2 = sum (1 , 2 , 3 , 4 );console .log (result2);const promise = Promise .resolve ();promise.then (() => { console .log ("hello promise" ); }); const arr = [1 , 2 , 3 , 4 , 5 ];console .log (arr.includes (5 ));if ("serviceWorker" in navigator) { window .addEventListener ("load" , () => { navigator.serviceWorker .register ("/service-worker.js" ) .then ((registration ) => { console .log ("SW registered: " , registration); }) .catch ((registrationError ) => { console .log ("SW registration failed: " , registrationError); }); }); }
运行指令
此时如果直接通过 VSCode 访问打包后页面,在浏览器控制台会发现 SW registration failed
。
因为我们打开的访问路径是:http://127.0.0.1:5500/dist/index.html
。此时页面会去请求 service-worker.js
文件,请求路径是:http://127.0.0.1:5500/service-worker.js
,这样找不到会 404。
实际 service-worker.js
文件路径是:http://127.0.0.1:5500/dist/service-worker.js
。
解决路径问题
serve 也是用来启动开发服务器来部署代码查看效果的。
此时通过 serve 启动的服务器我们 service-worker 就能注册成功了。
打包时会出现报错:
1 2 Error: Error with 'src\images\1.jpeg': '"C:\Users\86176\Desktop\webpack\webpack_code\node_modules\jpegtran-bin\vendor\jpegtran.exe"' Error with 'src\images\3.gif': spawn C:\Users\86176\Desktop\webpack\webpack_code\node_modules\optipng-bin\vendor\optipng.exe ENOENT
我们需要安装两个文件到 node_modules 中才能解决, 文件可以从课件中找到:
需要复制到 node_modules\jpegtran-bin\vendor
下面
jpegtran 官网地址
需要复制到 node_modules\optipng-bin\vendor
下面
OptiPNG 官网地址
总结
我们从 4 个角度对 webpack 和代码进行了优化:
提升开发体验
使用 Source Map
让开发或上线时代码报错能有更加准确的错误提示。
提升 webpack 提升打包构建速度
使用 HotModuleReplacement
让开发时只重新编译打包更新变化了的代码,不变的代码使用缓存,从而使更新速度更快。
使用 OneOf
让资源文件一旦被某个 loader 处理了,就不会继续遍历了,打包速度更快。
使用 Include/Exclude
排除或只检测某些文件,处理的文件更少,速度更快。
使用 Cache
对 eslint 和 babel 处理的结果进行缓存,让第二次打包速度更快。
使用 Thead
多进程处理 eslint 和 babel 任务,速度更快。(需要注意的是,进程启动通信都有开销的,要在比较多代码处理时使用才有效果)
减少代码体积
使用 Tree Shaking
剔除了没有使用的多余代码,让代码体积更小。
使用 @babel/plugin-transform-runtime
插件对 babel 进行处理,让辅助代码从中引入,而不是每个文件都生成辅助代码,从而体积更小。
使用 Image Minimizer
对项目中图片进行压缩,体积更小,请求速度更快。(需要注意的是,如果项目中图片都是在线链接,那么就不需要了。本地项目静态图片才需要进行压缩。)
优化代码运行性能
使用 Code Split
对代码进行分割成多个 js 文件,从而使单个文件体积更小,并行加载 js 速度更快。并通过 import 动态导入语法进行按需加载,从而达到需要使用时才加载该资源,不用时不加载资源。
使用 Preload / Prefetch
对代码进行提前加载,等未来需要使用时就能直接使用,从而用户体验更好。
使用 Network Cache
能对输出资源文件进行更好的命名,将来好做缓存,从而用户体验更好。
使用 Core-js
对 js 进行兼容性处理,让我们代码能运行在低版本浏览器。
使用 PWA
能让代码离线也能访问,从而提升用户体验。