diff --git a/src/loaders/js.ts b/src/loaders/js.ts index e7e79fe..918f13b 100644 --- a/src/loaders/js.ts +++ b/src/loaders/js.ts @@ -19,15 +19,20 @@ 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] || ""; output.push({ contents, srcPath: input.srcPath, path: input.path, - extension, + extension: `.d.${cm}ts`, declaration: true, }); } @@ -46,7 +51,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 +59,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 854fc2e..455ee02 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -106,47 +106,48 @@ 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/blank.vue.d.mts", "dist/components/js.vue", - "dist/components/js.vue.d.ts", + "dist/components/js.vue.d.mts", "dist/components/script-multi-block.vue", - "dist/components/script-multi-block.vue.d.ts", + "dist/components/script-multi-block.vue.d.mts", "dist/components/script-setup-ts.vue", - "dist/components/script-setup-ts.vue.d.ts", + "dist/components/script-setup-ts.vue.d.mts", "dist/components/ts.vue", - "dist/components/ts.vue.d.ts", + "dist/components/ts.vue.d.mts", "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"; @@ -187,7 +188,7 @@ describe("mkdist", () => { `); expect( - await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"), + await readFile(resolve(rootDir, "dist/components/ts.vue.d.mts"), "utf8"), ).toMatchInlineSnapshot(` "declare const _default: import("vue").DefineComponent<{}, {}, { test: string; @@ -199,7 +200,7 @@ describe("mkdist", () => { expect( await readFile( - resolve(rootDir, "dist/components/blank.vue.d.ts"), + resolve(rootDir, "dist/components/blank.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -210,7 +211,7 @@ describe("mkdist", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-multi-block.vue.d.ts"), + resolve(rootDir, "dist/components/script-multi-block.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -233,7 +234,7 @@ describe("mkdist", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-setup-ts.vue.d.ts"), + resolve(rootDir, "dist/components/script-setup-ts.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -537,47 +538,48 @@ 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/blank.vue.d.mts", "dist/components/js.vue", - "dist/components/js.vue.d.ts", + "dist/components/js.vue.d.mts", "dist/components/script-multi-block.vue", - "dist/components/script-multi-block.vue.d.ts", + "dist/components/script-multi-block.vue.d.mts", "dist/components/script-setup-ts.vue", - "dist/components/script-setup-ts.vue.d.ts", + "dist/components/script-setup-ts.vue.d.mts", "dist/components/ts.vue", - "dist/components/ts.vue.d.ts", + "dist/components/ts.vue.d.mts", "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"; @@ -611,7 +613,7 @@ describe("mkdist with vue-tsc v1", () => { expect( await readFile( - resolve(rootDir, "dist/components/blank.vue.d.ts"), + resolve(rootDir, "dist/components/blank.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -621,7 +623,7 @@ describe("mkdist with vue-tsc v1", () => { `); expect( - await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"), + await readFile(resolve(rootDir, "dist/components/ts.vue.d.mts"), "utf8"), ).toMatchInlineSnapshot(` "declare const _default: import("vue").DefineComponent<{}, {}, { test: string; @@ -693,7 +695,7 @@ describe("mkdist with vue-tsc v1", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-multi-block.vue.d.ts"), + resolve(rootDir, "dist/components/script-multi-block.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -716,7 +718,7 @@ describe("mkdist with vue-tsc v1", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-setup-ts.vue.d.ts"), + resolve(rootDir, "dist/components/script-setup-ts.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -794,47 +796,48 @@ 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/blank.vue.d.mts", "dist/components/js.vue", - "dist/components/js.vue.d.ts", + "dist/components/js.vue.d.mts", "dist/components/script-multi-block.vue", - "dist/components/script-multi-block.vue.d.ts", + "dist/components/script-multi-block.vue.d.mts", "dist/components/script-setup-ts.vue", - "dist/components/script-setup-ts.vue.d.ts", + "dist/components/script-setup-ts.vue.d.mts", "dist/components/ts.vue", - "dist/components/ts.vue.d.ts", + "dist/components/ts.vue.d.mts", "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"; @@ -868,7 +871,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => { expect( await readFile( - resolve(rootDir, "dist/components/blank.vue.d.ts"), + resolve(rootDir, "dist/components/blank.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -878,7 +881,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => { `); expect( - await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"), + await readFile(resolve(rootDir, "dist/components/ts.vue.d.mts"), "utf8"), ).toMatchInlineSnapshot(` "declare const _default: import("vue").DefineComponent<{}, {}, { test: string; @@ -918,7 +921,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-multi-block.vue.d.ts"), + resolve(rootDir, "dist/components/script-multi-block.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(` @@ -941,7 +944,7 @@ describe("mkdist with vue-tsc ~v2.0.21", () => { expect( await readFile( - resolve(rootDir, "dist/components/script-setup-ts.vue.d.ts"), + resolve(rootDir, "dist/components/script-setup-ts.vue.d.mts"), "utf8", ), ).toMatchInlineSnapshot(`