From 0f80dd1484ce546076c7936329610e7854ef6dff Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sun, 13 Dec 2020 17:44:59 +0000 Subject: [PATCH] [Flight] Support concatenated modules in Webpack plugin (#20449) * Extract recordModule * Add support for concatenated modules --- .../src/ReactFlightWebpackPlugin.js | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js b/packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js index a1957692b7e74..eb8c489d394d5 100644 --- a/packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js +++ b/packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js @@ -172,25 +172,36 @@ export default class ReactFlightWebpackPlugin { const json = {}; compilation.chunkGroups.forEach(chunkGroup => { const chunkIds = chunkGroup.chunks.map(c => c.id); + + function recordModule(id, mod) { + // TODO: Hook into deps instead of the target module. + // That way we know by the type of dep whether to include. + // It also resolves conflicts when the same module is in multiple chunks. + if (!/\.client\.js$/.test(mod.resource)) { + return; + } + const moduleExports = {}; + ['', '*'].concat(mod.buildMeta.providedExports).forEach(name => { + moduleExports[name] = { + id: id, + chunks: chunkIds, + name: name, + }; + }); + const href = pathToFileURL(mod.resource).href; + if (href !== undefined) { + json[href] = moduleExports; + } + } + chunkGroup.chunks.forEach(chunk => { chunk.getModules().forEach(mod => { - // TODO: Hook into deps instead of the target module. - // That way we know by the type of dep whether to include. - // It also resolves conflicts when the same module is in multiple chunks. - if (!/\.client\.js$/.test(mod.resource)) { - return; - } - const moduleExports = {}; - ['', '*'].concat(mod.buildMeta.providedExports).forEach(name => { - moduleExports[name] = { - id: mod.id, - chunks: chunkIds, - name: name, - }; - }); - const href = pathToFileURL(mod.resource).href; - if (href !== undefined) { - json[href] = moduleExports; + recordModule(mod.id, mod); + // If this is a concatenation, register each child to the parent ID. + if (mod.modules) { + mod.modules.forEach(concatenatedMod => { + recordModule(mod.id, concatenatedMod); + }); } }); });