Skip to content

Commit 858756f

Browse files
committed
fix: set filename dynamically using a function
1 parent 308f04c commit 858756f

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed

packages/core/src/config.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -855,25 +855,29 @@ const composeAutoExtensionConfig = (
855855
};
856856

857857
const hash = getHash();
858-
859-
const updatedConfig: EnvironmentConfig = {
860-
output: {
861-
filename: {
862-
js: `[name]${hash}${jsExtension}`,
863-
...config.output?.filename,
864-
},
865-
},
866-
};
867-
868-
const updatedJsExtension =
869-
typeof updatedConfig.output?.filename?.js === 'string' &&
870-
updatedConfig.output?.filename?.js
871-
? extname(updatedConfig.output.filename.js)
858+
const defaultJsFilename = `[name]${hash}${jsExtension}`;
859+
const userJsFilename = config.output?.filename?.js;
860+
861+
// will be returned to use in redirect feature
862+
// only support string type for now since we can not get the return value of function
863+
const finalJsExtension =
864+
typeof userJsFilename === 'string' && userJsFilename
865+
? extname(userJsFilename)
872866
: jsExtension;
873867

868+
const finalConfig = userJsFilename
869+
? {}
870+
: {
871+
output: {
872+
filename: {
873+
js: defaultJsFilename,
874+
},
875+
},
876+
};
877+
874878
return {
875-
config: updatedConfig,
876-
jsExtension: updatedJsExtension,
879+
config: finalConfig,
880+
jsExtension: finalJsExtension,
877881
dtsExtension,
878882
};
879883
};
174 KB
Loading

tests/integration/auto-extension/index.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { extname, join } from 'node:path';
2-
import { buildAndGetResults, queryContent } from 'test-helper';
2+
import {
3+
buildAndGetResults,
4+
generateFileTree,
5+
queryContent,
6+
} from 'test-helper';
37
import { describe, expect, test } from 'vitest';
48

59
describe('autoExtension: true', () => {
@@ -52,6 +56,22 @@ describe('should respect output.filename.js and output.filenameHash to override
5256
expect(entryFiles.cjs1).toMatchInlineSnapshot(
5357
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-hash/index.df02628a.js"`,
5458
);
59+
60+
// override different file types with function
61+
const fileTree = generateFileTree(
62+
join(fixturePath, './dist/cjs-override-filename-function'),
63+
);
64+
expect(fileTree).toMatchInlineSnapshot(`
65+
{
66+
"bar-image.js": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/bar-image.js",
67+
"bar-index.js": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/bar-index.js",
68+
"static": {
69+
"image": {
70+
"foo-image.png": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/static/image/foo-image.png",
71+
},
72+
},
73+
}
74+
`);
5575
});
5676

5777
test('type is module', async () => {
@@ -71,6 +91,22 @@ describe('should respect output.filename.js and output.filenameHash to override
7191
expect(entryFiles.cjs1).toMatchInlineSnapshot(
7292
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/cjs-override-filename-hash/index.df02628a.js"`,
7393
);
94+
95+
// override different file types with function
96+
const fileTree = generateFileTree(
97+
join(fixturePath, './dist/esm-override-filename-function'),
98+
);
99+
expect(fileTree).toMatchInlineSnapshot(`
100+
{
101+
"bar-image.js": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/bar-image.js",
102+
"bar-index.js": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/bar-index.js",
103+
"static": {
104+
"image": {
105+
"foo-image.png": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/static/image/foo-image.png",
106+
},
107+
},
108+
}
109+
`);
74110
});
75111
});
76112

tests/integration/auto-extension/type-commonjs/config-override/rslib.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ export default defineConfig({
4141
},
4242
},
4343
}),
44+
generateBundleCjsConfig({
45+
output: {
46+
filename: {
47+
image: () => {
48+
return 'foo-[name][ext]';
49+
},
50+
js: () => {
51+
return 'bar-[name].js';
52+
},
53+
},
54+
distPath: {
55+
root: './dist/cjs-override-filename-function',
56+
},
57+
},
58+
bundle: false,
59+
source: {
60+
entry: {
61+
index: [
62+
'../../__fixtures__/src/index.ts',
63+
'../../__fixtures__/src/image.png',
64+
],
65+
},
66+
},
67+
}),
4468
],
4569
source: {
4670
entry: {

tests/integration/auto-extension/type-module/config-override/rslib.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ export default defineConfig({
4141
},
4242
},
4343
}),
44+
generateBundleEsmConfig({
45+
output: {
46+
filename: {
47+
image: () => {
48+
return 'foo-[name][ext]';
49+
},
50+
js: () => {
51+
return 'bar-[name].js';
52+
},
53+
},
54+
distPath: {
55+
root: './dist/esm-override-filename-function',
56+
},
57+
},
58+
bundle: false,
59+
source: {
60+
entry: {
61+
index: [
62+
'../../__fixtures__/src/index.ts',
63+
'../../__fixtures__/src/image.png',
64+
],
65+
},
66+
},
67+
}),
4468
],
4569
source: {
4670
entry: {

0 commit comments

Comments
 (0)