Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent race conditions during webpack compilation #78

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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