Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dts): handle dir + file of same name #245

Merged
merged 1 commit into from
Sep 10, 2024
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
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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We maintain other regexes inline (and in modern v8 that has inline-cache there is no perf penalty of keeping regex inline)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only thinking because we use it twice. If you don’t think it’s significant I’ll inline it in both.

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
Loading