Skip to content

Commit 959285b

Browse files
committed
change to use parser.hook instead of buildModule to modify module
1 parent daddb6e commit 959285b

2 files changed

Lines changed: 63 additions & 49 deletions

File tree

package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/react-server-dom-webpack-plugin.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -147,42 +147,56 @@ export default class ReactFlightWebpackPlugin {
147147
new NullDependency.Template()
148148
);
149149

150-
compilation.hooks.buildModule.tap(PLUGIN_NAME, function(module: Module) {
151-
// We need to add all client references as dependency of something in the graph so
152-
// Webpack knows which entries need to know about the relevant chunks and include the
153-
// map in their runtime. The things that actually resolves the dependency is the Flight
154-
// client runtime. So we add them as a dependency of the Flight client runtime.
155-
// Anything that imports the runtime will be made aware of these chunks.
156-
157-
if ((module as ModuleWithResource).resource !== clientFileName) {
158-
return;
159-
}
160-
161-
clientFileNameFound = true;
162-
163-
if (resolvedClientReferences) {
164-
for (var i = 0; i < resolvedClientReferences.length; i++) {
165-
const dep = resolvedClientReferences[i];
150+
const handler = (parser: any) => {
151+
parser.hooks.program.tap(
152+
PLUGIN_NAME,
153+
() => {
154+
const module = parser.state.module;
155+
156+
if ((module as ModuleWithResource).resource !== clientFileName) {
157+
return;
158+
}
166159

167-
// console.log(`~~~ resolvedClientReferences ~~ ${i}`, dep.userRequest);
160+
clientFileNameFound = true;
161+
162+
if (resolvedClientReferences) {
163+
for (var i = 0; i < resolvedClientReferences.length; i++) {
164+
const dep = resolvedClientReferences[i];
165+
166+
// TODO, remove this undefined hack properly
167+
dep.request = dep.request.replace('undefined', '')
168+
169+
const chunkName = _this.chunkName
170+
.replace(/\[index\]/g, '' + i)
171+
.replace(/\[request\]/g, Template.toPath(dep.userRequest));
172+
173+
const block = new AsyncDependenciesBlock(
174+
{
175+
name: chunkName,
176+
},
177+
null,
178+
dep.request
179+
);
180+
181+
block.addDependency(dep);
182+
module.addBlock(block);
183+
}
184+
}
185+
},
186+
);
187+
};
168188

169-
const chunkName = _this.chunkName
170-
.replace(/\[index\]/g, '' + i)
171-
.replace(/\[request\]/g, Template.toPath(dep.userRequest));
189+
normalModuleFactory.hooks.parser
190+
.for('javascript/auto')
191+
.tap('HarmonyModulesPlugin', handler);
172192

173-
const block = new AsyncDependenciesBlock(
174-
{
175-
name: chunkName,
176-
},
177-
null,
178-
dep.request.replace('undefined', '')
179-
);
193+
normalModuleFactory.hooks.parser
194+
.for('javascript/esm')
195+
.tap('HarmonyModulesPlugin', handler);
180196

181-
block.addDependency(dep);
182-
module.addBlock(block);
183-
}
184-
}
185-
});
197+
normalModuleFactory.hooks.parser
198+
.for('javascript/dynamic')
199+
.tap('HarmonyModulesPlugin', handler);
186200
});
187201

188202
compiler.hooks.make.tap(PLUGIN_NAME, function(compilation: Compilation) {

0 commit comments

Comments
 (0)