Skip to content

Commit

Permalink
feat(rspack): support multi-configuration mode in Rspack options
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Jan 20, 2025
1 parent 813a94c commit d1b6f5d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
12 changes: 7 additions & 5 deletions packages/rspack/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -177,7 +179,7 @@ async function createRspackTargets(
externalDependencies: ['@rspack/cli'],
},
],
outputs: [outputPath],
outputs,
};

targets[options.serveTargetName] = {
Expand Down
95 changes: 54 additions & 41 deletions packages/rspack/src/utils/read-rspack-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration> {
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<Configuration[]> {
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;
}

0 comments on commit d1b6f5d

Please sign in to comment.