From 4dd5accd90e8e7fe1920364b16db67282c3c5efe Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Mon, 19 Feb 2024 17:27:17 +0800 Subject: [PATCH] fix(module-tools): add js extension when pkg type is module --- .changeset/silent-candles-teach.md | 6 ++++++ .../module-tools/src/builder/feature/redirect.ts | 10 ++++++++-- .../solutions/module-tools/src/utils/outExtension.ts | 1 + .../build/autoExtension/type-module/modern.config.ts | 10 +++++++++- .../autoExtension/type-module/type-module.test.ts | 8 +++++++- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 .changeset/silent-candles-teach.md diff --git a/.changeset/silent-candles-teach.md b/.changeset/silent-candles-teach.md new file mode 100644 index 000000000000..4ff21735d702 --- /dev/null +++ b/.changeset/silent-candles-teach.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +fix(module-tools): add js extension when pkg type is module +fix(module-tools): 当包类型为 module 时,给产物里的相对路径补全文件后缀 diff --git a/packages/solutions/module-tools/src/builder/feature/redirect.ts b/packages/solutions/module-tools/src/builder/feature/redirect.ts index bac87b49a40e..be790846eb27 100644 --- a/packages/solutions/module-tools/src/builder/feature/redirect.ts +++ b/packages/solutions/module-tools/src/builder/feature/redirect.ts @@ -46,6 +46,7 @@ async function redirectImport( filePath: string, outputDir: string, jsExtension: string, + isModule?: boolean, matchPath?: MatchPath, ): Promise { const str: MagicString = new MagicString(code); @@ -94,7 +95,11 @@ async function redirectImport( } if (redirect.autoExtension) { - if (ext === '' && jsExtension !== '.js' && name.startsWith('.')) { + if ( + ext === '' && + name.startsWith('.') && + (jsExtension !== '.js' || isModule) + ) { // add extension for relative path, no check if it's a directory. str.overwrite(start, end, `${name}${jsExtension}`); return; @@ -267,7 +272,7 @@ export const redirect = { if (!matchModule.length) { return args; } - const { jsExtension } = getDefaultOutExtension({ + const { jsExtension, isModule } = getDefaultOutExtension({ format, root, autoExtension, @@ -281,6 +286,7 @@ export const redirect = { id, dirname(outputPath), jsExtension, + isModule, matchPath, ); return { diff --git a/packages/solutions/module-tools/src/utils/outExtension.ts b/packages/solutions/module-tools/src/utils/outExtension.ts index 47bb40cc5250..c384fd2607c5 100644 --- a/packages/solutions/module-tools/src/utils/outExtension.ts +++ b/packages/solutions/module-tools/src/utils/outExtension.ts @@ -41,5 +41,6 @@ export const getDefaultOutExtension = (options: { return { jsExtension, dtsExtension, + isModule, }; }; diff --git a/tests/integration/module/fixtures/build/autoExtension/type-module/modern.config.ts b/tests/integration/module/fixtures/build/autoExtension/type-module/modern.config.ts index 1dcd24dfb5e9..01bc25b5253d 100644 --- a/tests/integration/module/fixtures/build/autoExtension/type-module/modern.config.ts +++ b/tests/integration/module/fixtures/build/autoExtension/type-module/modern.config.ts @@ -8,13 +8,21 @@ export default defineConfig({ autoExtension: true, format: 'cjs', sourceMap: true, + outDir: 'dist/cjs', }, - { buildType: 'bundleless', autoExtension: true, format: 'cjs', sourceMap: true, + outDir: 'dist/cjs', + }, + { + buildType: 'bundleless', + autoExtension: true, + format: 'esm', + sourceMap: true, + outDir: 'dist/esm', }, ], }); diff --git a/tests/integration/module/fixtures/build/autoExtension/type-module/type-module.test.ts b/tests/integration/module/fixtures/build/autoExtension/type-module/type-module.test.ts index e16584827953..cc65cd47e9c7 100644 --- a/tests/integration/module/fixtures/build/autoExtension/type-module/type-module.test.ts +++ b/tests/integration/module/fixtures/build/autoExtension/type-module/type-module.test.ts @@ -12,7 +12,7 @@ describe('autoExtension', () => { appDirectory: fixtureDir, enableDts: true, }); - const cwd = path.join(fixtureDir, 'dist'); + const cwd = path.join(fixtureDir, 'dist/cjs'); const outputDeclarationFile = await globby('*.d.cts', { cwd, }); @@ -31,6 +31,12 @@ describe('autoExtension', () => { expect( content.includes('./common.cjs') && content.includes('//# sourceMappingURL=index.cjs.map'), + ).toBeTruthy(); + + const esmContent = await fs.readFile( + path.join(fixtureDir, 'dist/esm', 'index.js'), + 'utf-8', ); + expect(esmContent.includes('./common.js')).toBeTruthy(); }); });