Skip to content

Commit

Permalink
feat: support dynamic imports with addRelativeDeclarationExtensions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpearson committed Jan 14, 2025
1 parent 0dc1c38 commit 1179618
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/utils/dts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { statSync } from "node:fs";
import { findStaticImports, findExports, findTypeExports } from "mlly";
import {
findStaticImports,
findDynamicImports,
findExports,
findTypeExports,
} from "mlly";
import { resolve } from "pathe";
import type { TSConfig } from "pkg-types";
import type { MkdistOptions } from "../make";
Expand Down Expand Up @@ -69,7 +74,29 @@ export function extractDeclarations(
const imports = findStaticImports(contents);
const exports = findExports(contents);
const typeExports = findTypeExports(contents);
for (const spec of [...exports, ...typeExports, ...imports]) {
const dynamicImports = findDynamicImports(contents).map(
(dynamicImport) => {
let specifier: string | undefined;
try {
const value = JSON.parse(dynamicImport.expression);
if (typeof value === "string") {
specifier = value;
}
} catch {
// ignore the error
}
return {
code: dynamicImport.code,
specifier,
};
},
);
for (const spec of [
...exports,
...typeExports,
...imports,
...dynamicImports,
]) {
if (!spec.specifier || !RELATIVE_RE.test(spec.specifier)) {
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/src/star/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from "./other";
export type { Other } from "./other";

export function wonder(twinkle: import("./other").Other): string {
return twinkle;
}
3 changes: 3 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe("mkdist", () => {
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
export declare function wonder(twinkle: import("./other.js").Other): string;
"
`);

Expand Down Expand Up @@ -613,6 +614,7 @@ describe("mkdist with vue-tsc v1", () => {
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
export declare function wonder(twinkle: import("./other.js").Other): string;
"
`);
expect(
Expand Down Expand Up @@ -870,6 +872,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
export declare function wonder(twinkle: import("./other.js").Other): string;
"
`);
expect(
Expand Down

0 comments on commit 1179618

Please sign in to comment.