-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
424 lines (379 loc) · 13.7 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
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
const path = require('path')
const fs = require('fs')
const glob = require('glob')
const webpack = require('webpack')
const zlib = require('zlib')
// webpack plugins
const TerserPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CompressionPlugin = require("compression-webpack-plugin")
const GenSourceCode = require('./webpack.gensrc')
const date = new Date()
const DEV_MODE = process.env.NODE_ENV !== 'production' && process.argv.indexOf('-p') === -1,
BUILD_NUMBER = `${date.getYear() - 100}${date.getMonth() + 1}${date.getDate()}.${Math.ceil((date - new Date().setHours(0, 0, 0, 0)) / 8641)}`
/*---------------------------------------------------
Define which directories are included from the build based on the config file
---------------------------------------------------*/
const EXCLUDE_FROM_BUILD = [
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, 'mongodb_tmp'),
path.resolve(__dirname, 'test'),
path.resolve(__dirname, 'server'),
path.resolve(__dirname, 'build'),
path.resolve(__dirname, 'extensions'),
path.resolve(__dirname, 'client/components/ui'),
path.resolve(__dirname, './api')]
const INCLUDE_IN_BUILD = []
let APP_CONFIG
if (fs.existsSync('/etc/lunuc/buildconfig.json')) {
APP_CONFIG = require('/etc/lunuc/buildconfig.json')
} else {
APP_CONFIG = require('./buildconfig.json')
}
const PACKAGE_JSON = require('./package.json')
const APP_VALUES = {
DEV_MODE,
BUILD_NUMBER,
APP_NAME: PACKAGE_JSON.name,
APP_VERSION: PACKAGE_JSON.version,
APP_DESCRIPTION: PACKAGE_JSON.description,
APP_DESCRIPTION: PACKAGE_JSON.description,
APP_COLOR: '#2196f3',
HTML_HEAD: '',
BASE_POLYFILL: '!window.Promise || !Promise.prototype.finally || !window.URL || !Object.values || !Object.assign || !window.Map || !window.Symbol',
...APP_CONFIG.options
}
APP_VALUES.UPLOAD_DIR_ABSPATH = path.join(__dirname, APP_VALUES.UPLOAD_DIR)
if (APP_CONFIG.extensions) {
for (const extensionName in APP_CONFIG.extensions) {
if (APP_CONFIG.extensions[extensionName].active) {
INCLUDE_IN_BUILD.push(path.resolve(__dirname, 'extensions/' + extensionName))
}
}
}
if (APP_CONFIG.ui) {
APP_CONFIG.ui.forEach(ui => {
INCLUDE_IN_BUILD.push(path.resolve(__dirname, 'client/components/ui/impl/' + ui.impl))
})
}
const excludeFunction = (path) => {
for (let i = 0; i < EXCLUDE_FROM_BUILD.length; i++) {
if (path.indexOf(EXCLUDE_FROM_BUILD[i]) === 0) {
// it belongs to the excluded files
for (let j = 0; j < INCLUDE_IN_BUILD.length; j++) {
if (path.indexOf(INCLUDE_IN_BUILD[j]) === 0) {
// it is an exception and should be included anyway
return false
}
}
return true
}
}
return false
}
const replacePlaceholders = (content, hostrule) => {
const result = new Function('const {' + Object.keys(APP_VALUES).join(',') + '} = this.APP_VALUES;return `' + content + '`').call({
APP_VALUES,
hostrule
})
return result
}
if (fs.existsSync(APP_VALUES.HOSTRULES_ABSPATH)) {
console.log('Replace hostrules templates')
fs.readdir(APP_VALUES.HOSTRULES_ABSPATH, function (err, files) {
if (err) {
console.error("Could not list the directory.", err)
}
files.forEach(function (file, index) {
// Make one pass and make the file complete
const hostpath = path.join(APP_VALUES.HOSTRULES_ABSPATH, file)
let HOSTRULE_JSON = {}
try {
HOSTRULE_JSON = require(path.join(APP_VALUES.HOSTRULES_ABSPATH, file + '.json'))
} catch (e) {
}
fs.stat(hostpath, function (error, stat) {
if (stat.isDirectory()) {
fs.readdir(hostpath, function (err, subFiles) {
if (err) {
console.error("Could not list the directory.", err)
}
subFiles.forEach(function (file, index) {
if (file.endsWith('.template')) {
fs.readFile(path.join(hostpath, file), 'utf8', function (err, contents) {
fs.writeFile(path.join(hostpath, file.substring(0, file.length - 9)), replacePlaceholders(contents, HOSTRULE_JSON), function (err) {
if (err) {
console.error("Error writing to file " + file, err)
}
})
})
}
})
})
}
})
})
})
}
const config = {
entry: ['./client/index.js'],
target: ['web', 'es5'],
output: {
path: path.resolve(__dirname, 'build'),
filename: '[name].bundle.js?v=' + BUILD_NUMBER,
chunkFilename: '[name].bundle.js?v=' + BUILD_NUMBER,
publicPath: '/'
},
externals: {
'sharp': 'commonjs sharp'
},
module: {
rules: [
{
test: /\.(js|mjs|cjs|jsx|tsx|ts)$/,
loader: 'babel-loader'
},
{
test: /\.global\.css$/,
exclude: excludeFunction,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
},
{
test: /^(?:(?!\.global).)*\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.global\.less$/,
exclude: excludeFunction,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"less-loader"
]
},
{
test: /^(?:(?!\.global).)*\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
}
]
},
plugins: [
new GenSourceCode(APP_VALUES), /* Generate some source code based on the buildconfig.json file */
new MiniCssExtractPlugin({
filename: 'style.css'
}) /* Extract css from bundle */
],
optimization: {
usedExports: true,
minimizer: []
}
}
const CopyWebpackPlugin = require('copy-webpack-plugin')
config.plugins.push(
new CopyWebpackPlugin({
patterns:
[
{
from: 'index.html', to: 'index.html',
transform: replacePlaceholders
},
{
from: 'index.min.html', to: 'index.min.html',
transform: replacePlaceholders
},
{
from: 'serviceworker.js', to: 'serviceworker.js',
transform: replacePlaceholders
},
{
from: 'manifest.json', to: 'manifest.json',
transform: replacePlaceholders
},
{from: 'favicon.ico', to: 'favicon.ico'},
{from: 'favicon.svg', to: 'favicon.svg'},
{from: 'favicon-192x192.png', to: 'favicon-192x192.png'},
{from: 'favicon-512x512.png', to: 'favicon-512x512.png'}
]
})
)
/**
* Developer / Debug Config
*/
if (DEV_MODE) {
console.log('Build for developing')
config.mode = 'development'
const PORT = (process.env.PORT || 8080)
const API_PORT = (process.env.API_PORT || 3000)
// config.plugins.push(new webpack.HotModuleReplacementPlugin())
config.devServer = {
/*contentBase: [path.join(__dirname, ''), path.join(__dirname, 'static'), path.join(__dirname, APP_VALUES.UPLOAD_DIR)],*/
static: [
{
directory: path.resolve(__dirname, ''),
publicPath: '/',
watch: false
},
{
directory: path.resolve(__dirname, 'static'),
publicPath: '/',
watch: false
},
{
directory: path.resolve(__dirname, APP_VALUES.UPLOAD_DIR),
publicPath: '/',
watch: false
}
],
historyApiFallback: {
rewrites: [
{
from: /.*\/-\/.*/,
to: function (context) {
const url = context.request.url, match = '/' + APP_CONFIG.options.PRETTYURL_SEPERATOR + '/'
if (url.indexOf(match) > -1) {
if (url.indexOf(APP_VALUES.UPLOAD_DIR) === 0) {
return url.split(match)[0]
}
return '/'
}
return context.request.url
}
}
]
},
/*inline: true,*/
hot: false,
port: PORT,
host: '0.0.0.0',
proxy: [
{
context: ['/graphql'],
target: `http://0.0.0.0:${API_PORT}`
},
{
context: ['/' + APP_VALUES.API_PREFIX],
target: `http://0.0.0.0:${API_PORT}`
},
{
context:['/lunucws'],
target: `ws://0.0.0.0:${API_PORT}`,
ws: true
}
],
client: {
logging: 'info'
},
liveReload:true,
hot:true,
devMiddleware: {
publicPath: '/',
stats: 'errors-only'
}
}
config.devtool = 'inline-source-map'
config.resolve = {
extensions: ['.js', '.json', '.wasm', '.cjs', '.mjs'],
alias: {
'react': 'preact/compat',
'react-dom': 'preact/compat'
}
}
} else {
console.log('Build for production')
config.mode = 'production'
config.resolve = {
extensions: ['.js', '.json', '.wasm', '.cjs', '.mjs'],
alias: {
'react': 'preact/compat',
'react-dom': 'preact/compat'
}
}
config.plugins.push(
{
apply: (compiler) => {
compiler.hooks.thisCompilation.tap('Replace', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'Replace',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
},
() => {
// get the file main.js
const key = 'main.bundle.js?v=' + BUILD_NUMBER
const file = compilation.getAsset(key)
let content = file.source.source()
content = content.replace(/new Error\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'new Error()')
content = content.replace(/new URIError\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'new URIError()')
content = content.replace(/new TypeError\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'new TypeError()')
content = content.replace(/new ReferenceError\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'new ReferenceError()')
content = content.replace(/throw E\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'throw E()')
content = content.replace(/throw Error\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/g, 'throw Error()')
// update main.js with new content
compilation.updateAsset(
key,
new webpack.sources.RawSource(content)
)
}
)
})
}
},
new CompressionPlugin({
minRatio: 0.95
}),
new CompressionPlugin({
filename: '[path][base].br[query]',
algorithm: 'brotliCompress',
compressionOptions: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
threshold: 10240,
minRatio: 0.95,
})
)
const terserOptions = {
extractComments: {
condition: false,
banner: () => {
return ''
}
},
terserOptions: {
ecma: undefined,
parse: {},
module: true,
mangle: {},
compress: {
booleans_as_integers: false,
drop_console: true,
pure_getters: true, /* 1kb */
unsafe: false,
passes: 2,
arguments: true, /* 50 bytes */
//unsafe: true /* 80 Bytes */
//toplevel: true /* 10 bytes */
//arguments: true /* 50 bytes */
//unsafe_proto:true /* 20 bytes */
//booleans_as_integers:true, /* 200 bytes */
//unsafe_Function: true /* 10 Bytes */
//unsafe_proto:true /* 10 Bytes */
},
format: {
comments: false
}
}
}
config.optimization.minimize = true
config.optimization.minimizer.push(
new TerserPlugin(terserOptions)
)
/*const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
config.plugins.push(new BundleAnalyzerPlugin())*/
}
module.exports = config
/* --port 8080 --hot --host 0.0.0.0 --content-base . */