Skip to content

Commit

Permalink
fix(module-tools): add js extension when pkg type is module (#5402)
Browse files Browse the repository at this point in the history
  • Loading branch information
10Derozan authored Feb 19, 2024
1 parent 4780684 commit f2e3162
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/silent-candles-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/module-tools': patch
---

fix(module-tools): add js extension when pkg type is module
fix(module-tools): 当包类型为 module 时,给产物里的相对路径补全文件后缀
10 changes: 8 additions & 2 deletions packages/solutions/module-tools/src/builder/feature/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function redirectImport(
filePath: string,
outputDir: string,
jsExtension: string,
isModule?: boolean,
matchPath?: MatchPath,
): Promise<MagicString> {
const str: MagicString = new MagicString(code);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -267,7 +272,7 @@ export const redirect = {
if (!matchModule.length) {
return args;
}
const { jsExtension } = getDefaultOutExtension({
const { jsExtension, isModule } = getDefaultOutExtension({
format,
root,
autoExtension,
Expand All @@ -281,6 +286,7 @@ export const redirect = {
id,
dirname(outputPath),
jsExtension,
isModule,
matchPath,
);
return {
Expand Down
1 change: 1 addition & 0 deletions packages/solutions/module-tools/src/utils/outExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export const getDefaultOutExtension = (options: {
return {
jsExtension,
dtsExtension,
isModule,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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();
});
});

0 comments on commit f2e3162

Please sign in to comment.