-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathwebpack.js
More file actions
executable file
·428 lines (370 loc) · 12.9 KB
/
webpack.js
File metadata and controls
executable file
·428 lines (370 loc) · 12.9 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
let fs = require('fs');
let config = require('config');
let path = require('path');
// 3rd party / slow to build modules
// no webpack dependencies inside
// no es6 (for 6to5 processing) inside
// NB: includes angular-*
let noProcessModulesRegExp = new RegExp("node_modules" + (path.sep === '/' ? path.sep : '\\\\') + "(angular|prismjs|sanitize-html|i18n-iso-countries)");
let devMode = process.env.NODE_ENV == 'development';
module.exports = function () {
let nib = require('nib');
let rupture = require('rupture');
let chokidar = require('chokidar');
let webpack = require('webpack');
let WriteVersionsPlugin = require('engine/webpack/writeVersionsPlugin');
let CssWatchRebuildPlugin = require('engine/webpack/cssWatchRebuildPlugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const fse = require('fs-extra');
// tutorial.js?hash
// tutorial.hash.js
function extHash(name, ext, hash) {
if (!hash) hash = '[hash]';
return config.assetVersioning == 'query' ? `${name}.${ext}?${hash}` :
config.assetVersioning == 'file' ? `${name}.${hash}.${ext}` :
`${name}.${ext}`;
}
let modulesDirectories = [path.join(process.cwd(), 'node_modules')];
if (process.env.NODE_PATH) {
modulesDirectories = modulesDirectories.concat(process.env.NODE_PATH.split(/[:;]/).map(p => path.resolve(p)));
}
//console.log("MODULE DIRS", modulesDirectories);
/**
* handler/client/assets/* goes to public/assets/
*/
let assetPaths = [config.assetsRoot];
for (let handlerName in config.handlers) {
let handlerPath = config.handlers[handlerName].path;
let from = `${handlerPath}/client/assets`;
if (fse.existsSync(from)) {
assetPaths.push(from);
}
}
//console.log("ASSET PATHS", assetPaths);
/**
* handler/client becomes handler.js
* handler/templates makes handler.css (via CssWatchRebuiltPlugin)
*/
let entries = {
head: 'client/head',
footer: 'client/footer',
tutorial: 'engine/koa/tutorial/client',
styles: config.tmpRoot + '/styles.styl',
frontpage: config.tmpRoot + '/frontpage.styl'
};
/*
for(let handlerName in config.handlers) {
let handlerNameShort = path.basename(handlerName);
let handlerPath = config.handlers[handlerName].path;
let e = [];
if (fse.existsSync(`${handlerPath}/client`)) {
e.push(`${handlerPath}/client`);
}
if (fse.existsSync(`${handlerPath}/templates`)) {
e.push(config.tmpRoot + `/${handlerNameShort}.styl`);
}
if (e.length) {
entries[handlerNameShort] = e;
}
}*/
//console.log("WEBPACK ENTRIES", entries);
let webpackConfig = {
output: {
devtoolNamespace: 'wp',
// fs path
path: path.join(config.publicRoot, 'pack'),
// path as js sees it
// if I use another domain here, need enable Allow-Access-.. header there
// and add to scripts, to let error handler track errors
publicPath: '/pack/',
// в dev-режиме файлы будут вида [name].js, но обращения - через [name].js?[hash], т.е. версия учтена
// в prod-режиме не можем ?, т.к. CDN его обрезают, поэтому [hash] в имени
// (какой-то [hash] здесь необходим, иначе к chunk'ам типа 3.js, которые генерируются require.ensure,
// будет обращение без хэша при загрузке внутри сборки. при изменении - барузерный кеш их не подхватит)
filename: extHash("[name]", 'js'),
chunkFilename: extHash("[name]-[id]", 'js'),
library: '[name]',
pathinfo: devMode
},
cache: devMode,
mode: devMode ? 'development' : 'production', // for tests uses prod too
watchOptions: {
aggregateTimeout: 10,
ignored: /node_modules/
},
watch: devMode,
devtool: devMode
? 'inline-cheap-module-source-map' // try "eval" ?
: process.env.NODE_ENV === 'production'
? 'source-map'
: false,
profile: Boolean(process.env.WEBPACK_STATS),
entry: entries,
/*
entry: {
styles: config.tmpRoot + '/styles.styl',
head: 'client/head',
tutorial: 'engine/koa/tutorial/client',
footer: 'client/footer',
},*/
module: {
rules: [
{
test: /\.yml$/,
use: 'yaml-loader'
},
{
// make t global, so that it will not be defined in the compiled template function
// and i18n plugin will substitute
test: /\.pug/,
use: 'pug-loader?root=' + config.projectRoot + '/templates&globals=__'
},
{
test: /\.js$/,
// babel shouldn't process modules which contain ws/browser.js,
// which must not be run in strict mode (global becomes undefined)
// babel would make all modules strict!
exclude(path) {
return noProcessModulesRegExp.test(path) || path.includes('node_modules/monaco-editor') || devMode;
},
use: [
// babel will work first
{
loader: 'babel-loader',
options: {
plugins: [
require.resolve('@babel/plugin-proposal-object-rest-spread')
],
presets: [
[require.resolve('@babel/preset-env'), {
//useBuiltIns: true,
targets: {
// not ie11, don't want regenerator-runtime and @babel/plugin-transform-runtime
browsers: '> 3%'
}
}]
]
}
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
],
},
{
test: /\.styl$/,
// MiniCssExtractPlugin breaks HMR for CSS
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('autoprefixer')
]
}
}
},
'engine/webpack/hover-loader',
{
loader: 'stylus-loader',
options: {
stylusOptions: {
linenos: true,
'resolve url': true,
use: [
rupture(),
nib(),
function(style) {
style.define('lang', config.lang);
style.define('isRTL', ['ar','fa','he'].includes(process.env.TUTORIAL_LANG));
style.define('env', config.env);
}
]
}
},
}
]
},
{
test: /\.(png|jpg|gif|woff|woff2|eot|otf|ttf|svg)$/,
type: 'asset/resource',
generator: {
filename: extHash('[name]','[ext]'), // Keeps the original file name and extension
},
}
],
noParse(path) {
/*
if (path.indexOf('!') != -1) {
path = path.slice(path.lastIndexOf('!') + 1);
}
*/
//console.log(path);
return noProcessModulesRegExp.test(path);
}
},
resolve: {
// allow require('styles') which looks for styles/index.styl
extensions: ['.js', '.styl'],
alias: {
'entities/maps/entities.json': 'engine/markit/emptyEntities',
'entities/lib/maps/entities.json': 'engine/markit/emptyEntities',
config: 'client/config',
},
fallback: {
fs: false, // Replace 'empty' with 'false' in Webpack 5 to exclude it
},
modules: modulesDirectories,
mainFields: ['browser', 'main', 'module'] // maybe not needed, from eslint webpack
},
resolveLoader: {
modules: modulesDirectories,
extensions: ['.js']
},
performance: {
maxEntrypointSize: 350000,
maxAssetSize: 350000, // warning if asset is bigger than 300k
assetFilter(assetFilename) {
// only check js/css
// ignore assets copied by CopyWebpackPlugin
if (assetFilename.startsWith('..')) {
// they look like ../courses/achievements/course-complete.svg
// built assets do not have ..
return false;
}
return assetFilename.endsWith('.js') || assetFilename.endsWith('.css');
},
},
plugins: [
new webpack.DefinePlugin({
LANG: JSON.stringify(config.lang),
IS_CLIENT: true
}),
// lodash is loaded when free variable _ occurs in the code
new webpack.ProvidePlugin({
_: 'lodash'
}),
new webpack.IgnorePlugin({
resourceRegExp: /fs-extra$/
}),
// ignore all moment.js locales except current lang
new webpack.IgnorePlugin({
checkResource(resource, context) {
// locale requires that file back from it, need to keep it
if (resource === '../moment') return false; // don't ignore this
if (resource.startsWith('./' + config.lang)) return false; // don't ignore current locale ./ru ./ru.js ./zh ./zh-cn.js
let ignore = context.endsWith(path.join('node_modules', 'moment', 'locale'));
if (ignore) {
// console.log("Ignore", resource, context);
return true;
}
return false;
},
}),
// ignore site locale files except the lang
new webpack.IgnorePlugin({
checkResource(resource, context) {
let isYmlForAnotherLanguage = resource.endsWith('.yml') && !resource.endsWith('/' + config.lang + '.yml');
let isUnderLocales = /\/locales(\/|$)/.test(context);
if (isYmlForAnotherLanguage && isUnderLocales) return true;
// console.log("checkResource", resource, context);
return false;
}
}),
new WriteVersionsPlugin(path.join(config.buildRoot, 'webpack', 'versions', 'all.json')),
new MiniCssExtractPlugin({
filename: extHash("[name]", 'css'),
chunkFilename: extHash("[id]", 'css'),
}),
new CssWatchRebuildPlugin(),
new CopyWebpackPlugin({
patterns: assetPaths.map((path) => {
return {
from: path,
to: config.publicRoot,
info: (file) => ({ minimized: true }),
noErrorOnMissing: true
};
})
}),
{
apply(compiler) {
if (process.env.WEBPACK_STATS) {
compiler.hooks.done.tap('MyStats', (stats) => {
stats = stats.toJson();
let dir = path.join(config.buildRoot, 'webpack', 'stats');
fs.ensureDirSync(dir);
fs.writeFileSync(path.join(dir, entryName + '.json'), JSON.stringify(stats));
});
}
},
},
],
recordsPath: path.join(config.tmpRoot, 'webpack.json'),
devServer: {
port: 3001, // dev server itself does not use it, but outer tasks do
historyApiFallback: true,
hot: true,
watchDelay: 10,
//noInfo: true,
publicPath: process.env.STATIC_HOST + ':3001/pack/',
contentBase: config.publicRoot
},
optimization: {
minimizer: [
new TerserPlugin({
parallel: 2,
terserOptions: {
ecma: 2022,
warnings: false,
compress: {
drop_console: true,
drop_debugger: true,
},
output: {
beautify: true,
indent_level: 0, // for error reporting, to see which line actually has the problem
// source maps actually didn't work in Qbaka that's why I put it here
},
},
}),
new CssMinimizerPlugin({}),
],
}
};
//if (process.env.NODE_ENV != 'development') { // production, ebook
/*
if (process.env.NODE_ENV == 'production') { // production, ebook
webpackConfig.plugins.push(
function clearBeforeRun() {
function clear(compiler, callback) {
fse.removeSync(webpackConfig.output.path + '/*');
callback();
}
// in watch mode this will clear between partial rebuilds
// thus removing unchanged files
// => use this plugin only in normal run
this.plugin('run', clear);
}
);
}
*/
return webpackConfig;
};