-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebpack.config.js
182 lines (182 loc) · 5.98 KB
/
webpack.config.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const glob = require('glob'); // 处理文件路径用到,很有用
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 将打包后js自动引入html文件插件
const {CleanWebpackPlugin} = require('clean-webpack-plugin'); // 清除打包文件工具
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // 分离css
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); // css压缩去重
const CopyWebpackPlugin = require('copy-webpack-plugin');
const cleanPath = [path.join(__dirname, './dist')];
const pagePath = require('./pagePath');
// 多页面配置函数
const pagesSetting = () => {
const entries = {};
const htmlWebpackPlugins = [];
const files = glob.sync(path.join(__dirname, './src/pages/**/*.js'));
files.forEach((file) => {
const match = file.match(/\/pages\/(.*)\/(.*).js$/);
const entry = match && match[1]; // 文件夹
// 保存入口文件
entries[entry] = file;
const pageFiles = fs.readdirSync(path.resolve(file, '..'));
const htmlFile = pageFiles.filter((file) => /\.html$/.test(file));
if (htmlFile.length > 0) {
htmlWebpackPlugins.push(new HtmlWebpackPlugin({
template: path.join(__dirname, `./src/pages/${entry}/${htmlFile[0]}`),
filename: `pages/${entry}/${htmlFile[0]}`,
chunks: [entry],
inject: true, // 将js放在body底部
minify:{
collapseWhitespace: true, // 折叠标签空白
minifyCSS: true,
minifyJS: true
}
}));
} else {
console.log(chalk.red(`${path.resolve(file, '..')}目录下无模板文件`))
}
});
return {
entries,
htmlWebpackPlugins
}
};
const {entries, htmlWebpackPlugins} = pagesSetting();
module.exports = {
mode: 'development',
entry: entries,
output: {
path: path.resolve(__dirname, './dist'),
filename: "pages/[name]/[name].[chunkhash:8].js",
},
module: {
rules: [
// {
// test: /\.js$/,
// exclude: /(node_modules|bower_components)/,
// use: {
// loader: 'babel-loader',
// options: {
// presets: ['@babel/preset-env']
// }
// }
// },
{
test: /\.css$/,
use:[
MiniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: "postcss-loader"}
]
},
{
test: /\.less$/,
use:[
MiniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: "postcss-loader"},
{loader: 'less-loader'}
]
},
{
test: /\.sass$/,
use: [
MiniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: "postcss-loader"},
{loader: 'sass-loader'}
]
},
{
test: /\.styl$/,
use: [
MiniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: "postcss-loader"},
{loader: 'stylus-loader'}
]
},
{
test: /\.(bmp|png|jpg|jpeg|ico|gif)$/,
use: [
{
loader: 'url-loader',
options:{
limit: 1024 * 12, // 文件小于12kb,输出DataUrl
outputPath: 'assets/images', // 该路径相对于html输出路径
publicPath: '../../assets/images',
name: '[name].[ext]'
}
}
]
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
attrs: ['img:src', 'img:data-src', 'audio:src'],
minimize: false,
}
}
]
},{
test:/\.(woff2?|woff|svg|eot|ttf)(\?.*)?$/,
use: [
{
loader: "url-loader",
options: {
name:"[name].[ext]",
limit: 1024 * 5,
outputPath:'assets/fonts/'
}
}
]
}
]
},
plugins: [
...htmlWebpackPlugins,
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: cleanPath
}),
new MiniCssExtractPlugin({
filename: 'pages/[name]/[name].[chunkhash:8].css',
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: [
'default',
{ discardComments: { removeAll: true } }
],
},
canPrint: true
}),
new CopyWebpackPlugin([
{
from: 'src/assets/fonts',
to: 'assets/fonts'
},
{
from: 'src/static',
to: 'static'
}
])
],
node: {
fs: 'empty'
},
devServer: {
host: "localhost",
open: true,
port: 8081,
compress: true,
historyApiFallback:{
rewrites: pagePath
}
}
};