Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,11 @@ const composeExternalsConfig = (
};
};

const composeAutoExtensionConfig = (
const composeOutputFilenameConfig = (
config: LibConfig,
format: Format,
autoExtension: boolean,
multiCompilerIndex: number | null,
pkgJson?: PkgJson,
): {
config: EnvironmentConfig;
Expand All @@ -998,9 +999,38 @@ const composeAutoExtensionConfig = (
return filenameHash ? '.[contenthash:8]' : '';
};

// Copied from https://github.com/web-infra-dev/rspack/blob/2efea8673f86a562559e26a9351680e8df4d9ae9/packages/rspack/src/config/defaults.ts#L667-L680.
const inferChunkFilename = (filename: string): string | undefined => {
if (typeof filename !== 'function') {
const hasName = filename.includes('[name]');
const hasId = filename.includes('[id]');
const hasChunkHash = filename.includes('[chunkhash]');
const hasContentHash = filename.includes('[contenthash]');
const multiCompilerPrefix =
typeof multiCompilerIndex === 'number' ? `${multiCompilerIndex}~` : '';
// Anything changing depending on chunk is fine

if (hasChunkHash || hasContentHash || hasName || hasId)
return filename.replace(
/(^|\/)([^/]*(?:\?|$))/,
`$1${multiCompilerPrefix}$2`,
);
// Otherwise prefix "[id]." in front of the basename to make it changing
return filename.replace(
/(^|\/)([^/]*(?:\?|$))/,
`$1${multiCompilerIndex}[id].$2`,
);
}

return undefined;
};

const hash = getHash();
const defaultJsFilename = `[name]${hash}${jsExtension}`;
const userJsFilename = config.output?.filename?.js;
const defaultJsChunkFilename = inferChunkFilename(
(userJsFilename as string) ?? defaultJsFilename,
);

// will be returned to use in redirect feature
// only support string type for now since we can not get the return value of function
Expand All @@ -1009,15 +1039,25 @@ const composeAutoExtensionConfig = (
? extname(userJsFilename)
: jsExtension;

const finalConfig = userJsFilename
? {}
: {
const chunkFilename: RsbuildConfig = {
tools: {
rspack: {
output: {
chunkFilename: defaultJsChunkFilename,
},
},
},
};

const finalConfig: RsbuildConfig = userJsFilename
? chunkFilename
: mergeRsbuildConfig(chunkFilename, {
output: {
filename: {
js: defaultJsFilename,
},
},
};
});

return {
config: finalConfig,
Expand Down Expand Up @@ -1604,6 +1644,7 @@ const composeExternalHelpersConfig = (

async function composeLibRsbuildConfig(
config: LibConfig,
multiCompilerIndex: number | null, // null means there's non multi-compiler
root?: string,
sharedPlugins?: RsbuildPlugins,
) {
Expand Down Expand Up @@ -1649,17 +1690,24 @@ async function composeLibRsbuildConfig(
config.output?.externals,
);
const {
config: autoExtensionConfig,
config: outputFilenameConfig,
jsExtension,
dtsExtension,
} = composeAutoExtensionConfig(config, format, autoExtension, pkgJson);
} = composeOutputFilenameConfig(
config,
format,
autoExtension,
multiCompilerIndex,
pkgJson,
);
const { entryConfig, outBase } = await composeEntryConfig(
config.source?.entry!,
config.bundle,
rootPath,
cssModulesAuto,
config.outBase,
);

const { config: bundlelessExternalConfig } = composeBundlelessExternalConfig(
jsExtension,
redirect,
Expand Down Expand Up @@ -1709,10 +1757,11 @@ async function composeLibRsbuildConfig(

return mergeRsbuildConfig(
formatConfig,
// outputConfig,
shimsConfig,
syntaxConfig,
externalHelpersConfig,
autoExtensionConfig,
outputFilenameConfig,
targetConfig,
// #region Externals configs
// The order of the externals config should come in the following order:
Expand Down Expand Up @@ -1761,7 +1810,7 @@ export async function composeCreateRsbuildConfig(
);
}

const libConfigPromises = libConfigsArray.map(async (libConfig) => {
const libConfigPromises = libConfigsArray.map(async (libConfig, index) => {
const userConfig = mergeRsbuildConfig<LibConfig>(
sharedRsbuildConfig,
libConfig,
Expand All @@ -1771,6 +1820,7 @@ export async function composeCreateRsbuildConfig(
// configuration and Lib configuration in the settings.
const libRsbuildConfig = await composeLibRsbuildConfig(
userConfig,
libConfigsArray.length > 1 ? index : null,
root,
sharedPlugins,
);
Expand Down
35 changes: 30 additions & 5 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
output: {
path: '<WORKSPACE>/dist',
filename: '[name].js',
chunkFilename: '[name].js',
chunkFilename: '0~[name].js',
publicPath: 'auto',
pathinfo: false,
hashFunction: 'xxhash64',
Expand Down Expand Up @@ -1091,7 +1091,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
output: {
path: '<WORKSPACE>/dist',
filename: '[name].cjs',
chunkFilename: '[name].cjs',
chunkFilename: '1~[name].cjs',
publicPath: 'auto',
pathinfo: false,
hashFunction: 'xxhash64',
Expand Down Expand Up @@ -1775,7 +1775,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
output: {
path: '<WORKSPACE>/dist',
filename: '[name].js',
chunkFilename: '[name].js',
chunkFilename: '2~[name].js',
publicPath: '/',
pathinfo: false,
hashFunction: 'xxhash64',
Expand Down Expand Up @@ -2375,7 +2375,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
output: {
path: '<WORKSPACE>/dist',
filename: '[name].js',
chunkFilename: '[name].js',
chunkFilename: '3~[name].js',
publicPath: '/',
pathinfo: false,
hashFunction: 'xxhash64',
Expand Down Expand Up @@ -2918,7 +2918,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
output: {
path: '<WORKSPACE>/dist',
filename: '[name].js',
chunkFilename: '[name].js',
chunkFilename: '4~[name].js',
publicPath: '/',
pathinfo: false,
hashFunction: 'xxhash64',
Expand Down Expand Up @@ -3695,6 +3695,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
],
},
[Function],
{
"output": {
"chunkFilename": "0~[name].js",
},
},
{
"target": [
"node",
Expand Down Expand Up @@ -3966,6 +3971,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
],
},
[Function],
{
"output": {
"chunkFilename": "1~[name].cjs",
},
},
{
"target": [
"node",
Expand Down Expand Up @@ -4199,6 +4209,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
],
},
[Function],
{
"output": {
"chunkFilename": "2~[name].js",
},
},
{
"target": [
"node",
Expand Down Expand Up @@ -4431,6 +4446,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
],
},
[Function],
{
"output": {
"chunkFilename": "3~[name].js",
},
},
{
"target": [
"node",
Expand Down Expand Up @@ -4590,6 +4610,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
},
[Function],
[Function],
{
"output": {
"chunkFilename": "4~[name].js",
},
},
{
"target": [
"web",
Expand Down
12 changes: 8 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/output/chunkFileName-multi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "minify-output-chunk-file-name-multi-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
21 changes: 21 additions & 0 deletions tests/integration/output/chunkFileName-multi/rslib1.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
source: {
entry: {
lib1: './src/lib1.js',
},
},
}),
generateBundleEsmConfig({
source: {
entry: {
lib2: './src/lib2.js',
},
},
}),
],
});
26 changes: 26 additions & 0 deletions tests/integration/output/chunkFileName-multi/rslib2.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
output: {
filename: {
js: 'static/js/[name].[contenthash:8].js',
},
},
lib: [
generateBundleEsmConfig({
source: {
entry: {
lib1: './src/lib1.js',
},
},
}),
generateBundleEsmConfig({
source: {
entry: {
lib2: './src/lib2.js',
},
},
}),
],
});
31 changes: 31 additions & 0 deletions tests/integration/output/chunkFileName-multi/rslib3.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
source: {
entry: {
lib1: './src/lib1.js',
},
},
output: {
filename: {
js: 'static1/js/[name].js',
},
},
}),
generateBundleEsmConfig({
source: {
entry: {
lib2: './src/lib2.js',
},
},
output: {
filename: {
js: 'static2/js/[name].[contenthash:8].js',
},
},
}),
],
});
4 changes: 4 additions & 0 deletions tests/integration/output/chunkFileName-multi/src/lib1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async function main() {
const { foo } = await import('./shared.js');
return foo;
}
4 changes: 4 additions & 0 deletions tests/integration/output/chunkFileName-multi/src/lib2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async function main() {
const { bar } = await import('./shared.js');
return bar;
}
2 changes: 2 additions & 0 deletions tests/integration/output/chunkFileName-multi/src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
6 changes: 6 additions & 0 deletions tests/integration/output/chunkFileName-single/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "minify-output-chunk-file-name-single-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
Loading
Loading