Skip to content

Commit 070372c

Browse files
authoredDec 14, 2020
[Flight] Fix webpack watch mode issue (#20457)
1 parent 0f80dd1 commit 070372c

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed
 

‎packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js

+47-49
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default class ReactFlightWebpackPlugin {
9595
}
9696

9797
apply(compiler: any) {
98+
let resolvedClientReferences;
9899
const run = (params, callback) => {
99100
// First we need to find all client files on the file system. We do this early so
100101
// that we have them synchronously available later when we need them. This might
@@ -107,66 +108,63 @@ export default class ReactFlightWebpackPlugin {
107108
contextResolver,
108109
compiler.inputFileSystem,
109110
compiler.createContextModuleFactory(),
110-
(err, resolvedClientReferences) => {
111+
(err, resolvedClientRefs) => {
111112
if (err) {
112113
callback(err);
113114
return;
114115
}
115-
compiler.hooks.compilation.tap(
116-
PLUGIN_NAME,
117-
(compilation, {normalModuleFactory}) => {
118-
compilation.dependencyFactories.set(
119-
ClientReferenceDependency,
120-
normalModuleFactory,
121-
);
122-
compilation.dependencyTemplates.set(
123-
ClientReferenceDependency,
124-
new NullDependency.Template(),
125-
);
126-
127-
compilation.hooks.buildModule.tap(PLUGIN_NAME, module => {
128-
// We need to add all client references as dependency of something in the graph so
129-
// Webpack knows which entries need to know about the relevant chunks and include the
130-
// map in their runtime. The things that actually resolves the dependency is the Flight
131-
// client runtime. So we add them as a dependency of the Flight client runtime.
132-
// Anything that imports the runtime will be made aware of these chunks.
133-
// TODO: Warn if we don't find this file anywhere in the compilation.
134-
if (module.resource !== clientFileName) {
135-
return;
136-
}
137-
if (resolvedClientReferences) {
138-
for (let i = 0; i < resolvedClientReferences.length; i++) {
139-
const dep = resolvedClientReferences[i];
140-
const chunkName = this.chunkName
141-
.replace(/\[index\]/g, '' + i)
142-
.replace(
143-
/\[request\]/g,
144-
Template.toPath(dep.userRequest),
145-
);
146-
147-
const block = new AsyncDependenciesBlock(
148-
{
149-
name: chunkName,
150-
},
151-
module,
152-
null,
153-
dep.require,
154-
);
155-
block.addDependency(dep);
156-
module.addBlock(block);
157-
}
158-
}
159-
});
160-
},
161-
);
162-
116+
resolvedClientReferences = resolvedClientRefs;
163117
callback();
164118
},
165119
);
166120
};
167121

168122
compiler.hooks.run.tapAsync(PLUGIN_NAME, run);
169123
compiler.hooks.watchRun.tapAsync(PLUGIN_NAME, run);
124+
compiler.hooks.compilation.tap(
125+
PLUGIN_NAME,
126+
(compilation, {normalModuleFactory}) => {
127+
compilation.dependencyFactories.set(
128+
ClientReferenceDependency,
129+
normalModuleFactory,
130+
);
131+
compilation.dependencyTemplates.set(
132+
ClientReferenceDependency,
133+
new NullDependency.Template(),
134+
);
135+
136+
compilation.hooks.buildModule.tap(PLUGIN_NAME, module => {
137+
// We need to add all client references as dependency of something in the graph so
138+
// Webpack knows which entries need to know about the relevant chunks and include the
139+
// map in their runtime. The things that actually resolves the dependency is the Flight
140+
// client runtime. So we add them as a dependency of the Flight client runtime.
141+
// Anything that imports the runtime will be made aware of these chunks.
142+
// TODO: Warn if we don't find this file anywhere in the compilation.
143+
if (module.resource !== clientFileName) {
144+
return;
145+
}
146+
if (resolvedClientReferences) {
147+
for (let i = 0; i < resolvedClientReferences.length; i++) {
148+
const dep = resolvedClientReferences[i];
149+
const chunkName = this.chunkName
150+
.replace(/\[index\]/g, '' + i)
151+
.replace(/\[request\]/g, Template.toPath(dep.userRequest));
152+
153+
const block = new AsyncDependenciesBlock(
154+
{
155+
name: chunkName,
156+
},
157+
module,
158+
null,
159+
dep.require,
160+
);
161+
block.addDependency(dep);
162+
module.addBlock(block);
163+
}
164+
}
165+
});
166+
},
167+
);
170168

171169
compiler.hooks.emit.tap(PLUGIN_NAME, compilation => {
172170
const json = {};

0 commit comments

Comments
 (0)