Skip to content

Commit d643d82

Browse files
authored
Feature/webpack5 (#146)
* WIP: fix deprecation warning on code splitting. * WIP: Webpack5 local and production build successful (without poloader currently) * Update @atomic-reactor/webpack-po-loader for webpack 5. Latest compression-webpack-plugin.
1 parent d219c22 commit d643d82

File tree

9 files changed

+4806
-3995
lines changed

9 files changed

+4806
-3995
lines changed

.core/gulp.tasks.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,45 @@ const reactium = (gulp, config, webpackConfig) => {
384384
if (!isDev || process.env.MANUAL_DEV_BUILD === 'true') {
385385
webpack(webpackConfig, (err, stats) => {
386386
if (err) {
387-
console.log(err());
387+
console.error(err.stack || err);
388+
if (err.details) {
389+
console.error(err.details);
390+
}
391+
388392
done();
389393
return;
390394
}
391395

392-
let result = stats.toJson();
393-
394-
if (result.errors.length > 0) {
395-
result.errors.forEach(error => {
396-
console.log(error);
397-
});
398-
396+
const info = stats.toJson();
397+
if (stats.hasErrors()) {
398+
console.error(info.errors);
399399
done();
400400
return;
401401
}
402402

403+
if (stats.hasWarnings()) {
404+
if (process.env.DEBUG === 'on') console.warn(info.warnings);
405+
}
406+
407+
const mainEntryAssets = _.pluck(
408+
info.namedChunkGroups.main.assets,
409+
'name',
410+
);
411+
ReactiumGulp.Hook.runSync(
412+
'main-webpack-assets',
413+
mainEntryAssets,
414+
info,
415+
stats,
416+
);
417+
const serverAppPath = path.resolve(rootPath, 'src/app/server');
418+
419+
fs.ensureDirSync(serverAppPath);
420+
fs.writeFileSync(
421+
path.resolve(serverAppPath, 'webpack-manifest.json'),
422+
JSON.stringify(mainEntryAssets),
423+
'utf-8',
424+
);
425+
403426
done();
404427
});
405428
} else {

.core/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ const registeredDevMiddleware = () => {
233233
name: 'webpack',
234234
use: wpMiddlware(compiler, {
235235
serverSideRender: true,
236-
path: '/',
237236
publicPath,
238237
}),
239238
order: Enums.priority.high,

.core/sdk/named-exports/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from './roles';
22
export * from './capability';
33
export * from './setting';
44
export * from './window';
5-
export * from './i18n';
5+
// export * from './i18n';
66
export * from './routing';
77
export * from './hookable-component';
88
export * from './app-context';

.core/server/renderer/index.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,46 @@ ReactiumBoot.Hook.registerSync(
7474
(req, AppScripts, res) => {
7575
// Webpack assets
7676
if (process.env.NODE_ENV === 'development') {
77-
const assetsByChunkName = res.locals.webpackStats.toJson()
78-
.assetsByChunkName;
79-
80-
Object.values(assetsByChunkName).forEach(chunk => {
81-
_.flatten([
82-
normalizeAssets(chunk)
83-
.filter(path => path.endsWith('.js'))
84-
.filter(path => /main\.js$/.test(path)),
85-
]).forEach(path =>
77+
const { stats: context } = res.locals.webpack.devMiddleware;
78+
const stats = context.toJson();
79+
_.pluck(stats.namedChunkGroups.main.assets, 'name').forEach(
80+
path => {
8681
AppScripts.register(path, {
8782
path: `/${path}`,
8883
order: ReactiumBoot.Enums.priority.highest,
8984
footer: true,
90-
}),
91-
);
92-
});
85+
});
86+
},
87+
);
9388

9489
return;
9590
}
9691

97-
const scriptPathBase =
98-
process.env.PUBLIC_DIRECTORY || `${process.cwd()}/public`;
92+
try {
93+
const webpackAssets = JSON.parse(
94+
fs.readFileSync(
95+
path.resolve(
96+
rootPath,
97+
'src/app/server/webpack-manifest.json',
98+
),
99+
),
100+
);
99101

100-
globby
101-
.sync(
102-
path
103-
.resolve(scriptPathBase, 'assets', 'js', '*main.js')
104-
.replace(/\\/g, '/'),
105-
)
106-
.map(script => `/assets/js/${path.parse(script).base}`)
107-
.forEach(path =>
108-
AppScripts.register(path, {
109-
path,
102+
ReactiumBoot.Hook.runSync('webpack-server-assets', webpackAssets);
103+
webpackAssets.forEach(asset =>
104+
AppScripts.register(asset, {
105+
path: `/assets/js/${asset}`,
110106
order: ReactiumBoot.Enums.priority.highest,
111107
footer: true,
112108
}),
113109
);
110+
} catch (error) {
111+
console.error(
112+
'build/src/app/server/webpack-manifest.json not found or invalid JSON',
113+
error,
114+
);
115+
process.exit(1);
116+
}
114117
},
115118
ReactiumBoot.Enums.priority.highest,
116119
'SERVER-APP-SCRIPTS-CORE',

.core/webpack.config.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const _ = require('underscore');
55
const path = require('path');
66
const globby = require('./globby-patch');
77
const webpack = require('webpack');
8-
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
98
const CompressionPlugin = require('compression-webpack-plugin');
109
const env = process.env.NODE_ENV || 'development';
1110
const rootPath = path.resolve(__dirname, '..');
@@ -49,8 +48,11 @@ module.exports = config => {
4948
publicPath: '/assets/js/',
5049
path: path.resolve(__dirname, dest),
5150
filename,
51+
asyncChunks: true,
5252
};
53-
sdk.devtool = env === 'development' ? 'source-map' : '';
53+
if (env === 'development') {
54+
sdk.devtool = 'source-map';
55+
}
5456

5557
sdk.setCodeSplittingOptimize(env);
5658
if (process.env.DISABLE_CODE_SPLITTING === 'true') {
@@ -82,13 +84,6 @@ module.exports = config => {
8284
to: path.resolve('./src/reactium-translations'),
8385
});
8486

85-
sdk.addPlugin(
86-
'suppress-critical-dep-warning',
87-
new FilterWarningsPlugin({
88-
exclude: /Critical dependency: the request of a dependency is an expression/i,
89-
}),
90-
);
91-
9287
if (env === 'production') {
9388
sdk.addPlugin('asset-compression', new CompressionPlugin());
9489
}

.core/webpack.sdk.js

+7-49
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,6 @@ const path = require('path');
88

99
global.ReactiumWebpack = ReactiumWebpack;
1010

11-
const matchChunk = (test, debug) => module => {
12-
const chunkNames = [];
13-
for (const chunk of module.chunksIterable) {
14-
chunkNames.push(chunk.name);
15-
}
16-
17-
const names = _.compact(
18-
_.flatten([
19-
module.nameForCondition && module.nameForCondition(),
20-
chunkNames,
21-
]),
22-
);
23-
24-
const match = !!names.find(name => test.test(name));
25-
if (debug && match) {
26-
console.log({
27-
test: test.toString(),
28-
name: module.nameForCondition && module.nameForCondition(),
29-
chunkNames,
30-
});
31-
}
32-
33-
return match;
34-
};
35-
3611
let artifacts = {};
3712
class WebpackReactiumWebpack {
3813
constructor(name, ddd, context) {
@@ -285,30 +260,11 @@ class WebpackReactiumWebpack {
285260
return this.plugins.list.map(({ id, plugin }) => plugin);
286261
}
287262

288-
matchChunk(test, debug) {
263+
matchChunk(test) {
289264
return module => {
290-
const chunkNames = [];
291-
for (const chunk of module.chunksIterable) {
292-
chunkNames.push(chunk.name);
293-
}
294-
295-
const names = _.compact(
296-
_.flatten([
297-
module.nameForCondition && module.nameForCondition(),
298-
chunkNames,
299-
]),
300-
);
301-
302-
const match = !!names.find(name => test.test(name));
303-
if (debug && match) {
304-
console.log({
305-
test: test.toString(),
306-
name: module.nameForCondition && module.nameForCondition(),
307-
chunkNames,
308-
});
309-
}
310-
311-
return match;
265+
const moduleName =
266+
module.nameForCondition && module.nameForCondition();
267+
return test.test(moduleName);
312268
};
313269
}
314270

@@ -355,6 +311,7 @@ class WebpackReactiumWebpack {
355311
setCodeSplittingOptimize(env) {
356312
this.optimizationValue = {
357313
minimize: Boolean(env !== 'development'),
314+
chunkIds: 'named',
358315
splitChunks: {
359316
chunks: 'all',
360317
cacheGroups: {
@@ -414,7 +371,6 @@ class WebpackReactiumWebpack {
414371
target: this.target,
415372
output: this.output,
416373
entry: this.entry,
417-
devtool: this.devtool,
418374
optimization: this.optimization,
419375
externals: this.getExternals(),
420376
module: {
@@ -426,6 +382,8 @@ class WebpackReactiumWebpack {
426382
...this.overrides,
427383
};
428384

385+
if (this.devtool) theConfig.devtool = this.devtool;
386+
429387
if (this.resolveAliases.list.length > 0) {
430388
const alias = {};
431389
this.resolveAliases.list.forEach(({ id: from, alias: to }) => {

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Thumbs.db
8383

8484
# Manifest
8585
src/manifest.js
86+
src/app/server/webpack-manifest.json
8687

8788
# Translation .mo
8889
*.mo

0 commit comments

Comments
 (0)