-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
137 lines (126 loc) · 3.33 KB
/
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
'use strict';
const glob = require('glob');
const path = require('path');
const MiniCssExtractplugin = require('mini-css-extract-plugin'); // 提取css单独一个文件
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// ! 设置多页面打包
const setMAP = () => {
const entry = {};
const HtmlWebpackPlugins = [];
const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
Object.keys(entryFiles).map((item) => {
const entryFile = entryFiles[item];
// 获取文件夹的名称
const match = entryFile.match(/src\/(.*)\/index\.js/);
const pageName = match && match[1];
entry[pageName] = entryFile;
HtmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `src/${pageName}/index.html`),
filename: `${pageName}.html`,
chunks: [pageName],
inject: true,
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
minifyJS: true,
removeComments: false,
},
}),
);
});
return {
entry,
HtmlWebpackPlugins,
};
};
const { entry, HtmlWebpackPlugins } = setMAP();
module.exports = {
entry,
output: {
path: path.join(__dirname, 'dist'),
filename: '[name]_[chunkhash:8].js', // chunkhash 8位的长度
},
mode: 'production',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader',
},
{
test: /.css$/, // 配置css的后缀名
use: [MiniCssExtractplugin.loader, 'css-loader'], //tips:执行的顺序是右到左的
},
{
test: /.less$/, // 配置less的后缀名
use: [
MiniCssExtractplugin.loader,
'css-loader',
'less-loader',
{
loader: 'postcss-loader',
options: {
plugins: [
require('autoprefixer')({
browsers: [
//浏览器列表
'ie>=8',
'Firefox>=20',
'Safari>=5',
'Android>=4',
'Ios>=6',
'last 4 version',
],
}),
],
},
},
{
loader: 'px2rem-loader',
options: {
remUnit: 75,
remPrecision: 8,
},
},
], //tips:执行的顺序是右到左的
},
{
test: /.(png|jpg|gif|jpeg)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
},
},
],
},
{
test: /.(woff|woff2|eot|ttf|otf)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
},
},
],
},
],
},
plugins: [
new MiniCssExtractplugin({
filename: '[name]_[contenthash:8].css',
}),
new OptimizeCssAssetsWebpackPlugin({
assetNameRegExp: /.css$/g,
cssProcessor: require('cssnano'),
}),
new CleanWebpackPlugin(),
].concat(HtmlWebpackPlugins),
};