Skip to content

Commit

Permalink
fix(bundling): add awaits
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored and Coly010 committed Jan 30, 2025
1 parent e4d2130 commit cabef38
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
3 changes: 1 addition & 2 deletions packages/rspack/src/executors/rspack/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { join } from 'path';
import { ExecutorContext } from '@nx/devkit';
import { type Configuration } from '@rspack/core';
import {
Expand All @@ -14,7 +13,7 @@ export async function getRspackConfigs(
options: NormalizedRspackExecutorSchema & { devServer?: any },
context: ExecutorContext
): Promise<Configuration | Configuration[]> {
let userDefinedConfig = resolveUserDefinedRspackConfig(
let userDefinedConfig = await resolveUserDefinedRspackConfig(
options.rspackConfig,
options.tsConfig
);
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function createRspackTargets(
): Promise<RspackTargets> {
const namedInputs = getNamedInputs(projectRoot, context);

const rspackConfig = resolveUserDefinedRspackConfig(
const rspackConfig = await resolveUserDefinedRspackConfig(
join(context.workspaceRoot, configFilePath),
getRootTsConfigPath(),
true
Expand Down
40 changes: 3 additions & 37 deletions packages/rspack/src/utils/resolve-user-defined-rspack-config.ts
Original file line number Diff line number Diff line change
@@ -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 import(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);
}

0 comments on commit cabef38

Please sign in to comment.