File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import type {
1010} from '@rspack/core' ;
1111import { getRootTsConfigPath } from '@nx/js' ;
1212import { getRspackCoreMajorVersion } from '../../utils/version-utils' ;
13+ import { loadRspackCore } from '../../utils/load-rspack-core' ;
1314
1415import { StatsJsonPlugin } from './plugins/stats-json-plugin' ;
1516import { 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
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import {
1717} from './loaders/stylesheet-loaders' ;
1818import { NormalizedNxAppRspackPluginOptions } from './models' ;
1919import { normalizeExtraEntryPoints } from './normalize-entry' ;
20+ import { loadRspackCore } from '../../utils/load-rspack-core' ;
2021
2122export 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,
Original file line number Diff line number Diff line change 11import { 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
94import { NormalizedRspackExecutorSchema } from '../executors/rspack/schema' ;
105import { getRspackConfigs } from '../executors/rspack/lib/config' ;
6+ import { loadRspackCore } from './load-rspack-core' ;
117
128export 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments