Skip to content

Commit

Permalink
optimize webpack runtime as a single chunk for subapps
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jan 14, 2020
1 parent 8d12e0d commit 759ba51
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (AppMode.hasSubApps) {
// The filename has the pattern of hex-sum.bundle1~bundle2~bundle#.js
// https://webpack.js.org/plugins/split-chunks-plugin/
config.optimization = {
runtimeChunk: "single",
splitChunks: {
chunks: "all",
minSize: 30 * 1024,
Expand Down
11 changes: 9 additions & 2 deletions packages/subapp-web/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ const utils = {
// find entry point
const entryPoints = assets.entryPoints[entryName];
assert(entryPoints, `subapp-web: no entry point found for ${name}`);

// without webpack optimization.runtimeChunk the entry point bundles are generated
// as <entryName>.bundle.js, like header.bundle.js
// but with runtimeChunk, the entry point are generated with hash
// as <hash>.<entryName>.js
// So check both cases.
const matchEntry = x => x === `${entryName}.bundle.js` || x.endsWith(`.${entryName}.js`);
// map all IDs to actual assets
const bundleAssets = entryPoints
.map(id => {
let bundleName;
const js = assets.chunksById.js[id];
// Only use IDs that has bundle with name that starts with entry's name
if (Array.isArray(js)) {
bundleName = js.find(x => x.startsWith(entryName));
} else if (js.startsWith(entryName)) {
bundleName = js.find(matchEntry);
} else if (matchEntry(js)) {
bundleName = js;
}
return bundleName && assets.js.find(x => x.name === bundleName);
Expand Down
2 changes: 1 addition & 1 deletion packages/subapp-web/src/subapp-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function loadSubAppBundles(names, done) {
return chunkIds.reduce((a2, id) => {
// chunk could have multiple assets
return [].concat(ba.jsChunksById[id]).reduce((a3, asset) => {
return a3.concat({ name, id, asset });
return asset ? a3.concat({ name, id, asset }) : a3;
}, a2);
}, a);
}, [])
Expand Down

0 comments on commit 759ba51

Please sign in to comment.