diff --git a/src/loaders/js.ts b/src/loaders/js.ts index e7e79fe9..3d2e9309 100644 --- a/src/loaders/js.ts +++ b/src/loaders/js.ts @@ -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"]); @@ -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, }); } @@ -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 }) @@ -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, diff --git a/test/index.test.ts b/test/index.test.ts index 854fc2e9..66ff0600 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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", @@ -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(), @@ -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"; @@ -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"; @@ -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", @@ -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(), @@ -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"; @@ -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"; @@ -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", @@ -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(), @@ -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"; @@ -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";