-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.2.js
168 lines (162 loc) · 5.71 KB
/
webpack.prod.2.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const glob = require('glob');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// 此脚本用来支持动态多页面打包
const setMPA = () => {
const entry = {};
const HtmlWebpackPlugins = [];
const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
// console.log('entryFiles', entryFiles);
// 以下为打印结果 需要运行此函数 setMPA();
// entryFiles [ 'C:/Users/Ge_yang/Desktop/webpack-demo/src/index/index.js',
// 'C:/Users/Ge_yang/Desktop/webpack-demo/src/search/index.js' ]
Object.keys(entryFiles)
.map((index) => {
const entryFile = entryFiles[index];
const match = entryFile.match(/src\/(.*)\/index\.js/);
// console.log('match', match);
// 以下为打印结果 需要运行此函数 setMPA();
// match [ 'src/index/index.js',
// 'index',
// index: 38,
// input: 'C:/Users/Ge_yang/Desktop/webpack-demo/src/index/index.js',
// groups: undefined ]
// ====================================
// match [ 'src/search/index.js',
// 'search',
// index: 38,
// input: 'C:/Users/Ge_yang/Desktop/webpack-demo/src/search/index.js',
// groups: undefined ]
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 } = setMPA();
module.exports = {
// 多入口文件配置
entry: entry,
output: {
path: path.join(__dirname, 'dist'),
// chunkhash 为js文件指纹配置,配置长度为8位
filename: '[name]_[chunkhash:8].js'
},
mode: 'production',
module: {
rules: [
// 配置js
{
test: /.js$/,
use: 'babel-loader'
},
// 配置css
{
test: /.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
// 配置less转css
{
test: /.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'less-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')({
overrideBrowserslist: ['last 2 version', '>1%', 'ios 7']
// browsers: ['last 2 version', '>1%', 'ios 7']
})
]
}
},
{
loader: 'px2rem-loader',
options: {
remUnit: 75,
remPrecision: 8
}
}
]
},
// 配置图片
{
test: /.(png|jpg|gif|jpeg)$/,
use: [
{
loader: 'file-loader',
options: {
// hash为图片/文字文件指纹配置,配置长度为8位
name: '[name]_[hash:8].[ext]'
}
}
]
},
// 使用url可以对小图片进行base64转换,优于file-loader
// {
// test: /.(png|jpg|gif|jpeg)$/,
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10240
// }
// }
// ]
// },
// 配置文字
{
test: /.(woff|woff2|eot|ttf|otf)$/,
use: [
{
loader: 'file-loader',
options: {
// hash为图片/文字文件指纹配置,配置长度为8位
name: '[name]_[hash:8].[ext]'
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]_[contenthash:8].css'
}),
// 配置 --> css代码压缩 附:须安装css处理器 npm i cssnano -D
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano')
}),
// 每次 npm run build 后,解决需手动删除dist目录的问题
new CleanWebpackPlugin()
].concat(HtmlWebpackPlugins)
};