-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
webpack.config.js
404 lines (332 loc) · 13.3 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
/**
* eslint no-console: 0
*
* @file `webpack.config` for `@dbux/runtime`
*/
const path = require('path');
const fs = require('fs');
const mergeWith = require('lodash/mergeWith');
const isArray = require('lodash/isArray');
const isFunction = require('lodash/isFunction');
const webpack = require('webpack');
// eslint-disable-next-line import/no-extraneous-dependencies
// const t = require('@babel/types');
// const nodeExternals = require('webpack-node-externals');
const webpackCommon = require('./config/webpack.config.common');
const { msgPackPlugin } = webpackCommon;
const Process = require('./dbux-projects/src/util/Process').default;
const {
getDependenciesPackageJson,
makeResolve,
makeAbsolutePaths
} = require('./dbux-cli/lib/package-util');
// NOTE: we use this for bundling `debug` as "browser", which is used by `socket.io-client
require('process').type = 'renderer';
const MonoRoot = path.resolve(__dirname);
// ###########################################################################
// run external scripts
// ###########################################################################
// const execCaptureOut = (cmd, options) => Process.execCaptureOut(cmd, options);
const exec = (cmd, options) => Process.exec(cmd, options);
exec(`node ./scripts/auto-write-files.js`);
// ###########################################################################
// utilities
// ###########################################################################
function arrayMerge(dst, src) {
if (isArray(dst)) {
return dst.concat(src);
}
return undefined;
}
function mergeWithArrays(dst, src) {
return mergeWith(dst, src, arrayMerge);
}
module.exports = (env, argv) => {
try {
// ###########################################################################
// setup
// ###########################################################################
const mode = argv.mode || 'development';
const {
DBUX_VERSION,
DBUX_ROOT
} = webpackCommon('dbux-runtime', mode);
// const outputFolderName = 'dist';
// const outFile = 'bundle.js';
const defaultEntryPoint = 'src/index.js';
// resolve.alias.src =
// alias['socket.io-client'] = path.resolve(path.join(root, 'dbux-runtime/node_modules', 'socket.io-client', 'socket.io.js' ));
// console.warn(resol);
// `context` is the path from which any relative paths are resolved
const context = MonoRoot;
// ###########################################################################
// multi config
// ###########################################################################
// NOTE: this used to be used for multiple different cfgs
// -> now only used for dbux-runtime
const targets = [
...[
{
target: 'node'
},
{
target: 'web'
}
].map(cfg => ['dbux-runtime', resolve => {
const { target } = cfg;
const externals = target !== 'node' ? [] : [
// nodeExternals({
// allowlist: [
// ...Object.keys(resolve.alias).map(name => new RegExp(`^${name}/src/.*`))
// // (...args) => {
// // console.error(...args);
// // return true;
// // }
// ]
// })
];
return {
...cfg,
output: {
filename: `[name]/dist/index-${target}.js`,
sourceMapFilename: `[name]/dist/index-${target}.js.map`,
},
externals
// externals: [
// 'tty',
// 'tls',
// 'fs',
// 'net',
// 'util'
// ]
// externals: [
// nodeExternals({
// allowlist: [
// ...Object.keys(resolve.alias).map(name => new RegExp(`^${name}/src/.*`))
// // (...args) => {
// // console.error(...args);
// // return true;
// // }
// ]
// })
// // (info, callback) => {
// // // eslint-disable-next-line no-shadow
// // const { request, context } = info;
// // if (/^util|debug/.test(request)) {
// // console.error(request, context, new Error().stack);
// // }
// // callback();
// // }
// ]
};
}])
];
// ###########################################################################
// buildConfig
// ###########################################################################
// eslint-disable-next-line no-inner-declarations
function buildConfig([target, configOverrides]) {
const targetRoot = path.join(MonoRoot, target);
// console.error('base', JSON.stringify(targets.map(([target, c]) => ({ target, c: c({ alias: {} }) })), null, 2));
// ###########################################################################
// plugins
// ###########################################################################
const webpackPlugins = [
new webpack.EnvironmentPlugin({
NODE_ENV: mode,
DBUX_VERSION,
DBUX_ROOT
}),
msgPackPlugin()
];
// ###########################################################################
// output
// ###########################################################################
const output = {
// path: path.join(context, outputFolderName),
path: context,
library: '[name]', // see https://github.com/webpack/webpack/tree/master/examples/multi-part-library
// library: target
libraryTarget: 'umd',
libraryExport: 'default',
publicPath: 'dbux',
filename: '[name]/dist/index.js',
sourceMapFilename: '[name]/dist/index.js.map',
// see: https://gist.github.com/jarshwah/389f93f2282a165563990ed60f2b6d6c
devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]', // map to source with absolute file path not webpack:// protocol
// hackfix for bug: https://medium.com/@JakeXiao/window-is-undefined-in-umd-library-output-for-webpack4-858af1b881df
// globalObject: '(typeof self !== "undefined" ? self : this)',
globalObject: '(typeof globalThis !== "undefined" ? globalThis : (typeof self !== "undefined" ? self : this))'
};
// ###########################################################################
// entry
// ###########################################################################
const entry = {
[target]: path.join(targetRoot, defaultEntryPoint)
};
// ###########################################################################
// resolve + dependencies
// ###########################################################################
// find all dbux depencies in target, so we can resolve their `src` folders
const dependencyPattern = /^@dbux\/.*/;
// look up dependency by package name
const dependencyLinks = getDependenciesPackageJson(targetRoot/* , target */, dependencyPattern).
map(depName => path.join(MonoRoot, 'node_modules', depName));
// look up folders of each dependency
const resolveFolderNames = dependencyLinks.map(
// link => fs.realpathSync(link).replace(MonoRoot, '')
link => path.relative(MonoRoot, fs.realpathSync(link))
);
resolveFolderNames.push(target);
// resolve
const resolve = makeResolve(MonoRoot, resolveFolderNames);
// resolve.alias['@'] = src;
// add `src` folders to babel-loader
const absoluteDependencies = makeAbsolutePaths(MonoRoot, resolveFolderNames);
const includeSrcs = absoluteDependencies.map(r => path.join(r, 'src'));
// console.debug(` babel targets for ${target}: ${absoluteDependencies
// .map(s => s.substring(MonoRoot.length + 1))
// .join(', ')
// }`);
// ###########################################################################
// stats
// ###########################################################################
const stats = {
errorDetails: true
};
// ###########################################################################
// final cfg
// ###########################################################################
let cfg = {
watchOptions: {
poll: true,
// // keep watching notepack, but ignore everything else
// ignored: /node_modules[\\/](?!notepack\.io)/
ignored: /node_modules/
},
mode,
// https://github.com/webpack/webpack/issues/2145
// devtool: 'inline-module-source-map',
devtool: 'source-map',
// devtool: 'inline-source-map',
plugins: webpackPlugins,
context,
entry,
output,
stats,
resolve,
// ###########################################################################
// module
// ###########################################################################
module: {
rules: [
{
// test(resource) {
// // see: https://stackoverflow.com/a/46769010
// console.debug('[TEST]\n ', resource);
// return true;
// },
loader: 'babel-loader',
include: includeSrcs,
options: {
babelrcRoots: absoluteDependencies
}
},
{
use: ['source-map-loader'],
include: absoluteDependencies.map(r => path.join(r, 'dist')),
test: /\.js$/,
enforce: 'pre'
}
],
},
node: {
__dirname: true,
__filename: true
},
// see: https://webpack.js.org/guides/code-splitting/
// optimization: {
// splitChunks: {
// chunks: 'all',
// },
// },
// externals: [{
// fs: 'commonjs fs',
// net: 'commonjs net',
// // tls: 'commonjs tls',
// // NOTE: `ws` is externalized for `@dbux/runtime` because we don't need it for running in the browser
// ws: 'commonjs ws',
// util: 'commonjs util',
// // util: (target === 'dbux-runtime') ? 'commonjs util'
// },
// see: https://www.npmjs.com/package/webpack-node-externals
// NOTE: `node-externals` does not bundle `node_modules`
// nodeExternals({
// allowlist: [
// 'perf_hooks',
// ...Object.keys(resolve.alias).map(name => new RegExp(`^${name}/src/.*`))
// ],
// // (...args) {
// // console.debug('nodeExternals', ...args);
// // return true;
// // }
// // [
// // 'perf_hooks',
// // // quote from the docs: "Important - if you have set aliases in your webpack config with the exact same names as modules in node_modules, you need to whitelist them so Webpack will know they should be bundled."
// // ...Object.keys(resol.alias)
// // ]
// }),
// 'fs', 'net' // debug library complains about these
};
configOverrides = isFunction(configOverrides) ? configOverrides(resolve) : configOverrides;
cfg = mergeWithArrays(cfg, configOverrides);
return cfg;
}
// ###########################################################################
// other configs
// (WARNING: add node configs only! don't mix targets with webpack; it doesn't like it.)
// ###########################################################################
const otherWebpackConfigPaths = [
`./dbux-runtime/deps/ws.webpack.config`,
// NOTE: Don't build `dbux-graph-client` here bc/ Webpack bugs out when merging configs with different targets (i.e. `node` + `browser`)
...[
'babel-plugin',
'cli',
'server',
'code',
].map(name => `./dbux-${name}/webpack.config`),
];
const otherWebpackConfigs = otherWebpackConfigPaths.map(p =>
/* eslint-disable-next-line global-require, import/no-dynamic-require */
require(p)
);
// ###########################################################################
// module.exports
// ###########################################################################
/**
* future-work: use parallel-webpack to build multiple packages at the same time?
* (see https://github.com/trivago/parallel-webpack)
* Alternatively: `lerna --parallel exec -- npm start`
* NOTE: Probably not necessary, since only initial build takes more than 10s, else it's all rather fast.
*/
const allCfgs = [
...targets.map(buildConfig),
// NOTE: you can have multiple configs per file (see https://stackoverflow.com/a/46825869)
...otherWebpackConfigs.
map(cb => cb(env, argv)).
flat()
];
// allCfgs.forEach(cfg => {
// console.warn('CONFIG', JSON.stringify(cfg, null, 2));
// });
return allCfgs;
}
catch (err) {
console.error('webpack.config failed:', err);
throw err;
}
};
// console.warn(Object.keys(resol.alias).map(name => new RegExp(`^${name}/.*`)));
// console.debug('[Dbux] webpack.config loaded',
// // targetsAbsolute
// );