Skip to content

Commit a5f75e4

Browse files
authored
fix: output.filenameHash does not take effect on JavaScript files (#818)
1 parent e7a5716 commit a5f75e4

File tree

6 files changed

+107
-10
lines changed

6 files changed

+107
-10
lines changed

packages/core/src/config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,21 @@ const composeAutoExtensionConfig = (
841841
autoExtension,
842842
});
843843

844+
const filenameHash = config.output?.filenameHash ?? false;
845+
846+
const getHash = () => {
847+
if (typeof filenameHash === 'string') {
848+
return filenameHash ? `.[${filenameHash}]` : '';
849+
}
850+
return filenameHash ? '.[contenthash:8]' : '';
851+
};
852+
853+
const hash = getHash();
854+
844855
const updatedConfig: EnvironmentConfig = {
845856
output: {
846857
filename: {
847-
js: `[name]${jsExtension}`,
858+
js: `[name]${hash}${jsExtension}`,
848859
...config.output?.filename,
849860
},
850861
},

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,43 @@ describe('autoExtension: false', () => {
3434
});
3535
});
3636

37-
describe('should respect output.filename.js to override builtin logic', () => {
37+
describe('should respect output.filename.js and output.filenameHash to override builtin logic', () => {
3838
test('type is commonjs', async () => {
3939
const fixturePath = join(__dirname, 'type-commonjs', 'config-override');
4040
const { entryFiles } = await buildAndGetResults({ fixturePath });
41-
expect(extname(entryFiles.esm!)).toEqual('.mjs');
42-
expect(entryFiles.cjs).toMatchInlineSnapshot(
43-
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs/index.58a9daaa.js"`,
41+
42+
// override output.filename.js
43+
expect(extname(entryFiles.esm0!)).toEqual('.mjs');
44+
expect(entryFiles.cjs0).toMatchInlineSnapshot(
45+
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename/index.58a9daaa.js"`,
46+
);
47+
48+
// override output.filenameHash
49+
expect(entryFiles.esm1).toMatchInlineSnapshot(
50+
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/esm-override-filename-hash/index.996a7edd.js"`,
51+
);
52+
expect(entryFiles.cjs1).toMatchInlineSnapshot(
53+
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-hash/index.58a9daaa.js"`,
4454
);
4555
});
4656

4757
test('type is module', async () => {
4858
const fixturePath = join(__dirname, 'type-module', 'config-override');
4959
const { entryFiles } = await buildAndGetResults({ fixturePath });
50-
expect(entryFiles.esm).toMatchInlineSnapshot(
51-
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm/index.996a7edd.js"`,
60+
61+
// override output.filename.js
62+
expect(entryFiles.esm0).toMatchInlineSnapshot(
63+
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename/index.996a7edd.js"`,
64+
);
65+
expect(extname(entryFiles.cjs0!)).toEqual('.cjs');
66+
67+
// override output.filenameHash
68+
expect(entryFiles.esm1).toMatchInlineSnapshot(
69+
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-hash/index.996a7edd.js"`,
70+
);
71+
expect(entryFiles.cjs1).toMatchInlineSnapshot(
72+
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/cjs-override-filename-hash/index.58a9daaa.js"`,
5273
);
53-
expect(extname(entryFiles.cjs!)).toEqual('.cjs');
5474
});
5575
});
5676

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,36 @@ export default defineConfig({
99
filename: {
1010
js: '[name].mjs',
1111
},
12+
distPath: {
13+
root: './dist/esm-override-filename',
14+
},
1215
},
1316
}),
1417
generateBundleCjsConfig({
1518
output: {
1619
filename: {
1720
js: '[name].[contenthash:8].js',
1821
},
22+
distPath: {
23+
root: './dist/cjs-override-filename',
24+
},
25+
},
26+
}),
27+
generateBundleEsmConfig({
28+
autoExtension: false,
29+
output: {
30+
filenameHash: true,
31+
distPath: {
32+
root: './dist/esm-override-filename-hash',
33+
},
34+
},
35+
}),
36+
generateBundleCjsConfig({
37+
output: {
38+
filenameHash: true,
39+
distPath: {
40+
root: './dist/cjs-override-filename-hash',
41+
},
1942
},
2043
}),
2144
],

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export default defineConfig({
88
filename: {
99
js: '[name].[contenthash:8].js',
1010
},
11+
distPath: {
12+
root: './dist/esm-override-filename',
13+
},
1114
},
1215
}),
1316
generateBundleCjsConfig({
@@ -16,6 +19,26 @@ export default defineConfig({
1619
filename: {
1720
js: '[name].cjs',
1821
},
22+
distPath: {
23+
root: './dist/cjs-override-filename',
24+
},
25+
},
26+
}),
27+
generateBundleEsmConfig({
28+
output: {
29+
filenameHash: true,
30+
distPath: {
31+
root: './dist/esm-override-filename-hash',
32+
},
33+
},
34+
}),
35+
generateBundleCjsConfig({
36+
autoExtension: false,
37+
output: {
38+
filenameHash: true,
39+
distPath: {
40+
root: './dist/cjs-override-filename-hash',
41+
},
1942
},
2043
}),
2144
],

website/docs/en/config/rsbuild/output.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ Whether to add a hash value to the filename after the production build.
7878

7979
Rslib sets `output.filenameHash` to `false` by default.
8080

81+
::: info Filename hash
82+
83+
By default, Rslib does not add a hash value in the middle of filenames.
84+
85+
To enable this behavior, you can set `output.filenameHash` to `true`.
86+
87+
You can also specify different filenames for different types of files by configuring `output.filename`.
88+
89+
:::
90+
8191
## output.filename <RsbuildDocBadge path="/config/output/filename" text="output.filename" />
8292

8393
Sets the filename of dist files.
8494

85-
By default, Rslib sets `output.filename` based on [format](/config/lib/format), see [autoExtension](/config/lib/auto-extension) for more information.
95+
By default, Rslib will modify the JavaScript output file extension by setting `output.filename.js` according to [format](/config/lib/format), see [autoExtension](/config/lib/auto-extension) for more details.
8696

8797
{/* ## output.injectStyles <RsbuildDocBadge path="/config/output/inject-styles" text="output.injectStyles" /> */}
8898

website/docs/zh/config/rsbuild/output.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,21 @@ const defaultDistPath = {
7474

7575
Rslib 默认将 `output.filenameHash` 设置为 `false`
7676

77+
::: info 文件名中的 hash 值
78+
79+
默认情况下,Rslib 不会在文件名中间添加 hash 值。
80+
81+
要开启这个行为,你可以将 `output.filenameHash` 设置为 `true`
82+
83+
你也可以通过设置 `output.filename` 为不同类型的文件指定不同的名称。
84+
85+
:::
86+
7787
## output.filename <RsbuildDocBadge path="/config/output/filename" text="output.filename" />
7888

7989
设置构建产物的名称。
8090

81-
Rslib 默认会根据 [format](/config/lib/format) 设置 `output.filename`,详情可查看 [autoExtension](/config/lib/auto-extension)
91+
Rslib 默认会根据 [format](/config/lib/format) 设置 `output.filename.js` 来修改 JavaScript 产物文件的扩展名,详情可查看 [autoExtension](/config/lib/auto-extension)
8292

8393
## output.inlineScripts <RsbuildDocBadge path="/config/output/inline-scripts" text="output.inlineScripts" />
8494

0 commit comments

Comments
 (0)