完整配置
howcode 2022-07-19 0 webpack
# 开发环境
https://www.bilibili.com/video/BV1e7411j7T5?p=18
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CommonCssLoader = [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { importLoaders: 1 },
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [["postcss-preset-env"]],
},
},
},
];
process.env.NODE_ENV = "development";
module.exports = {
entry: "./assets/js/main.js",
output: {
filename: "js/app.js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{
oneOf: [
{
// 处理 css 资源
test: /\.css$/i,
use: [...CommonCssLoader],
},
{
// 处理 scss 资源
test: /\.s[ac]ss$/i,
use: [...CommonCssLoader, "sass-loader"],
},
{
// 处理图片资源
test: /\.(jpg|jpeg|png|gif)$/i,
loader: "url-loader",
options: {
limit: 8 * 1024,
name: "[hash:12].[ext]",
esModule: false,
outputPath: "images",
},
},
{
// 处理 html 中的图片资源
test: /\.html$/i,
loader: "html-loader",
},
],
},
],
},
plugins: [
// 处理 html 资源
new HtmlWebpackPlugin({
template: "./index.html",
minify: {
collapseWhitespace: true,
removeComments: true,
},
}),
new MiniCssExtractPlugin({
// 对输出结果重命名
filename: "css/app.css",
}),
],
mode: "development",
devServer: {
static: {
directory: path.join(__dirname, "build"),
},
compress: true,
port: 3000,
liveReload: true,
watchFiles: {
paths: ["./assets/*/*", "./*.html"],
options: {
usePolling: false,
},
},
open: true,
hot: true,
},
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
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
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
# 生产环境
// 生产环境
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CommonCssLoader = [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { importLoaders: 1 },
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [["postcss-preset-env"]],
},
},
},
];
// process.env.NODE_ENV = 'development'
module.exports = {
entry: "./assets/js/main.js",
output: {
filename: "js/app.js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{
// 处理 css 资源
test: /\.css$/i,
use: [...CommonCssLoader],
},
{
// 处理 scss 资源
test: /\.s[ac]ss$/i,
use: [...CommonCssLoader, "sass-loader"],
},
{
// 处理图片资源
test: /\.(jpg|jpeg|png|gif)$/i,
loader: "url-loader",
options: {
limit: 8 * 1024,
name: "[hash:12].[ext]",
esModule: false,
outputPath: "images",
},
},
{
// 处理 html 中的图片资源
test: /\.html$/i,
loader: "html-loader",
},
],
},
plugins: [
// 处理 html 资源
new HtmlWebpackPlugin({
template: "./index.html",
minify: {
collapseWhitespace: true,
removeComments: true,
},
}),
new MiniCssExtractPlugin({
// 对输出结果重命名
filename: "css/app.css",
}),
],
mode: "production",
};
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
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
评论
- 表情
——暂无评论——