Skip to content

Commit

Permalink
fix: declaration extensions should correspond to their js extension (u…
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpearson committed Dec 15, 2024
1 parent 9390803 commit bd52c24
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
21 changes: 12 additions & 9 deletions src/loaders/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DECLARATION_RE = /\.d\.[cm]?ts$/;
const CM_LETTER_RE = /(?<=\.)(c|m)(?=[jt]s$)/;

const KNOWN_EXT_RE = /\.(c|m)?[jt]sx?$/;
const VUE_EXT_RE = /\.vue\.[jt]s$/;

const TS_EXTS = new Set([".ts", ".mts", ".cts"]);

Expand All @@ -19,15 +20,23 @@ export const jsLoader: Loader = async (input, { options }) => {

let contents = await input.getContents();

const isCjs = options.format === "cjs";
let extension = isCjs ? ".js" : ".mjs"; // TODO: Default to .cjs in next major version
if (options.ext) {
extension = options.ext.startsWith(".") ? options.ext : `.${options.ext}`;
}

// declaration
if (options.declaration && !input.srcPath?.match(DECLARATION_RE)) {
const cm = input.srcPath?.match(CM_LETTER_RE)?.[0] || "";
const extension = `.d.${cm}ts`;
const cm = extension.match(CM_LETTER_RE)?.[0] || "";
// Vue files always create .vue.d.ts declarations
// No matter what the corresponding js file uses as an extension
const isVue = VUE_EXT_RE.test(input.path);
output.push({
contents,
srcPath: input.srcPath,
path: input.path,
extension,
extension: isVue ? ".d.ts" : `.d.${cm}ts`,
declaration: true,
});
}
Expand All @@ -46,7 +55,6 @@ export const jsLoader: Loader = async (input, { options }) => {
}

// esm => cjs
const isCjs = options.format === "cjs";
if (isCjs) {
contents = jiti("")
.transform({ source: contents, retainLines: false })
Expand All @@ -55,11 +63,6 @@ export const jsLoader: Loader = async (input, { options }) => {
.replace("module.exports = void 0;", "");
}

let extension = isCjs ? ".js" : ".mjs"; // TODO: Default to .cjs in next major version
if (options.ext) {
extension = options.ext.startsWith(".") ? options.ext : `.${options.ext}`;
}

output.push({
contents,
path: input.path,
Expand Down
83 changes: 43 additions & 40 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,23 @@ describe("mkdist", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.d.mts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.d.mts",
"dist/dir-export.mjs",
"dist/foo.mjs",
"dist/foo.d.ts",
"dist/foo.d.mts",
"dist/index.mjs",
"dist/index.d.ts",
"dist/index.d.mts",
"dist/star/index.mjs",
"dist/star/index.d.ts",
"dist/star/index.d.mts",
"dist/star/other.mjs",
"dist/star/other.d.ts",
"dist/star/other.d.mts",
"dist/types.d.ts",
"dist/components/index.mjs",
"dist/components/index.d.ts",
"dist/components/index.d.mts",
"dist/components/blank.vue",
"dist/components/blank.vue.d.ts",
"dist/components/js.vue",
Expand All @@ -134,19 +135,19 @@ describe("mkdist", () => {
"dist/components/ts.vue.d.ts",
"dist/components/jsx.mjs",
"dist/components/tsx.mjs",
"dist/components/jsx.d.ts",
"dist/components/tsx.d.ts",
"dist/components/jsx.d.mts",
"dist/components/tsx.d.mts",
"dist/bar/index.mjs",
"dist/bar/index.d.ts",
"dist/bar/index.d.mts",
"dist/bar/esm.mjs",
"dist/bar/esm.d.mts",
"dist/ts/test1.mjs",
"dist/ts/test2.mjs",
"dist/ts/test1.d.mts",
"dist/ts/test2.d.cts",
"dist/ts/test2.d.mts",
"dist/nested.css",
"dist/prop-types/index.mjs",
"dist/prop-types/index.d.ts",
"dist/prop-types/index.d.mts",
]
.map((f) => resolve(rootDir, f))
.sort(),
Expand All @@ -156,14 +157,14 @@ describe("mkdist", () => {
"manual declaration",
);

expect(await readFile(resolve(rootDir, "dist/star/index.d.ts"), "utf8"))
expect(await readFile(resolve(rootDir, "dist/star/index.d.mts"), "utf8"))
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
"
`);

expect(await readFile(resolve(rootDir, "dist/dir-export.d.ts"), "utf8"))
expect(await readFile(resolve(rootDir, "dist/dir-export.d.mts"), "utf8"))
.toMatchInlineSnapshot(`
"export { default as bar } from "./bar.js";
export * from "./star/index.js";
Expand All @@ -175,7 +176,7 @@ describe("mkdist", () => {
).toMatch("declare");

expect(
await readFile(resolve(rootDir, "dist/components/index.d.ts"), "utf8"),
await readFile(resolve(rootDir, "dist/components/index.d.mts"), "utf8"),
).toMatchInlineSnapshot(`
"export * as jsx from "./jsx.jsx.js";
export * as tsx from "./tsx.tsx.js";
Expand Down Expand Up @@ -537,22 +538,23 @@ describe("mkdist with vue-tsc v1", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.d.mts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.d.mts",
"dist/dir-export.mjs",
"dist/foo.mjs",
"dist/foo.d.ts",
"dist/foo.d.mts",
"dist/index.mjs",
"dist/index.d.ts",
"dist/index.d.mts",
"dist/star/index.mjs",
"dist/star/index.d.ts",
"dist/star/index.d.mts",
"dist/star/other.mjs",
"dist/star/other.d.ts",
"dist/star/other.d.mts",
"dist/types.d.ts",
"dist/components/index.mjs",
"dist/components/index.d.ts",
"dist/components/index.d.mts",
"dist/components/blank.vue",
"dist/components/blank.vue.d.ts",
"dist/components/js.vue",
Expand All @@ -565,19 +567,19 @@ describe("mkdist with vue-tsc v1", () => {
"dist/components/ts.vue.d.ts",
"dist/components/jsx.mjs",
"dist/components/tsx.mjs",
"dist/components/jsx.d.ts",
"dist/components/tsx.d.ts",
"dist/components/jsx.d.mts",
"dist/components/tsx.d.mts",
"dist/bar/index.mjs",
"dist/bar/index.d.ts",
"dist/bar/index.d.mts",
"dist/bar/esm.mjs",
"dist/bar/esm.d.mts",
"dist/ts/test1.mjs",
"dist/ts/test2.mjs",
"dist/ts/test1.d.mts",
"dist/ts/test2.d.cts",
"dist/ts/test2.d.mts",
"dist/nested.css",
"dist/prop-types/index.mjs",
"dist/prop-types/index.d.ts",
"dist/prop-types/index.d.mts",
]
.map((f) => resolve(rootDir, f))
.sort(),
Expand All @@ -587,7 +589,7 @@ describe("mkdist with vue-tsc v1", () => {
"manual declaration",
);

expect(await readFile(resolve(rootDir, "dist/star/index.d.ts"), "utf8"))
expect(await readFile(resolve(rootDir, "dist/star/index.d.mts"), "utf8"))
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
Expand All @@ -598,7 +600,7 @@ describe("mkdist with vue-tsc v1", () => {
).toMatch("declare");

expect(
await readFile(resolve(rootDir, "dist/components/index.d.ts"), "utf8"),
await readFile(resolve(rootDir, "dist/components/index.d.mts"), "utf8"),
).toMatchInlineSnapshot(`
"export * as jsx from "./jsx.jsx.js";
export * as tsx from "./tsx.tsx.js";
Expand Down Expand Up @@ -794,22 +796,23 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.d.mts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.d.mts",
"dist/dir-export.mjs",
"dist/foo.mjs",
"dist/foo.d.mts",
"dist/foo.d.ts",
"dist/index.mjs",
"dist/index.d.ts",
"dist/index.d.mts",
"dist/star/index.mjs",
"dist/star/index.d.ts",
"dist/star/index.d.mts",
"dist/star/other.mjs",
"dist/star/other.d.ts",
"dist/star/other.d.mts",
"dist/types.d.ts",
"dist/components/index.mjs",
"dist/components/index.d.ts",
"dist/components/index.d.mts",
"dist/components/blank.vue",
"dist/components/blank.vue.d.ts",
"dist/components/js.vue",
Expand All @@ -822,19 +825,19 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
"dist/components/ts.vue.d.ts",
"dist/components/jsx.mjs",
"dist/components/tsx.mjs",
"dist/components/jsx.d.ts",
"dist/components/tsx.d.ts",
"dist/components/jsx.d.mts",
"dist/components/tsx.d.mts",
"dist/bar/index.mjs",
"dist/bar/index.d.ts",
"dist/bar/index.d.mts",
"dist/bar/esm.mjs",
"dist/bar/esm.d.mts",
"dist/ts/test1.mjs",
"dist/ts/test2.mjs",
"dist/ts/test1.d.mts",
"dist/ts/test2.d.cts",
"dist/ts/test2.d.mts",
"dist/nested.css",
"dist/prop-types/index.mjs",
"dist/prop-types/index.d.ts",
"dist/prop-types/index.d.mts",
]
.map((f) => resolve(rootDir, f))
.sort(),
Expand All @@ -844,7 +847,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
"manual declaration",
);

expect(await readFile(resolve(rootDir, "dist/star/index.d.ts"), "utf8"))
expect(await readFile(resolve(rootDir, "dist/star/index.d.mts"), "utf8"))
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
Expand All @@ -855,7 +858,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => {
).toMatch("declare");

expect(
await readFile(resolve(rootDir, "dist/components/index.d.ts"), "utf8"),
await readFile(resolve(rootDir, "dist/components/index.d.mts"), "utf8"),
).toMatchInlineSnapshot(`
"export * as jsx from "./jsx.jsx.js";
export * as tsx from "./tsx.tsx.js";
Expand Down

0 comments on commit bd52c24

Please sign in to comment.