-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateConfig.js
137 lines (133 loc) · 4.54 KB
/
createConfig.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
const { WebpackDeduplicationPlugin } = require('webpack-deduplication-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const path = require('path');
const webpack = require('webpack');
const DerbyViewsPlugin = require('./lib/DerbyViewPlugin');
module.exports = function createConfig(apps, rootDir, opts = {}) {
const options = {
hotModuleReplacement: false,
defines: {},
...opts,
};
return ({
mode: 'development',
entry: Object.entries(apps).reduce((acc, [name, path]) => ({
...acc,
[name]: options.hotModuleReplacement ? [
'webpack-hot-middleware/client',
'@derbyjs/derby-webpack/lib/browser',
path,
] : [
'@derbyjs/derby-webpack/lib/browser',
path,
],
}), {}),
node: {
__dirname: true,
__filename: true,
},
optimization: {
chunkIds: 'named',
moduleIds: 'named',
minimize: false,
concatenateModules: true,
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
...(Object.entries(apps).reduce((acc, [name]) => ({
...acc,
[`${name}_views`]: {
test: new RegExp(`/derby-webpack-virtual-fs/app-views/${name}__views.js`),
name: `${name}_views`,
chunks: 'all',
priority: 20,
}
}), {})),
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
},
}
},
},
output: {
filename: '[name]-[contenthash].js',
chunkFilename: '[id]-[chunkhash].js',
clean: true,
path: path.resolve(rootDir, './public/derby'),
publicPath: '/derby/',
},
// @TODO: evaluate other options for performance/precision for dev and static build
devtool: 'source-map',
module: {
rules: [
// Workaround for Webpack error when processing the async@3's ESM code dist/async.mjs:
// "The request 'process/browser' failed to resolve only because it was resolved as fully specified"
// https://github.com/webpack/webpack/issues/11467#issuecomment-691873586
//
// If the `fullySpecified: false` option is removed in the future, an alternative could be to use
// `resolve.mainFields` to have Webpack prefer the CommonJS versions of packages over the ESM versions.
{
test: /\.m?js/,
resolve: {
fullySpecified: false
}
}
],
},
plugins: ([
// order matters
// provide plugin before hot module replacement
// to ensure polyfills can be applied
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
options.hotModuleReplacement ? new webpack.HotModuleReplacementPlugin() : undefined,
new webpack.DefinePlugin({
'process.title': JSON.stringify('browser'),
'process.env.DERBY_HASH': JSON.stringify(process.env.DERBY_HASH || 'd3rby-h4$h'),
'process.browser': true,
...options.defines,
}),
new WebpackDeduplicationPlugin({}),
new DerbyViewsPlugin(apps),
new WebpackManifestPlugin({
writeToFileEmit: true,
fileName: path.resolve(rootDir, './public/manifest.json'),
}),
].filter(Boolean)),
resolve: {
extensions: ['...', '.coffee', '.ts'], // .coffee and .ts last so .js files in node_modules get precedence
// Enable below polyfills to work when `npm link`ing libraries
symlinks: false,
/*
* Polyfills for core Node libraries
*
* Without these, Webpack produces errors like `Module not found: Error: Can't resolve 'url'`.
* Modules with trailing slashes have the same names as Node core libs. The trailing slash
* causes Node to skip the usual behavior of prioritizing core modules in requires.
*/
fallback: {
events: require.resolve('events/'),
path: require.resolve('path-browserify'),
process: require.resolve('process/browser'),
racer: require.resolve('racer'),
buffer: require.resolve('buffer/'),
crypto: require.resolve('crypto-browserify'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
stream: require.resolve('stream-browserify'),
os: require.resolve('os-browserify'),
url: require.resolve('url/'),
constants: false,
fs: false,
zlib: false,
net: false,
tls: false,
vm: false,
},
},
});
}