Skip to content

Commit 7d864c8

Browse files
authored
fix(bundling): rspack should allow ES config module imports (#29095)
Reproduction repo: https://github.com/olaf-cichocki/sample ## Current Behavior When using mjs config file we end up with following error: ``` ⚠️ Unable to construct project graph. Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it. Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it. An error occurred while processing files for the @nx/rspack/plugin plugin. - apps/appA/rspack.config.mjs: require() of ES Module /Users/miro/Dev/Testbox/sample/apps/appA/rspack.config.mjs not supported. Instead change the require of /Users/miro/Dev/Testbox/sample/apps/appA/rspack.config.mjs to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/miro/Dev/Testbox/sample/apps/appA/rspack.config.mjs not supported. Instead change the require of /Users/miro/Dev/Testbox/sample/apps/appA/rspack.config.mjs to a dynamic import() which is available in all CommonJS modules. at resolveUserDefinedRspackConfig (/Users/miro/Dev/Testbox/sample/node_modules/.pnpm/@nx[email protected]_@[email protected]_@[email protected]_react-dom@1_7iwdcl66l7me4m7pewq22wegge/node_modules/@nx/rspack/src/utils/resolve-user-defined-rspack-config.js:19:16) at createRspackTargets (/Users/miro/Dev/Testbox/sample/node_modules/.pnpm/@nx[email protected]_@[email protected]_@[email protected]_react-dom@1_7iwdcl66l7me4m7pewq22wegge/node_modules/@nx/rspack/src/plugins/plugin.js:65:98) at createNodesInternal (/Users/miro/Dev/Testbox/sample/node_modules/.pnpm/@nx[email protected]_@[email protected]_@[email protected]_react-dom@1_7iwdcl66l7me4m7pewq22wegge/node_modules/@nx/rspack/src/plugins/plugin.js:51:34) at async /Users/miro/Dev/Testbox/sample/node_modules/.pnpm/[email protected]/node_modules/nx/src/project-graph/plugins/utils.js:10:27 at async Promise.all (index 0) ``` ## Expected Behavior Using EU module config files is supported ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # Thank you @olaf-cichocki for reporting the issue.
1 parent c7ff6d3 commit 7d864c8

File tree

3 files changed

+5
-40
lines changed

3 files changed

+5
-40
lines changed

packages/rspack/src/executors/rspack/lib/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { join } from 'path';
21
import { ExecutorContext } from '@nx/devkit';
32
import { type Configuration } from '@rspack/core';
43
import {
@@ -14,7 +13,7 @@ export async function getRspackConfigs(
1413
options: NormalizedRspackExecutorSchema & { devServer?: any },
1514
context: ExecutorContext
1615
): Promise<Configuration | Configuration[]> {
17-
let userDefinedConfig = resolveUserDefinedRspackConfig(
16+
let userDefinedConfig = await resolveUserDefinedRspackConfig(
1817
options.rspackConfig,
1918
options.tsConfig
2019
);

packages/rspack/src/plugins/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async function createRspackTargets(
141141
): Promise<RspackTargets> {
142142
const namedInputs = getNamedInputs(projectRoot, context);
143143

144-
const rspackConfig = resolveUserDefinedRspackConfig(
144+
const rspackConfig = await resolveUserDefinedRspackConfig(
145145
join(context.workspaceRoot, configFilePath),
146146
getRootTsConfigPath(),
147147
true
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
import { clearRequireCache } from '@nx/devkit/src/utils/config-utils';
2-
import { registerTsProject } from '@nx/js/src/internal';
1+
import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';
32

4-
export function resolveUserDefinedRspackConfig(
3+
export async function resolveUserDefinedRspackConfig(
54
path: string,
65
tsConfig: string,
76
/** Skip require cache and return latest content */
87
reload = false
98
) {
10-
if (reload) {
11-
// Clear cache if the path is in the cache
12-
if (require.cache[path]) {
13-
// Clear all entries because config may import other modules
14-
clearRequireCache();
15-
}
16-
}
17-
18-
// Don't transpile non-TS files. This prevents workspaces libs from being registered via tsconfig-paths.
19-
// There's an issue here with Nx workspace where loading plugins from source (via tsconfig-paths) can lead to errors.
20-
if (!/\.(ts|mts|cts)$/.test(path)) {
21-
return require(path);
22-
}
23-
24-
const cleanupTranspiler = registerTsProject(tsConfig);
25-
// eslint-disable-next-line @typescript-eslint/no-var-requires
26-
const maybeCustomRspackConfig = require(path);
27-
cleanupTranspiler();
28-
29-
// If the user provides a configuration in TS file
30-
// then there are 3 cases for exploring an object. The first one is:
31-
// `module.exports = { ... }`. And the second one is:
32-
// `export default { ... }`. The ESM format is compiled into:
33-
// `{ default: { ... } }`
34-
// There is also a case of
35-
// `{ default: { default: { ... } }`
36-
const customRspackConfig =
37-
'default' in maybeCustomRspackConfig
38-
? 'default' in maybeCustomRspackConfig.default
39-
? maybeCustomRspackConfig.default.default
40-
: maybeCustomRspackConfig.default
41-
: maybeCustomRspackConfig;
42-
43-
return customRspackConfig;
9+
return await loadConfigFile(path);
4410
}

0 commit comments

Comments
 (0)