diff --git a/src/loaders/js.ts b/src/loaders/js.ts index e9e1ff3..c3bf89b 100644 --- a/src/loaders/js.ts +++ b/src/loaders/js.ts @@ -6,11 +6,13 @@ import type { Loader, LoaderResult } from "../loader"; const DECLARATION_RE = /\.d\.[cm]?ts$/; const CM_LETTER_RE = /(?<=\.)(c|m)(?=[jt]s$)/; +const KNOWN_EXT_RE = /\.(c|m)?[jt]sx?$/; + +const TS_EXTS = new Set([".ts", ".mts", ".cts"]); + export const jsLoader: Loader = async (input, { options }) => { - if ( - ![".ts", ".js", ".cjs", ".mjs", ".tsx", ".jsx"].includes(input.extension) || - DECLARATION_RE.test(input.path) - ) { + if (!KNOWN_EXT_RE.test(input.path) || DECLARATION_RE.test(input.path)) { + console.log("Ignoring file with known extension:", input.path); return; } @@ -32,7 +34,7 @@ export const jsLoader: Loader = async (input, { options }) => { } // typescript => js - if (input.extension === ".ts") { + if (TS_EXTS.has(input.extension)) { contents = await transform(contents, { ...options.esbuild, loader: "ts", diff --git a/test/fixture/src/ts/test1.mts b/test/fixture/src/ts/test1.mts new file mode 100644 index 0000000..869a185 --- /dev/null +++ b/test/fixture/src/ts/test1.mts @@ -0,0 +1 @@ +export default "test1.mts" as string; diff --git a/test/fixture/src/ts/test2.cts b/test/fixture/src/ts/test2.cts new file mode 100644 index 0000000..1d61271 --- /dev/null +++ b/test/fixture/src/ts/test2.cts @@ -0,0 +1 @@ +module.exports = "test2.cts" as string; diff --git a/test/index.test.ts b/test/index.test.ts index 337560d..e848d1d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -26,6 +26,8 @@ describe("mkdist", () => { "dist/components/tsx.mjs", "dist/bar/index.mjs", "dist/bar/esm.mjs", + "dist/ts/test1.mjs", + "dist/ts/test2.mjs", ] .map((f) => resolve(rootDir, f)) .sort() @@ -105,6 +107,10 @@ describe("mkdist", () => { "dist/bar/index.d.ts", "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", ] .map((f) => resolve(rootDir, f)) .sort() @@ -180,6 +186,8 @@ describe("mkdist", () => { "dist/components/tsx.mjs", "dist/bar/index.mjs", "dist/bar/esm.mjs", + "dist/ts/test1.mjs", + "dist/ts/test2.mjs", ] .map((f) => resolve(rootDir, f)) .sort()