Skip to content

Commit

Permalink
fix(dts): handle dir + file of same name (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Sep 10, 2024
1 parent 580b67c commit f85db94
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/utils/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export async function getDeclarations(
return extractDeclarations(vfs, inputFiles, opts);
}

const EXT_RE = /\.(m|c)?(ts|js)$/;

export function extractDeclarations(
vfs: Map<string, string>,
inputFiles: string[],
Expand All @@ -56,8 +58,7 @@ export function extractDeclarations(
const dtsFilename = filename.replace(/\.(m|c)?(ts|js)x?$/, ".d.$1ts");
let contents = vfs.get(dtsFilename) || "";
if (opts?.addRelativeDeclarationExtensions) {
const ext =
filename.match(/\.(m|c)?(ts|js)$/)?.[0].replace(/ts$/, "js") || ".js";
const ext = filename.match(EXT_RE)?.[0].replace(/ts$/, "js") || ".js";
const imports = findStaticImports(contents);
const exports = findExports(contents);
const typeExports = findTypeExports(contents);
Expand All @@ -67,9 +68,16 @@ export function extractDeclarations(
}
let isDir = false;
try {
isDir = statSync(
resolve(filename, "..", spec.specifier),
).isDirectory();
const declaration = resolve(
filename,
"..",
spec.specifier + ext.replace(EXT_RE, ".d.$1ts"),
);
if (!vfs.get(declaration)) {
isDir = statSync(
resolve(filename, "..", spec.specifier),
).isDirectory();
}
} catch {
// file does not exist
}
Expand Down
1 change: 1 addition & 0 deletions test/fixture/src/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "bar";
1 change: 1 addition & 0 deletions test/fixture/src/dir-export.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as bar } from "./bar";
export * from "./star";
11 changes: 10 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("mkdist", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.mjs",
"dist/foo.mjs",
Expand Down Expand Up @@ -101,6 +102,8 @@ describe("mkdist", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.mjs",
Expand Down Expand Up @@ -150,7 +153,8 @@ describe("mkdist", () => {
`);
expect(await readFile(resolve(rootDir, "dist/dir-export.d.ts"), "utf8"))
.toMatchInlineSnapshot(`
"export * from "./star/index.js";
"export { default as bar } from "./bar.js";
export * from "./star/index.js";
"
`);
expect(
Expand Down Expand Up @@ -207,6 +211,7 @@ describe("mkdist", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.mjs",
"dist/demo.scss",
"dist/_base.scss",
"dist/dir-export.mjs",
Expand Down Expand Up @@ -449,6 +454,8 @@ describe("mkdist with vue-tsc v1", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.mjs",
Expand Down Expand Up @@ -564,6 +571,8 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.mjs",
Expand Down

0 comments on commit f85db94

Please sign in to comment.