diff --git a/packages/rspack/src/executors/rspack/lib/config.ts b/packages/rspack/src/executors/rspack/lib/config.ts index 97ca2f3004..99195cbf52 100644 --- a/packages/rspack/src/executors/rspack/lib/config.ts +++ b/packages/rspack/src/executors/rspack/lib/config.ts @@ -1,4 +1,3 @@ -import { join } from 'path'; import { ExecutorContext } from '@nx/devkit'; import { type Configuration } from '@rspack/core'; import { @@ -14,7 +13,7 @@ export async function getRspackConfigs( options: NormalizedRspackExecutorSchema & { devServer?: any }, context: ExecutorContext ): Promise { - let userDefinedConfig = resolveUserDefinedRspackConfig( + let userDefinedConfig = await resolveUserDefinedRspackConfig( options.rspackConfig, options.tsConfig ); diff --git a/packages/rspack/src/plugins/plugin.ts b/packages/rspack/src/plugins/plugin.ts index 10605b38c2..a3b5c9d63b 100644 --- a/packages/rspack/src/plugins/plugin.ts +++ b/packages/rspack/src/plugins/plugin.ts @@ -141,7 +141,7 @@ async function createRspackTargets( ): Promise { const namedInputs = getNamedInputs(projectRoot, context); - const rspackConfig = resolveUserDefinedRspackConfig( + const rspackConfig = await resolveUserDefinedRspackConfig( join(context.workspaceRoot, configFilePath), getRootTsConfigPath(), true diff --git a/packages/rspack/src/utils/resolve-user-defined-rspack-config.ts b/packages/rspack/src/utils/resolve-user-defined-rspack-config.ts index 24c612296a..41cfef4541 100644 --- a/packages/rspack/src/utils/resolve-user-defined-rspack-config.ts +++ b/packages/rspack/src/utils/resolve-user-defined-rspack-config.ts @@ -1,44 +1,10 @@ -import { clearRequireCache } from '@nx/devkit/src/utils/config-utils'; -import { registerTsProject } from '@nx/js/src/internal'; +import { loadConfigFile } from '@nx/devkit/src/utils/config-utils'; -export function resolveUserDefinedRspackConfig( +export async function resolveUserDefinedRspackConfig( path: string, tsConfig: string, /** Skip require cache and return latest content */ reload = false ) { - if (reload) { - // Clear cache if the path is in the cache - if (require.cache[path]) { - // Clear all entries because config may import other modules - clearRequireCache(); - } - } - - // Don't transpile non-TS files. This prevents workspaces libs from being registered via tsconfig-paths. - // There's an issue here with Nx workspace where loading plugins from source (via tsconfig-paths) can lead to errors. - if (!/\.(ts|mts|cts)$/.test(path)) { - return require(path); - } - - const cleanupTranspiler = registerTsProject(tsConfig); - // eslint-disable-next-line @typescript-eslint/no-var-requires - const maybeCustomRspackConfig = require(path); - cleanupTranspiler(); - - // If the user provides a configuration in TS file - // then there are 3 cases for exploring an object. The first one is: - // `module.exports = { ... }`. And the second one is: - // `export default { ... }`. The ESM format is compiled into: - // `{ default: { ... } }` - // There is also a case of - // `{ default: { default: { ... } }` - const customRspackConfig = - 'default' in maybeCustomRspackConfig - ? 'default' in maybeCustomRspackConfig.default - ? maybeCustomRspackConfig.default.default - : maybeCustomRspackConfig.default - : maybeCustomRspackConfig; - - return customRspackConfig; + return await loadConfigFile(path); }