-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
237 lines (213 loc) · 6.82 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlBeautifyPlugin = require('@nurminen/html-beautify-webpack-plugin');
const HtmlWebpackSkipAssetsPlugin = require('html-webpack-skip-assets-plugin').HtmlWebpackSkipAssetsPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const TerserPlugin = require("terser-webpack-plugin");
const isDev = process.env.WEBPACK_SERVE;
function entryPoints(dir) {
let entry_points = {
style: './src/scss/style.scss'
};
function each_file(dir) {
try {
fs.readdirSync(dir, {withFileTypes: true}).forEach(function(item) {
const file = item.name;
const parts = file.split('.');
if (parts[1] !== 'js') return;
var file_path = dir + '/' + file;
entry_points[parts[0]] = file_path;
});
} catch (e) {
console.log(e);
}
}
each_file(dir);
return entry_points;
}
function generateHtmlPlugins(templateDir) {
const templateFiles = fs.readdirSync(path.resolve(__dirname, templateDir), {withFileTypes: true});
return templateFiles.reduce((result, item) => {
const parts = item.name.split('.');
const name = parts[0];
const extension = parts[1];
if (extension === 'html') result.push(
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, `./theme/html/${name}.html`),
template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`),
chunks: ['style', 'vendors', 'global', name],
chunksSortMode: 'manual',
excludeAssets: 'style.js'
})
);
return result;
}, []);
}
function plugins() {
let plugins = [...generateHtmlPlugins('./src/pages/')];
if (isDev) {
plugins.push(new webpack.HotModuleReplacementPlugin());
} else {
plugins = [
...plugins,
new HtmlBeautifyPlugin({
config: {
html: {
end_with_newline: true,
indent_size: 4,
indent_with_tabs: true,
indent_inner_html: true,
preserve_newlines: true,
unformatted: ['strong', 'i', 'b'],
inline: []
}
}
}),
new HtmlWebpackSkipAssetsPlugin({
excludeAssets: ['style.js']
}),
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: ({chunk}) => chunk.name === 'style' ? '[name].css' : 'css/[name].css'
})
]
}
return plugins;
}
function processNestedHtml(content, loaderContext) {
const INCLUDE_PATTERN = /\<include src=\"(.+)\"\/?\>(?:\<\/include\>)?/gi;
return !INCLUDE_PATTERN.test(content)
? content
: content.replace(INCLUDE_PATTERN, (m, src) => {
const pathToPartial = path.resolve(loaderContext.context, src);
loaderContext.addDependency(pathToPartial);
return processNestedHtml(fs.readFileSync(pathToPartial, 'utf8'), {...loaderContext, context: path.dirname(pathToPartial)});
});
}
function styleLoaders() {
if (isDev) {
return [
"style-loader",
"css-loader",
"resolve-url-loader",
{
loader: "sass-loader",
options: {
sourceMap: true, // <-- !!IMPORTANT!! for "resolve-url-loader"
}
}
]
}
return [
MiniCssExtractPlugin.loader,
"css-loader",
"postcss-loader",
"resolve-url-loader",
{
loader: "sass-loader",
options: {
sourceMap: true, // <-- !!IMPORTANT!! for "resolve-url-loader"
}
}
]
}
module.exports = {
entry: entryPoints('./src/js'),
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, './theme'),
assetModuleFilename: 'images/[name][ext]',
clean: isDev ? false : {
keep(asset) {
const targets = ['fonts/', 'html/', 'css/', 'images/', 'js/', '.css'];
return !targets.some(v => asset.includes(v));
}
}
},
plugins: plugins(),
externals: {
jquery: 'jQuery',
},
module: {
rules: [
{
test: /\.html$/i,
use: {
loader: 'html-loader',
options: {
minimize: false,
preprocessor: processNestedHtml
}
}
},
{
test: /\.s[ac]ss|\.css/i,
use: styleLoaders()
},
{
test: /\.js$/i,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.(png|jpg|gif)$/i,
type: 'asset/resource',
use: isDev ? void(0) : 'image-webpack-loader'
},
{
test: /\.svg$/i,
type: isDev ? 'asset' : 'asset/resource',
generator: {
filename: isDev ? 'images/[name][hash][ext]' : 'images/[name][ext]'
}
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]'
}
},
],
},
devServer: {
client: {
overlay: true,
},
static: {
directory: path.join(__dirname, './theme/html'),
},
watchFiles: ['src/pages/**/*'],
hot: true,
host: '0.0.0.0',
port: 8080
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks (chunk) {
// exclude `style`
return chunk.name !== 'style';
}
}
}
},
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin({ extractComments: false })
],
runtimeChunk: 'single'
}
};