Skip to content

Commit f0ac951

Browse files
authored
fix: should respect output.filename.js config (#108)
1 parent 74a7ac9 commit f0ac951

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

e2e/cases/auto-extension/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,23 @@ describe('autoExtension: false', () => {
3333
expect(extname(entryFiles.cjs!)).toEqual('.js');
3434
});
3535
});
36+
37+
describe('should respect output.filename.js to override builtin logic', () => {
38+
test('type is commonjs', async () => {
39+
const fixturePath = join(__dirname, 'type-commonjs', 'config-override');
40+
const { entryFiles } = await buildAndGetResults(fixturePath);
41+
expect(extname(entryFiles.esm!)).toEqual('.mjs');
42+
expect(entryFiles.cjs).toMatchInlineSnapshot(
43+
`"<ROOT>/e2e/cases/auto-extension/type-commonjs/config-override/dist/cjs/index.babf05f8.js"`,
44+
);
45+
});
46+
47+
test('type is module', async () => {
48+
const fixturePath = join(__dirname, 'type-module', 'config-override');
49+
const { entryFiles } = await buildAndGetResults(fixturePath);
50+
expect(entryFiles.esm).toMatchInlineSnapshot(
51+
`"<ROOT>/e2e/cases/auto-extension/type-module/config-override/dist/esm/index.4ca64135.js"`,
52+
);
53+
expect(extname(entryFiles.cjs!)).toEqual('.cjs');
54+
});
55+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "auto-extension-config-override-commonjs-test",
3+
"version": "1.0.0",
4+
"private": true
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
autoExtension: false,
8+
output: {
9+
filename: {
10+
js: '[name].mjs',
11+
},
12+
},
13+
}),
14+
generateBundleCjsConfig({
15+
output: {
16+
filename: {
17+
js: '[name].[contenthash:8].js',
18+
},
19+
},
20+
}),
21+
],
22+
source: {
23+
entry: {
24+
index: '../../__fixtures__/src/index.ts',
25+
},
26+
},
27+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "auto-extension-config-override-module-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
output: {
8+
filename: {
9+
js: '[name].[contenthash:8].js',
10+
},
11+
},
12+
}),
13+
generateBundleCjsConfig({
14+
autoExtension: false,
15+
output: {
16+
filename: {
17+
js: '[name].cjs',
18+
},
19+
},
20+
}),
21+
],
22+
source: {
23+
entry: {
24+
index: '../../__fixtures__/src/index.ts',
25+
},
26+
},
27+
});

packages/core/src/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
240240
};
241241

242242
const composeAutoExtensionConfig = (
243-
format: Format,
243+
config: LibConfig,
244244
autoExtension: boolean,
245245
pkgJson?: PkgJson,
246246
): {
@@ -249,7 +249,7 @@ const composeAutoExtensionConfig = (
249249
dtsExtension: string;
250250
} => {
251251
const { jsExtension, dtsExtension } = getDefaultExtension({
252-
format,
252+
format: config.format!,
253253
pkgJson,
254254
autoExtension,
255255
});
@@ -259,6 +259,7 @@ const composeAutoExtensionConfig = (
259259
output: {
260260
filename: {
261261
js: `[name]${jsExtension}`,
262+
...config.output?.filename,
262263
},
263264
},
264265
},
@@ -494,7 +495,7 @@ async function composeLibRsbuildConfig(
494495
config: autoExtensionConfig,
495496
jsExtension,
496497
dtsExtension,
497-
} = composeAutoExtensionConfig(format!, autoExtension, pkgJson);
498+
} = composeAutoExtensionConfig(config, autoExtension, pkgJson);
498499
const bundleConfig = composeBundleConfig(jsExtension, config.bundle);
499500
const targetConfig = composeTargetConfig(config.output?.target);
500501
const syntaxConfig = composeSyntaxConfig(

0 commit comments

Comments
 (0)