-
Notifications
You must be signed in to change notification settings - Fork 34
/
webpack.config.js
193 lines (172 loc) · 4.68 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
183
184
185
186
187
188
189
190
191
192
193
/**
* @config webpack.config
* @author Wayne
* @update 2022-01-16
*/
const glob = require('glob');
const { resolve, join } = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LessFunc = require('less-plugin-functions');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CONFIG = require('./package.json');
// top banner
const MY_BANNER = `
${CONFIG.name}
@version: ${CONFIG.version}
@description: ${CONFIG.description}
@author: ${CONFIG.author}
@task: ${CONFIG.task || ''}
@build time: ${new Date()}
`;
const IMAGE_LIMIT = 5000; // base64 limit
// document https://webpack.js.org/concepts/
module.exports = (options = {}) => {
const entries = glob.sync('./src/**/enter.js');
const entryJSList = {};
const entryHtmlList = [];
for (const _path of entries) {
const chunkName = _path.slice('./src/js/'.length, -'/enter.js'.length);
entryJSList[chunkName] = _path;
entryHtmlList.push(
new HtmlWebpackPlugin({
template: './src/' + chunkName + '.html',
filename: chunkName + '.html',
chunks: ['manifest', 'vendor', chunkName, 'commons'],
minify: {
removeAttributeQuotes: false,
},
})
);
}
const plugins = [
...entryHtmlList,
new MiniCssExtractPlugin({
filename: `css/[name]${options.dev ? '' : '.[chunkhash:8]'}.css`,
chunkFilename: `[id].css`,
}),
new webpack.optimize.SplitChunksPlugin({
names: 'commons',
}),
];
if (!options.dev) plugins.push(new webpack.IgnorePlugin(/mock\/*/)); // ignore mock
if (options.prod) {
// add banner description
plugins.push(new webpack.BannerPlugin(MY_BANNER));
}
return {
stats: options.dev ? 'minimal' : 'normal',
entry: entryJSList,
resolve: {
extensions: ['.js', '.css', 'less'],
alias: {
'@': resolve(__dirname, 'src'),
'~': resolve(__dirname, 'src'),
mock: resolve(__dirname, 'src/mock'),
lib: resolve(__dirname, 'src/js/lib'),
css: resolve(__dirname, 'src/css'),
less: resolve(__dirname, 'src/less'),
},
},
output: {
path: resolve(__dirname, 'dist'),
filename: options.dev
? '[name].js'
: //'js/[name].js',
'js/[name].[chunkhash:8].js',
chunkFilename: '[id].js?[chunkhash:8]',
},
devtool: (options.dev && 'cheap-module-source-map') || (options.prod ? false : 'source-map'),
module: {
rules: [
// js
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['babel-preset-env'],
},
},
'eslint-loader',
],
},
// html
{
test: /\.html$/,
use: {
loader: 'html-loader',
options: {
root: resolve(__dirname, 'src'),
attrs: ['img:src'],
},
},
},
// css
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [require('autoprefixer')()],
},
},
],
},
// less
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [require('autoprefixer')()],
},
},
{
loader: 'less-loader',
options: {
strictMath: true,
plugins: [new LessFunc()],
},
},
],
},
// image or font
{
test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: IMAGE_LIMIT,
name: 'images/[hash].[ext]',
},
},
],
},
],
},
plugins: plugins,
devServer: {
port: 3000,
hot: true,
contentBase: join(__dirname, 'src'),
overlay: true,
historyApiFallback: {
index: '/assets/',
disableDotRule: true,
},
inline: true,
},
performance: {
hints: options.dev ? false : 'warning',
},
};
};