定义
- 本质上,webpack是一个用于现代JavaScript应用程序的静态模块打包工具。当webpack处理应用程序时,它会在内部从一个或多个入口点构建一个依赖图(dependency graph),然后将你项目中所需的每一个模块组合成一个或多个bundles,它们均为静态资源,用于展示你的内容。
- 静态模块:指的是编写代码过程中的html,css,js,图片等固定内容的文件
- 打包:把静态模块内容,压缩,整合,转译等(前端工程化)
- 把less/sass 转成 css代码
- 把ES6+降级成 ES5
- 支持多种模块标准语法(例如:CommonJs、ECMAScript)
使用
- 在终端中输入命令为当前开发环境配置webpack
1
| npm i webpack webpack-cli --save-dev
|
- 在package.json文件中编写自定义命令
1 2 3
| "scripts": { "build": "webpack" },
|
- 运行打包命令,自动产生dist分发文件夹(压缩和优化后,用于最终运行的代码)
修改Webpack打包的出口和入口
- 在项目根目录下,新建 webpack.config.js 配置文件
- 导出配置对象,配置入口,出口文件的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const path = require('path')
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true } }
|
自动生成html文件
- 下载 html-webpack-plugin 本地软件包
1
| npm i html-webpack-plugin --save-dev
|
- 配置 webpack.config.js 让 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
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }) ] }
|
打包 CSS 代码
加载器 css-loader:解析 css 代码
加载器 style-loader:把解析后的 css 代码插入到 DOM
- 在js文件中引入css文件代码
1
| import css from "file.css"
|
- 下载 css-loader 和 style-loader 本地软件包
1
| npm i css-loader style-loader --save-dev
|
- 配置 webpack.config.js 让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
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }) ], module: { rules: [ { test: /\.css$/i, use: ["style-loader", "css-loader"], }, ], } }
|
优化-提取 CSS 代码
插件:mini-css-extract-plugin:提取 css 代码
- 下载 mini-css-extract-plugin 本地软件包
1
| npm install --save-dev mini-css-extract-plugin
|
- 配置 webpack.config.js 让 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 38 39 40
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin({ filename: './login/index.css' }) ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, "css-loader"] }, ], } }
|
注意mini-css-extract-plugin插件和style-loader不能一起使用
mini-css-extract-plugin插件是提取css代码,并生成css文件
优化-压缩 CSS 代码
- 下载 css-minimizer-webpack-plugin 本地软件包
1
| npm install css-minimizer-webpack-plugin --save-dev
|
- 配置 webpack.config.js 让 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 38 39 40 41 42 43 44 45 46 47 48 49
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin() ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, "css-loader"] }, ], }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
|
打包 less 代码
加载器 less-loader:把 less 代码编译为 css 代码
- 将less代码文件引入js文件中
- 下载 less 和 less-loader 本地软件包
1
| npm i less less-loader --save-dev
|
- 配置 webpack.config.js 让 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin() ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, "css-loader"] }, { test: /\.less$/i, use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"] } ], }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
|
打包图片
资源模块:Webpack5 内置资源模块(字体、图片等)打包,无需额外 loader
配置webpack.config.js 让 Webpack 拥有打包图片功能
- 占位符 【hash】对模块内容做算法计算,得到映射的数字字母组合的字符串
- 占位符【ext】使用当前模块原本的占位符,例如 :. png/.jpg等字符串
- 占位符【query】保留引入文件时代码中查询参数(只有URL下生效)
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
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
module.exports = { entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin() ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, "css-loader"] }, { test: /\.less$/i, use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ], }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
|
注意:
- 判断临界值默认为8kb
- 大于8kb 文件:生成一个单独的文件并导出URL地址
- 小于8kb 文件:导出一个data URI(base64字符串)
- js 中引入本地图片资源要用 import 方法(如果是网络图片http地址,可以直接将http地址赋值给src属性),否则无法对图片进行打包
搭建开发环境
开发环境:配置webpack-dev-server 快速开发应用程序
作用:启动 Web 服务,自动检测代码变化,热更新到网页
注意:dist 目录和打包内容是在内存里(更新快)
- 下载 webpack-dev-server 软件包到当前项目
1
| npm i webpack-dev-server --save-dev
|
- 设置模式为开发模式,并配置自定义命令
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
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
module.exports = { mode: 'development', entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin() ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, "css-loader"] }, { test: /\.less$/i, use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ], }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
|
1 2 3 4 5
| "scripts": { "build": "webpack", "dev": "webpack serve --open" },
|
- 使用 npm run dev 来启动开发服务器,实现热更新效果
注意1:webpack-dev-server 借助 http 模块创建 8080 默认 Web 服务
注意2:默认访问public下的index.html文件
注意3:webpack-dev-server 根据配置,打包相关代码在内存当中,以 output.path 的值作为服务器根目录(所以可以直接自己拼接访问 dist 目录下内容,此dist目录存在于内存中,并非项目中的dist目录)
打包模式
打包模式:告知 Webpack 使用相应模式的内置优化
- 分类:
| 模式名字 |
模式名字 |
特点 |
场景 |
| 开发模式 |
development |
调试代码,实时加载,模块热替换等 |
本地开发 |
| 生产模式 |
production |
压缩代码,资源优化,更轻量等 |
打包上线 |
- 设置
- 方式一:在 webpack.config.js 配置文件设置 mode 选项
1 2 3 4 5 6
|
module.exports = { mode: 'production' }
|
- 方式二:在 package.json 命令行设置 mode 参数
1 2 3 4
| "scripts": { "build": "webpack --mode=production", "dev": "webpack serve --mode=development" },
|
命令行设置优先级高于配置文件中的设置
打包模式的应用
根据不同的环境采用不同的打包模式
- 方案一:webpcak.config.js 配置导出函数,但是局限性大(只接受两种模式)
- 方案二:借助 cross-env(跨平台通用)包命令,设置参数区分环境
1
| npm i cross-env --save-dev
|
- 配置自定义命令,传入参数和值(会绑定到 process.env 对象下)
1 2 3 4
| "scripts": { "build": "cross-env NODE_ENV=production webpack --mode=production", "dev": "cross-env NODE_ENV=development webpack serve --mode=development" },
|
- 在 webpcak.config.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
| const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
module.exports = { mode: 'development', entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/index.html'), filename: path.resolve(__dirname, 'dist/login/index.html') }), new MiniCssExtractPlugin() ], module: { rules: [ { test: /\.css$/i, use: [process.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader"] }, { test: /\.less$/i, use: [process.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ], }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
|
- 方案三:配置不同 webpcak.config.js (适用多种模式差异性较大的情况)
webpack 官网:https://webpack.docschina.org/
前端 - 注入环境变量
问题:cross-env 设置的只在 Node.js 环境生效,前端代码无法访问 process.env.NODE_ENV
解决:适用 webpack 内置的 DefinePlugin 插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const webpack = require('source/_posts/frontend/Webpack')
module.exports = { plugins: [ new webpack.DefinePulgin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }) ] }
|
1 2 3 4 5
| if (process.env.NODE_ENV === 'production') { console.log = function () {} } console.log('生产环境不执行打印语句,开发环境执行')
|
开发环境调错 - source map
问题:代码被压缩和混淆,无法正确定位源代码位置(行数和列数)
source map:可以准确追踪 error 和 warning 在原始代码的位置
设置:webpack.config.js 配置 devtool选项
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 webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
config = { mode: 'development', entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/login.html'), filename: path.resolve(__dirname, 'dist/login/login.html') }), new MiniCssExtractPlugin({ filename: './login/index.css' }), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }) ], module: { rules: [ { test: /\.css$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader"] }, { test: /\.less$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ] }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], } }
if (process.env.NODE_ENV === 'development') { config.devtool = 'inline-source-map' }
module.exports = config
|
inline-source-map 选项:把源码的位置信息一起打包在js文件内
注意:source map 仅适用于开发环境,不要再生产环境使用(防止被轻易查看源码位置)
解析别名 alias
解析别名:配置模块如何解析,创建 import 引入路径别名,来确保模块引入变得更简单
在webpack.config.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
| const path = require('path') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
config = { mode: 'development', entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/login.html'), filename: path.resolve(__dirname, 'dist/login/login.html') }), new MiniCssExtractPlugin({ filename: './login/index.css' }), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }) ], module: { rules: [ { test: /\.css$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader"] }, { test: /\.less$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ] }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], }, resolve: { alias: { '@': path.resolve(__dirname, 'src') } } }
if (process.env.NODE_ENV === 'production') { config.devtool = 'inline-source-map' }
module.exports = config
|
1 2 3
| import youAxios from '@/utils/request' console.log(youAxios)
|
优化 - CDN使用
CDN 定义
CDN定义:内容分发网络,指的是一组分布在各个地区的服务器
作用:把静态资源文件/第三方库放在CDN网络中各个服务器中,供用户就近请求获取
好处:减轻自己服务器请求压力,就近请求物理延迟低,配套缓存策略
webpack 使用 CDN
为减小上线后前端代码的体积,可以在开发模式下使用本地第三方库,生产模式下使用 CDN 加载引入第三方库
- 在html文件中引入在cdn服务器上的第三方库,并用模板代码包裹
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
| <!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"> <% if (htmlWebpackPlugin.options.useCdn){ %> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.7/css/bootstrap.min.css" rel="stylesheet"> <% } %> <title>黑马头条-数据管理平台</title> </head>
<body> <div class="alert info-box"> 操作结果 </div> <div class="login-wrap"> <div class="title">黑马头条</div> <div> <form class="login-form"> <div class="item"> <input type="text" class="form-control" name="mobile" placeholder="请输入手机号" value="13888888888"> </div> <div class="item"> <input type="text" class="form-control" name="code" placeholder="默认验证码246810" value="246810"> </div> <div class="item"> <button type="button" class="btn btn-primary btn">登 录</button> </div> </form> </div> </div> <% if (htmlWebpackPlugin.options.useCdn){ %> <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.11.0/axios.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.7/js/bootstrap.min.js"></script> <% } %> </body>
</html>
|
免费的cdn服务器:https://www.bootcdn.cn/
免费的cdn服务器:https://unpkg.com/
第二个服务器需要在网址后面加第三方包名,按下enter即可自动跳转到对应的cdn地址
- 在webpack.config.js文件中配置useCdn属性
- 在webpack.config.js文件中配置 externals(外部扩展)
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
| const path = require('path') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
config = { mode: 'development', entry: path.resolve(__dirname, 'src/login/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: './login/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/login.html'), filename: path.resolve(__dirname, 'dist/login/login.html'), useCdn: process.env.NODE_ENV === 'production' }), new MiniCssExtractPlugin({ filename: './login/index.css' }), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }) ], module: { rules: [ { test: /\.css$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader"] }, { test: /\.less$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ] }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], }, resolve: { alias: { '@': path.resolve(__dirname, 'src') } } }
if (process.env.NODE_ENV === 'development') { config.devtool = 'inline-source-map' }
if (process.env.NODE_ENV === 'production') { config.externals = {
'axios': 'axios', "bootstrap/dist/css/bootstrap.min.css": 'bootstrap' } }
module.exports = config
|
打包多页面
查找使用 npm 下载第三方库的命令官网:https://www.npmjs.com/
在webpack.config.js中配置多入口和多页面的设置即可实现打包多个html文件(多页面)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| const config = { entry: { '模块名1':path.resolve(_dirname,'src/入口1.js'), '模块名2':path.resolve(_dirname,'src/入口2.js'), }, output: { path: path.resolve(_dirname, 'dist'), filename: './[name]/index.js' } plugins: [ new HtmlWebpackPlugin({ template:'./public/页面1.html', filename:'./路径/index.html', chunks : ['模块名1'] }), new HtmlWebpackPlugin({ template:'./public/页面2.html', filename:'./路径/index.html', chunks : ['模块名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
| const path = require('path') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
config = { mode: 'development', entry: { 'login': path.resolve(__dirname, 'src/login/index.js'), 'content': path.resolve(__dirname, 'src/content/index.js'), 'publish': path.resolve(__dirname, 'src/publish/index.js') }, output: { path: path.resolve(__dirname, 'dist'), filename: './[name]/index.js', clean: true }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/login.html'), filename: path.resolve(__dirname, 'dist/login/index.html'), useCdn: process.env.NODE_ENV === 'production', chunks: ['login'] }), new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/content.html'), filename: path.resolve(__dirname, 'dist/content/index.html'), useCdn: process.env.NODE_ENV === 'production', chunks: ['content'] }), new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'pubLic/publish.html'), filename: path.resolve(__dirname, 'dist/publish/index.html'), useCdn: process.env.NODE_ENV === 'production', chunks: ['publish'] }), new MiniCssExtractPlugin({ filename: './[name]/index.css' }), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }) ], module: { rules: [ { test: /\.css$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader"] }, { test: /\.less$/i, use: [process.env.NODE_ENV === 'production' ? MiniCssExtractPlugin.loader : 'style-loader', "css-loader", "less-loader"] }, { test: /\.(png|jpg|jpeg|gif)$/i, type: 'asset', generator: { filename: 'assets/[hash][ext][query]' } } ] }, optimization: { minimizer: [ `...`, new CssMinimizerPlugin(), ], }, resolve: { alias: { '@': path.resolve(__dirname, 'src') } } }
if (process.env.NODE_ENV === 'development') { config.devtool = 'inline-source-map' }
if (process.env.NODE_ENV === 'production') { config.externals = {
'axios': 'axios', "bootstrap/dist/css/bootstrap.min.css": 'bootstrap', 'form-serialize': 'serialize', '@wangeditor/editor': 'WangEditor' } }
module.exports = config
|
优化 - 分割公共代码
配置webpack.config.js的splitChunks 分隔功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const config = { optimization: { splitChunks: { chunks:'all', cacheGroups:{ commons:{ minSize:0, minChunks:2, reuseExistingChunk: true, name(module, chunks, cacheGroupKey){ const allChunksNames =chunks.map((item)=> item.name).join('~') return`./js/${allChunksNames}` } } } } } }
|