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
36 changes: 20 additions & 16 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,25 +855,29 @@ const composeAutoExtensionConfig = (
};

const hash = getHash();

const updatedConfig: EnvironmentConfig = {
output: {
filename: {
js: `[name]${hash}${jsExtension}`,
...config.output?.filename,
},
},
};

const updatedJsExtension =
typeof updatedConfig.output?.filename?.js === 'string' &&
updatedConfig.output?.filename?.js
? extname(updatedConfig.output.filename.js)
const defaultJsFilename = `[name]${hash}${jsExtension}`;
const userJsFilename = config.output?.filename?.js;

// will be returned to use in redirect feature
// only support string type for now since we can not get the return value of function
const finalJsExtension =
typeof userJsFilename === 'string' && userJsFilename
? extname(userJsFilename)
: jsExtension;

const finalConfig = userJsFilename
? {}
: {
output: {
filename: {
js: defaultJsFilename,
},
},
};

return {
config: updatedConfig,
jsExtension: updatedJsExtension,
config: finalConfig,
jsExtension: finalJsExtension,
dtsExtension,
};
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 37 additions & 1 deletion tests/integration/auto-extension/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { extname, join } from 'node:path';
import { buildAndGetResults, queryContent } from 'test-helper';
import {
buildAndGetResults,
generateFileTree,
queryContent,
} from 'test-helper';
import { describe, expect, test } from 'vitest';

describe('autoExtension: true', () => {
Expand Down Expand Up @@ -52,6 +56,22 @@ describe('should respect output.filename.js and output.filenameHash to override
expect(entryFiles.cjs1).toMatchInlineSnapshot(
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-hash/index.df02628a.js"`,
);

// override different file types with function
const fileTree = generateFileTree(
join(fixturePath, './dist/cjs-override-filename-function'),
);
expect(fileTree).toMatchInlineSnapshot(`
{
"bar-image.js": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/bar-image.js",
"bar-index.js": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/bar-index.js",
"static": {
"image": {
"foo-image.png": "<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs-override-filename-function/static/image/foo-image.png",
},
},
}
`);
});

test('type is module', async () => {
Expand All @@ -71,6 +91,22 @@ describe('should respect output.filename.js and output.filenameHash to override
expect(entryFiles.cjs1).toMatchInlineSnapshot(
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/cjs-override-filename-hash/index.df02628a.js"`,
);

// override different file types with function
const fileTree = generateFileTree(
join(fixturePath, './dist/esm-override-filename-function'),
);
expect(fileTree).toMatchInlineSnapshot(`
{
"bar-image.js": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/bar-image.js",
"bar-index.js": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/bar-index.js",
"static": {
"image": {
"foo-image.png": "<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm-override-filename-function/static/image/foo-image.png",
},
},
}
`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ export default defineConfig({
},
},
}),
generateBundleCjsConfig({
output: {
filename: {
image: () => {
return 'foo-[name][ext]';
},
js: () => {
return 'bar-[name].js';
},
},
distPath: {
root: './dist/cjs-override-filename-function',
},
},
bundle: false,
source: {
entry: {
index: [
'../../__fixtures__/src/index.ts',
'../../__fixtures__/src/image.png',
],
},
},
}),
],
source: {
entry: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ export default defineConfig({
},
},
}),
generateBundleEsmConfig({
output: {
filename: {
image: () => {
return 'foo-[name][ext]';
},
js: () => {
return 'bar-[name].js';
},
},
distPath: {
root: './dist/esm-override-filename-function',
},
},
bundle: false,
source: {
entry: {
index: [
'../../__fixtures__/src/index.ts',
'../../__fixtures__/src/image.png',
],
},
},
}),
],
source: {
entry: {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/guide/migration/modernjs-module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ export default defineConfig({

This migration doc is contributed by community user [YanPes](https://github.com/YanPes). Much appreciation for his contribution!

Rslib is designed to be next generation of Modern.js Module and we will provide more detailed guide and scripts for migration in 2025 Q1.
Rslib is designed to be the next generation solution of Modern.js Module, and we will provide detailed configuration mapping documentation as well as feature migration guides and scripts in the second quarter of 2025.
2 changes: 1 addition & 1 deletion website/docs/zh/guide/migration/modernjs-module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ export default defineConfig({

此迁移文档由社区用户 [YanPes](https://github.com/YanPes) 贡献。非常感谢他的贡献!

Rslib 旨在成为 Modern.js Module 的下一代解决方案,我们将在 2025 年第一季度提供更多详细的迁移指南和脚本
Rslib 旨在成为 Modern.js Module 的下一代解决方案,我们将在 2025 年第二季度提供详细的配置映射文档以及功能迁移指南与脚本
Loading