From d1b6f5df0c1b626e5eabe8d24b2b166516210531 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Mon, 20 Jan 2025 14:14:24 -0700 Subject: [PATCH] feat(rspack): support multi-configuration mode in Rspack options --- packages/rspack/src/plugins/plugin.ts | 12 ++- .../rspack/src/utils/read-rspack-options.ts | 95 +++++++++++-------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/packages/rspack/src/plugins/plugin.ts b/packages/rspack/src/plugins/plugin.ts index f0d6af6de7e1c..10605b38c2490 100644 --- a/packages/rspack/src/plugins/plugin.ts +++ b/packages/rspack/src/plugins/plugin.ts @@ -149,10 +149,12 @@ async function createRspackTargets( const rspackOptions = await readRspackOptions(rspackConfig); - const outputPath = normalizeOutputPath( - rspackOptions.output?.path, - projectRoot - ); + const outputs = []; + for (const config of rspackOptions) { + if (config.output?.path) { + outputs.push(normalizeOutputPath(config.output.path, projectRoot)); + } + } const targets = {}; @@ -177,7 +179,7 @@ async function createRspackTargets( externalDependencies: ['@rspack/cli'], }, ], - outputs: [outputPath], + outputs, }; targets[options.serveTargetName] = { diff --git a/packages/rspack/src/utils/read-rspack-options.ts b/packages/rspack/src/utils/read-rspack-options.ts index f303342fb048f..71f50c1a7017c 100644 --- a/packages/rspack/src/utils/read-rspack-options.ts +++ b/packages/rspack/src/utils/read-rspack-options.ts @@ -5,53 +5,66 @@ import { readNxJsonFromDisk } from 'nx/src/devkit-internals'; /** * Reads the Rspack options from a give Rspack configuration. The configuration can be: - * 1. A standard config object - * 2. A standard function that returns a config object - * 3. A Nx-specific composable function that takes Nx context, rspack config, and returns the config object. + * 1. A single standard config object + * 2. A standard function that returns a config object (standard Rspack) + * 3. An array of standard config objects (multi-configuration mode) + * 4. A Nx-specific composable function that takes Nx context, rspack config, and returns the config object. * * @param rspackConfig */ export async function readRspackOptions( rspackConfig: unknown -): Promise { - let config: Configuration; - if (isNxRspackComposablePlugin(rspackConfig)) { - config = await rspackConfig( - {}, - { - // These values are only used during build-time, so passing stubs here just to read out - // the returned config object. - options: { - root: workspaceRoot, - projectRoot: '', - sourceRoot: '', - outputFileName: '', - assets: [], - main: '', - tsConfig: '', - outputPath: '', - rspackConfig: '', - useTsconfigPaths: undefined, - }, - context: { - root: workspaceRoot, - cwd: undefined, - isVerbose: false, - nxJsonConfiguration: readNxJsonFromDisk(workspaceRoot), - projectGraph: null, - projectsConfigurations: null, +): Promise { + let configs: Configuration[] = []; + + const resolveConfig = async (config: Configuration) => { + if (isNxRspackComposablePlugin(rspackConfig)) { + config = await rspackConfig( + {}, + { + // These values are only used during build-time, so passing stubs here just to read out + // the returned config object. + options: { + root: workspaceRoot, + projectRoot: '', + sourceRoot: '', + outputFileName: '', + assets: [], + main: '', + tsConfig: '', + outputPath: '', + rspackConfig: '', + useTsconfigPaths: undefined, + }, + context: { + root: workspaceRoot, + cwd: undefined, + isVerbose: false, + nxJsonConfiguration: readNxJsonFromDisk(workspaceRoot), + projectGraph: null, + projectsConfigurations: null, + }, + } + ); + } else if (typeof rspackConfig === 'function') { + config = await rspackConfig( + { + production: true, // we want the production build options }, - } - ); - } else if (typeof rspackConfig === 'function') { - config = await rspackConfig( - { - production: true, // we want the production build options - }, - {} - ); + {} + ); + } else { + config = rspackConfig; + } + return config; + }; + + if (Array.isArray(rspackConfig)) { + for (const config of rspackConfig) { + configs.push(await resolveConfig(config)); + } } else { - config = rspackConfig; + configs.push(await resolveConfig(rspackConfig)); } - return config; + return configs; }