Skip to content

Commit 4803cd2

Browse files
committed
fix: Implicit 'index' was written to output filename for imports (fixes #105)
1 parent e7b6777 commit 4803cd2

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/utils/resolve-path-update-node.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,18 @@ export function resolvePathAndUpdateNode(
108108
filePath = tsInstance.normalizePath(path.join(path.relative(sourceFileDir, moduleDir), path.basename(filePath)));
109109
}
110110

111-
// Remove extension if implicit
112-
if (extension && implicitExtensions.includes(extension))
113-
filePath = filePath.slice(0, -extension.length) + maybeGetExplicitExtension(filePath, extension);
111+
/* Fixup filename */
112+
if (extension) {
113+
const isImplicitIndex =
114+
path.basename(filePath, extension) === 'index' &&
115+
path.basename(moduleName, path.extname(moduleName)) !== 'index';
116+
117+
// Remove implicit index
118+
if (isImplicitIndex) filePath = path.dirname(filePath);
119+
// Remove implicit extension
120+
else if (implicitExtensions.includes(extension))
121+
filePath = filePath.slice(0, -extension.length) + maybeGetExplicitExtension(filePath, extension);
122+
}
114123

115124
return filePath[0] === "." || isURL(filePath) ? filePath : `./${filePath}`;
116125
}

test/projects/specific/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export { GeneralConstA, GeneralTypeA } from "#root/general";
2323
export { GeneralConstB } from "#root/general.js";
2424

2525
export const b = 3;
26+
27+
export { ConstB } from '#elision'

test/projects/specific/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"baseUrl": ".",
1616
"paths": {
1717
"#root/*": [ "./src/*", "./generated/*" ],
18-
"#exclusion/*": [ "./src/excluded/*" ]
18+
"#exclusion/*": [ "./src/excluded/*" ],
19+
"#elision": [ "./src/type-elision" ],
20+
"#elision/*": [ "./src/type-elision/*" ]
1921
},
2022
"resolveJsonModule": true
2123
}

test/tests/transformer/specific.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,10 @@ describe(`Transformer -> Specific Cases`, () => {
143143
expect(normalEmit[indexFile].dts).toMatch(`export { GeneralConstA, GeneralTypeA } from "./general";`);
144144
expect(normalEmit[indexFile].dts).toMatch(`export { GeneralConstB } from "./general.js";`);
145145
});
146+
147+
test(`Does not output implicit index filenames`, () => {
148+
expect(normalEmit[indexFile].js).toMatch(`export { ConstB } from "./type-elision"`);
149+
expect(normalEmit[indexFile].dts).toMatch(`export { ConstB } from "./type-elision"`);
150+
});
146151
});
147152
});

0 commit comments

Comments
 (0)