Skip to content

Commit

Permalink
Prevent race conditions during webpack compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed May 17, 2024
1 parent 97c58bd commit 247f98c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-beans-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mfng/webpack-rsc': patch
---

Prevent race conditions during webpack compilation
72 changes: 44 additions & 28 deletions packages/webpack-rsc/src/webpack-rsc-server-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,46 @@ export class WebpackRscServerPlugin {
});
};

let needsAdditionalPass = false;

compiler.hooks.finishMake.tapPromise(
WebpackRscServerPlugin.name,
async (compilation) => {
this.serverManifest = {};
const clientReferences = [...this.clientReferencesMap.keys()];
const serverReferences = [...this.serverReferencesMap.keys()];
const referencesBefore = [...clientReferences, ...serverReferences];

await Promise.all([
...Array.from(this.clientReferencesMap.keys()).map(
async (resource) => {
try {
await includeModule(compilation, resource);
} catch (error) {
this.clientReferencesMap.delete(resource);
}
},
),
...Array.from(this.serverReferencesMap.keys()).map(
async (resource) => {
try {
await includeModule(compilation, resource, webpackRscLayerName);
} catch (error) {
this.serverReferencesMap.delete(resource);
}
},
),
...clientReferences.map(async (resource) => {
try {
await includeModule(compilation, resource);
} catch (error) {
this.clientReferencesMap.delete(resource);
}
}),
...serverReferences.map(async (resource) => {
try {
await includeModule(compilation, resource, webpackRscLayerName);
} catch (error) {
this.serverReferencesMap.delete(resource);
}
}),
]);

const referencesAfter = [
...this.clientReferencesMap.keys(),
...this.serverReferencesMap.keys(),
];

if (
referencesBefore.length !== referencesAfter.length ||
!referencesAfter.every((reference) =>
referencesBefore.includes(reference),
)
) {
needsAdditionalPass = true;
}
},
);

Expand Down Expand Up @@ -191,16 +206,12 @@ export class WebpackRscServerPlugin {
return;
}

if (module.layer === webpackRscLayerName) {
if (isClientModule) {
void includeModule(compilation, resource).catch(() => {
this.clientReferencesMap.delete(resource);
});
}

if (isServerModule && !hasServerReferenceDependency(module)) {
module.addDependency(new ServerReferenceDependency());
}
if (
module.layer === webpackRscLayerName &&
isServerModule &&
!hasServerReferenceDependency(module)
) {
module.addDependency(new ServerReferenceDependency());
}
});

Expand All @@ -226,6 +237,11 @@ export class WebpackRscServerPlugin {
.for(`javascript/esm`)
.tap(`HarmonyModulesPlugin`, onNormalModuleFactoryParser);

compilation.hooks.needAdditionalPass.tap(
WebpackRscServerPlugin.name,
() => !(needsAdditionalPass = !needsAdditionalPass),
);

compilation.hooks.afterOptimizeModuleIds.tap(
WebpackRscServerPlugin.name,
(modules) => {
Expand Down

0 comments on commit 247f98c

Please sign in to comment.