Skip to content

Commit

Permalink
Fix rspack race condition in injection
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Jan 13, 2025
1 parent f506585 commit 84950be
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/plugins/injection/src/xpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import { INJECTED_FILE } from '@dd/core/constants';
import { getUniqueId } from '@dd/core/helpers';
import { getUniqueId, outputFile, rm } from '@dd/core/helpers';
import type { GlobalContext, Logger, PluginOptions, ToInjectItem } from '@dd/core/types';
import { InjectPosition } from '@dd/core/types';
import { createRequire } from 'module';
Expand Down Expand Up @@ -102,10 +102,22 @@ export const getXpackPlugin =
// We need to prepare the injections before the build starts.
// Otherwise they'll be empty once resolved.
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
// RSpack MAY try to resolve the entry points before the loader is ready.
// There must be some race condition around this, because it's not always the case.
if (context.bundler.name === 'rspack') {
await outputFile(filePath, '');
}
// Prepare the injections.
await addInjections(log, toInject, contentsToInject, context.cwd);
});

if (context.bundler.name === 'rspack') {
compiler.hooks.done.tapPromise(PLUGIN_NAME, async () => {
// Delete the fake file we created.
await rm(filePath);
});
}

// Handle the InjectPosition.START and InjectPosition.END.
// This is a re-implementation of the BannerPlugin,
// that is compatible with all versions of webpack and rspack,
Expand Down

0 comments on commit 84950be

Please sign in to comment.