1. clean-webpack-plugin 清除打包的输出目录
packge.json
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
}
webpack.config.js
var { CleanWebpackPlugin } = require("clean-webpack-plugin")
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "[name].[chunkhash:5].js"
},
plugins: [
new CleanWebpackPlugin()
]
}
2. html-webpack-plugin 自动生成页面
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
}
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
devtool: "source-map",
entry: {
home: "./src/index.js",
a: "./src/a.js"
},
output: {
filename: "scripts/[name].[chunkhash:5].js"
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "home.html",
chunks: ["home"]
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "a.html",
chunks: ["a"]
})
]
}
3. copy-webpack-plugin 复制静态资源
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^5.1.1",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
}
}
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "scripts/[name].[chunkhash:5].js"
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html"
}),
new CopyPlugin([
{ from: "./public", to: "./" }
])
]
}
4. 开发服务器
在开发阶段,目前遇到的问题是打包、运行、调试过程过于繁琐,回顾一下我们的操作流程:
- 编写代码
- 控制台运行命令完成打包
- 打开页面查看效果
- 继续编写代码,回到步骤2
并且,我们往往希望把最终生成的代码和页面部署到服务器上,来模拟真实环境
为了解决这些问题,webpack官方制作了一个单独的库:webpack-dev-server
它既不是plugin也不是loader
先来看看它怎么用
- 安装
- 执行
webpack-dev-server命令
webpack-dev-server命令几乎支持所有的webpack命令参数,如--config、-env等等,你可以把它当作webpack命令使用
这个命令是专门为开发阶段服务的,真正部署的时候还是得使用webpack命令
当我们执行webpack-dev-server命令后,它做了以下操作:
- 内部执行webpack命令,传递命令参数
- 开启watch
- 注册hooks:类似于plugin,webpack-dev-server会向webpack中注册一些钩子函数,主要功能如下:
- 将资源列表(aseets)保存起来
- 禁止webpack输出文件
- 用express开启一个服务器,监听某个端口,当请求到达后,根据请求的路径,给予相应的资源内容
配置
针对webpack-dev-server的配置,参考:www.webpackjs.com/configurati…
常见配置有:
- port:配置监听端口
- proxy:配置代理,常用于跨域访问
- stats:配置控制台输出内容
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
}
}
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "scripts/[name].[chunkhash:5].js"
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html"
})
],
devServer: {
port: 8000,
open: true,
proxy: { //代理规则
"/api": {
target: "http://open.duyiedu.com",
changeOrigin: true //更改请求头中的host和origin
}
}
},
stats: {
modules: false,
colors: true
}
}
5. file-loader、url-loader 对普通文件的处理
file-loader: 生成依赖的文件到输出目录,然后将模块文件设置为:导出一个路径
//file-loader
function loader(source){
// source:文件内容(图片内容 buffer)
// 1. 生成一个具有相同文件内容的文件到输出目录
// 2. 返回一段代码 export default "文件名"
}
url-loader:将依赖的文件转换为:导出一个base64格式的字符串
//file-loader
function loader(source){
// source:文件内容(图片内容 buffer)
// 1. 根据buffer生成一个base64编码
// 2. 返回一段代码 export default "base64编码"
}
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"file-loader": "^5.0.2",
"html-webpack-plugin": "^3.2.0",
"url-loader": "^3.0.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
},
"description": ""
}
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "scripts/[name].[chunkhash:5].js"
},
module: {
rules: [
{
test: /\.(png)|(gif)|(jpg)$/,
use: [{
loader: "url-loader",
options: {
// limit: false //不限制任何大小,所有经过loader的文件进行base64编码返回
limit: 10 * 1024, //只要文件不超过 100*1024 字节,则使用base64编码,否则,交给file-loader进行处理
name: "imgs/[name].[hash:5].[ext]"
}
}]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html"
})
],
devServer: {
open: true
},
stats: {
modules: false,
colors: true
}
}
6.解决路径问题
在使用file-loader或url-loader时,可能会遇到一个非常有趣的问题
比如,通过webpack打包的目录结构如下:
dist
|—— img
|—— a.png #file-loader生成的文件
|—— scripts
|—— main.js #export default "img/a.png"
|—— html
|—— index.html #<script src="../scripts/main.js" ></script>
这种问题发生的根本原因:模块中的路径来自于某个loader或plugin,当产生路径时,loader或plugin只有相对于dist目录的路径,并不知道该路径将在哪个资源中使用,从而无法确定最终正确的路径
面对这种情况,需要依靠webpack的配置publicPath解决
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"file-loader": "^5.0.2",
"html-webpack-plugin": "^3.2.0",
"url-loader": "^3.0.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
},
"description": ""
}
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "scripts/[name].[chunkhash:5].js",
publicPath: "/"
},
module: {
rules: [
{
test: /\.(png)|(gif)|(jpg)$/,
use: [{
loader: "file-loader",
options: {
name: "imgs/[name].[hash:5].[ext]"
}
}]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "html/index.html"
})
],
devServer: {
open: true,
openPage: "html/index.html",
},
stats: {
modules: false,
colors: true
}
}
7.webpack内置插件
所有的webpack内置插件都作为webpack的静态属性存在的,使用下面的方式即可创建一个插件对象
const webpack = require("webpack")
new webpack.插件名(options)
DefinePlugin
全局常量定义插件,使用该插件通常定义一些常量值,例如:
new webpack.DefinePlugin({
PI: `Math.PI`, // PI = Math.PI
VERSION: `"1.0.0"`, // VERSION = "1.0.0"
DOMAIN: JSON.stringify("duyi.com")
})
这样一来,在源码中,我们可以直接使用插件中提供的常量,当webpack编译完成后,会自动替换为常量的值
BannerPlugin
它可以为每个chunk生成的文件头部添加一行注释,一般用于添加作者、公司、版权等信息
new webpack.BannerPlugin({
banner: `
hash:[hash]
chunkhash:[chunkhash]
name:[name]
author:yuanjin
corporation:duyi
`
})
ProvidePlugin
自动加载模块,而不必到处 import 或 require
new webpack.ProvidePlugin({
$: 'jquery',
_: 'lodash'
})
然后在我们任意源码中:
$('#item'); // <= 起作用
_.drop([1, 2, 3], 2); // <= 起作用
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
},
"description": "",
"dependencies": {
"jquery": "^3.4.1",
"lodash": "^4.17.15"
}
}
const webpack = require("webpack")
module.exports = {
mode: "development",
devtool: "source-map",
plugins: [
new webpack.DefinePlugin({
PI: `Math.PI`, // const PI = Math.PI
VERSION: `"1.0.0"`, // VERSION = "1.0.0"
DOMAIN: JSON.stringify("duyi.com") // DOMAIN = "duyi.com"
}),
new webpack.BannerPlugin({
banner: `
hash:[hash]
chunkhash:[chunkhash]
name:[name]
author:yuanjin
corporation:duyi
`
}),
new webpack.ProvidePlugin({
$: 'jquery',
_: 'lodash'
})
]
}
index.js
console.log(PI);
console.log(VERSION);
console.log(DOMAIN);
var r1 = $('#item'); // <= 起作用
var r2 = _.drop([1, 2, 3], 2); // <= 起作用
console.log(r1, r2);