Skip to content

Commit e04ce5b

Browse files
claude[bot]claudenx-cloud[bot]FrozenPandaz
authored
fix(rspack): lazy-load @rspack/core in create-compiler to avoid eager ESM resolution (#36476)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com> Co-authored-by: Jason Jean <jasonjean1993@gmail.com>
1 parent a374c6f commit e04ce5b

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

packages/rspack/src/plugins/utils/apply-base-config.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from '@rspack/core';
1111
import { getRootTsConfigPath } from '@nx/js';
1212
import { getRspackCoreMajorVersion } from '../../utils/version-utils';
13+
import { loadRspackCore } from '../../utils/load-rspack-core';
1314

1415
import { StatsJsonPlugin } from './plugins/stats-json-plugin';
1516
import { GeneratePackageJsonPlugin } from './plugins/generate-package-json-plugin';
@@ -60,10 +61,8 @@ export function applyBaseConfig(
6061
options.outputHashing ??= 'all';
6162

6263
// Lazy-require avoids loading @rspack/core (pure ESM in v2) at module
63-
// parse time, so Jest can still load this file.
64-
const rspackCore: typeof import('@rspack/core') = compiler
65-
? (compiler.rspack as unknown as typeof import('@rspack/core'))
66-
: require('@rspack/core');
64+
// parse time, so Jest can still load this file. See load-rspack-core.ts.
65+
const rspackCore = loadRspackCore(compiler);
6766

6867
applyNxIndependentConfig(options, config, rspackCore);
6968

packages/rspack/src/plugins/utils/apply-web-config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from './loaders/stylesheet-loaders';
1818
import { NormalizedNxAppRspackPluginOptions } from './models';
1919
import { normalizeExtraEntryPoints } from './normalize-entry';
20+
import { loadRspackCore } from '../../utils/load-rspack-core';
2021

2122
export function applyWebConfig(
2223
options: NormalizedNxAppRspackPluginOptions,
@@ -37,9 +38,8 @@ export function applyWebConfig(
3738
// Prefer compiler.rspack when available; otherwise lazy-require
3839
// @rspack/core (works on Node 22.12+ via require(esm), and keeps the
3940
// file Jest-loadable since the require is inside the function body).
40-
const rspackCore: typeof import('@rspack/core') = compiler
41-
? (compiler.rspack as unknown as typeof import('@rspack/core'))
42-
: require('@rspack/core');
41+
// See load-rspack-core.ts.
42+
const rspackCore = loadRspackCore(compiler);
4343
const {
4444
CssExtractRspackPlugin,
4545
DefinePlugin,

packages/rspack/src/utils/create-compiler.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { ExecutorContext } from '@nx/devkit';
2-
import {
3-
rspack,
4-
type Compiler,
5-
type Configuration,
6-
type MultiCompiler,
7-
} from '@rspack/core';
2+
import type { Compiler, Configuration, MultiCompiler } from '@rspack/core';
83

94
import { NormalizedRspackExecutorSchema } from '../executors/rspack/schema';
105
import { getRspackConfigs } from '../executors/rspack/lib/config';
6+
import { loadRspackCore } from './load-rspack-core';
117

128
export async function createCompiler(
139
options: NormalizedRspackExecutorSchema & {
@@ -21,6 +17,8 @@ export async function createCompiler(
2117
validateConfig(config);
2218
}
2319

20+
// Lazy-loaded to avoid resolving @rspack/core (pure ESM) before a build runs; see load-rspack-core.ts.
21+
const { rspack } = loadRspackCore();
2422
return rspack(config);
2523
}
2624

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Compiler } from '@rspack/core';
2+
3+
// @rspack/core v2 is pure ESM; lazy-require avoids resolving it before a Compiler exists.
4+
export function loadRspackCore(
5+
compiler?: Pick<Compiler, 'rspack'>
6+
): typeof import('@rspack/core') {
7+
// Reuse the compiler's already-resolved module to avoid ending up with a second, possibly different copy.
8+
return compiler
9+
? (compiler.rspack as unknown as typeof import('@rspack/core'))
10+
: require('@rspack/core');
11+
}

0 commit comments

Comments
 (0)