forked from plone/volto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
razzle.config.js
343 lines (311 loc) · 11 KB
/
razzle.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
/* eslint no-console: 0 */
const path = require('path');
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder');
const nodeExternals = require('webpack-node-externals');
const LoadablePlugin = require('@loadable/webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const fs = require('fs');
const RootResolverPlugin = require('./webpack-plugins/webpack-root-resolver');
const RelativeResolverPlugin = require('./webpack-plugins/webpack-relative-resolver');
const createAddonsLoader = require('./create-addons-loader');
const AddonConfigurationRegistry = require('./addon-registry');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const fileLoaderFinder = makeLoaderFinder('file-loader');
const projectRootPath = path.resolve('.');
const languages = require('./src/constants/Languages');
const { poToJson } = require('@plone/scripts/i18n.cjs');
const packageJson = require(path.join(projectRootPath, 'package.json'));
const registry = new AddonConfigurationRegistry(projectRootPath);
const defaultModify = ({
env: { target, dev },
webpackConfig: config,
webpackObject: webpack,
options,
}) => {
// Compile language JSON files from po files
poToJson({ registry, addonMode: false });
if (dev) {
config.plugins.unshift(
new webpack.DefinePlugin({
__DEVELOPMENT__: true,
}),
);
} else {
config.plugins.unshift(
new webpack.DefinePlugin({
__DEVELOPMENT__: false,
}),
);
}
if (target === 'web') {
config.plugins.unshift(
new webpack.DefinePlugin({
__CLIENT__: true,
__SERVER__: false,
}),
);
config.plugins.push(
new LoadablePlugin({
outputAsset: false,
writeToDisk: { filename: path.resolve(`${projectRootPath}/build`) },
}),
);
if (dev && process.env.DEBUG_CIRCULAR) {
config.plugins.push(
new CircularDependencyPlugin({
exclude: /node_modules/,
// `onStart` is called before the cycle detection starts
onStart({ compilation }) {
console.log('start detecting webpack modules cycles');
},
failOnError: false,
// `onDetected` is called for each module that is cyclical
onDetected({ module: webpackModuleRecord, paths, compilation }) {
// `paths` will be an Array of the relative module paths that make up the cycle
// `module` will be the module record generated by webpack that caused the cycle
compilation.warnings.push(new Error(paths.join(' -> ')));
},
// `onEnd` is called before the cycle detection ends
onEnd({ compilation }) {
console.log(
`Detected ${compilation.warnings.length} circular dependencies`,
);
compilation.warnings.forEach((item) => {
if (item.message.includes('config')) {
console.log(item.message);
}
});
},
}),
);
}
config.output.filename = dev
? 'static/js/[name].js'
: 'static/js/[name].[chunkhash:8].js';
config.optimization = Object.assign({}, config.optimization, {
runtimeChunk: true,
splitChunks: {
chunks: 'all',
name: dev,
cacheGroups: {
// We reset the default values set by webpack
// So the chunks have all proper names (no random numbers)
// The CSS gets bundled in one CSS chunk and it's consistent with
// the `style-loader` load order, so no difference between
// local (project CSS) and `node_modules` ones.
vendors: false,
default: false,
},
},
});
config.plugins.unshift(
// restrict moment.js locales to en/de
// see https://github.com/jmblog/how-to-optimize-momentjs-with-webpack for details
new webpack.ContextReplacementPlugin(
/moment[/\\]locale$/,
new RegExp(Object.keys(languages).join('|')),
),
new LodashModuleReplacementPlugin({
shorthands: true,
cloning: true,
currying: true,
caching: true,
collections: true,
exotics: true,
guards: true,
metadata: true,
deburring: true,
unicode: true,
chaining: true,
memoizing: true,
coercions: true,
flattening: true,
paths: true,
placeholders: true,
}),
);
}
if (target === 'node') {
config.plugins.unshift(
new webpack.DefinePlugin({
__CLIENT__: false,
__SERVER__: true,
}),
);
// Razzle sets some of its basic env vars in the default config injecting them (for
// the client use, mainly) in a `DefinePlugin` instance. However, this also ends in
// the server build, removing the ability of the server node process to read from
// the system's (or process') env vars. In this case, in the server build, we hunt
// down the instance of the `DefinePlugin` defined by Razzle and remove the
// `process.env.PORT` so it can be overridable in runtime
const idxArr = config.plugins
.map((plugin, idx) =>
plugin.constructor.name === 'DefinePlugin' ? idx : '',
)
.filter(String);
idxArr.forEach((index) => {
const { definitions } = config.plugins[index];
if (definitions['process.env.PORT']) {
const newDefs = Object.assign({}, definitions);
// Transforms the stock RAZZLE_PUBLIC_DIR into relative path,
// so one can move the build around
newDefs['process.env.RAZZLE_PUBLIC_DIR'] = newDefs[
'process.env.RAZZLE_PUBLIC_DIR'
].replace(projectRootPath, '.');
// Handles the PORT, so it takes the real PORT from the runtime enviroment var,
// but keeps the one from build time, if different from 3000 (by not deleting it)
// So build time one takes precedence, do not set it in build time if you want
// to control it always via runtime (assumming 3000 === not set in build time)
if (newDefs['process.env.PORT'] === '3000') {
delete newDefs['process.env.PORT'];
}
config.plugins[index] = new webpack.DefinePlugin(newDefs);
}
});
}
// Don't load config|variables|overrides) files with file-loader
// Don't load SVGs from ./src/icons with file-loader
const fileLoader = config.module.rules.find(fileLoaderFinder);
fileLoader.exclude = [
/\.(config|variables|overrides)$/,
/icons\/.*\.svg$/,
...fileLoader.exclude,
];
let addonsFromEnvVar = [];
if (process.env.ADDONS) {
addonsFromEnvVar = process.env.ADDONS.split(';');
}
const addonsLoaderPath = createAddonsLoader(
registry.getAddonDependencies(),
registry.getAddons(),
);
config.resolve.plugins = [
new RelativeResolverPlugin(registry),
new RootResolverPlugin(),
];
config.resolve.alias = {
...registry.getAddonCustomizationPaths(),
...registry.getAddonsFromEnvVarCustomizationPaths(),
...registry.getProjectCustomizationPaths(),
...config.resolve.alias,
'../../theme.config$': `${projectRootPath}/theme/theme.config`,
'volto-themes': `${registry.voltoPath}/theme/themes`,
'load-volto-addons': addonsLoaderPath,
...registry.getResolveAliases(),
'@plone/volto': `${registry.voltoPath}/src`,
// to be able to reference path uncustomized by webpack
'@plone/volto-original': `${registry.voltoPath}/src`,
// be able to reference current package from customized package
'@package': `${projectRootPath}/src`,
'@root': `${projectRootPath}/src`,
// we're incorporating redux-connect
'redux-connect': `${registry.voltoPath}/src/helpers/AsyncConnect`,
// avoids including lodash multiple times.
// semantic-ui-react uses lodash-es, everything else uses lodash
'lodash-es': path.dirname(require.resolve('lodash')),
};
config.performance = {
maxAssetSize: 10000000,
maxEntrypointSize: 10000000,
};
let addonsAsExternals = [];
const { include } = options.webpackOptions.babelRule;
if (packageJson.name !== '@plone/volto') {
include.push(fs.realpathSync(`${registry.voltoPath}/src`));
}
// Add babel support external (ie. node_modules npm published packages)
const packagesNames = Object.keys(registry.packages);
if (registry.packages && packagesNames.length > 0) {
packagesNames.forEach((addon) => {
const p = fs.realpathSync(registry.packages[addon].modulePath);
if (include.indexOf(p) === -1) {
include.push(p);
}
});
addonsAsExternals = packagesNames.map((addon) => new RegExp(addon));
}
if (process.env.ADDONS) {
addonsFromEnvVar.forEach((addon) => {
const normalizedAddonName = addon.split(':')[0];
const p = fs.realpathSync(
registry.packages[normalizedAddonName].modulePath,
);
if (include.indexOf(p) === -1) {
include.push(p);
}
addonsAsExternals = [
...addonsAsExternals,
...packagesNames.map(
(normalizedAddonName) => new RegExp(normalizedAddonName),
),
];
});
}
// write a .dot file with the graph
// convert it to svg with: `dot addon-dependency-graph.dot -Tsvg -o out.svg`
if (process.env.DEBUG_ADDONS_LOADER && target === 'node') {
const addonsDepGraphPath = path.join(
process.cwd(),
'addon-dependency-graph.dot',
);
const graph = registry.getDotDependencyGraph();
fs.writeFileSync(addonsDepGraphPath, new Buffer.from(graph));
}
config.externals =
target === 'node'
? [
nodeExternals({
allowlist: [
dev ? 'webpack/hot/poll?300' : null,
/\.(eot|woff|woff2|ttf|otf)$/,
/\.(svg|png|jpg|jpeg|gif|ico)$/,
/\.(mp4|mp3|ogg|swf|webp)$/,
/\.(css|scss|sass|sss|less)$/,
// Add support for addons to include externals (ie. node_modules npm published packages)
...addonsAsExternals,
/^@plone\/volto/,
].filter(Boolean),
}),
]
: [];
return config;
};
const addonExtenders = registry.getAddonExtenders().map((m) => require(m));
const defaultPlugins = [
{ object: require('./webpack-plugins/webpack-less-plugin')({ registry }) },
{ object: require('./webpack-plugins/webpack-svg-plugin') },
{ object: require('./webpack-plugins/webpack-bundle-analyze-plugin') },
{ object: require('./jest-extender-plugin') },
];
const plugins = addonExtenders.reduce(
(acc, extender) => extender.plugins(acc),
defaultPlugins,
);
module.exports = {
plugins,
modifyJestConfig: ({ jestConfig }) => {
jestConfig.testEnvironment = 'jsdom';
return jestConfig;
},
modifyWebpackConfig: ({
env: { target, dev },
webpackConfig,
webpackObject,
options,
}) => {
const defaultConfig = defaultModify({
env: { target, dev },
webpackConfig,
webpackObject,
options,
});
const res = addonExtenders.reduce(
(acc, extender) => extender.modify(acc, { target, dev }, webpackConfig),
defaultConfig,
);
return res;
},
options: {
enableReactRefresh: true,
},
};