# 三.watch 的用法
# 1.安装
package.json
{
"name": "3.watch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2.文件
# 1.src/index.js
console.log("index")
1
# 2.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>webpack配置watch</title>
</head>
<body></body>
</html>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 3.配置
webpack.config.js
const path = require("path")
let HtmlWebpackPlugin = require("html-webpack-plugin")
module.exports = {
mode: "production",
entry: {
home: "./src/index.js",
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"),
},
watch: true,
//监控选项
watchOptions: {
poll: 1000, //每秒 问我 1000次
aggregateTimeout: 500, //防抖 我一直输入代码
ignored: /node_modules/, //忽略监控的文件夹
},
plugins: [
new HtmlWebpackPlugin({
template: "./index.html",
filename: "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
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
# 4.运行
npm run dev
1
修改 js 文件,查看控制台发现页面会自动刷新
← 二.sourceMap 四.小插件应用 →