diff --git a/src/strip-loader.ts b/src/strip-loader.ts index 61b9e6700..e76be2605 100644 --- a/src/strip-loader.ts +++ b/src/strip-loader.ts @@ -13,12 +13,10 @@ export async function load( ) { const { format } = context; if (format?.endsWith("-typescript")) { - // Use format 'module' so it returns the source as-is, without stripping the types. - // Format 'commonjs' would not return the source for historical reasons. try { const { source } = await nextLoad(url, { ...context, - format: "module", + format, }); // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source const { code } = transformSync(source!.toString(), { diff --git a/src/transform-loader.ts b/src/transform-loader.ts index 8e859b4c4..ff9fa9231 100644 --- a/src/transform-loader.ts +++ b/src/transform-loader.ts @@ -15,11 +15,9 @@ export async function load( const { format } = context; if (format?.endsWith("-typescript")) { try { - // Use format 'module' so it returns the source as-is, without stripping the types. - // Format 'commonjs' would not return the source for historical reasons. const { source } = await nextLoad(url, { ...context, - format: "module", + format, }); // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source diff --git a/test/fixtures/commonjs.cts b/test/fixtures/commonjs.cts new file mode 100644 index 000000000..d29bb2812 --- /dev/null +++ b/test/fixtures/commonjs.cts @@ -0,0 +1,9 @@ +const message: string = "Hello from CommonJS TypeScript (.cts)!"; + +function greet(name: string): void { + console.log(`${message} - ${name}`); +} + +module.exports = { greet }; + +greet("Test"); diff --git a/test/fixtures/commonjs.ts b/test/fixtures/commonjs.ts new file mode 100644 index 000000000..309b94e9c --- /dev/null +++ b/test/fixtures/commonjs.ts @@ -0,0 +1,9 @@ +const message: string = "Hello from CommonJS TypeScript!"; + +function greet(name: string): void { + console.log(`${message} - ${name}`); +} + +module.exports = { greet }; + +greet("Test"); diff --git a/test/fixtures/esm.mts b/test/fixtures/esm.mts new file mode 100644 index 000000000..8c951aa91 --- /dev/null +++ b/test/fixtures/esm.mts @@ -0,0 +1,7 @@ +const message: string = "Hello from ESM TypeScript!"; + +export function greet(name: string): void { + console.log(`${message} - ${name}`); +} + +greet("Test"); diff --git a/test/fixtures/esm/esm.ts b/test/fixtures/esm/esm.ts new file mode 100644 index 000000000..8c951aa91 --- /dev/null +++ b/test/fixtures/esm/esm.ts @@ -0,0 +1,7 @@ +const message: string = "Hello from ESM TypeScript!"; + +export function greet(name: string): void { + console.log(`${message} - ${name}`); +} + +greet("Test"); diff --git a/test/fixtures/esm/package.json b/test/fixtures/esm/package.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/test/fixtures/esm/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/loader.test.js b/test/loader.test.js index 3d00a86db..b0257792f 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -99,3 +99,99 @@ test("should throw syntax error for invalid typescript", async (t) => { match(result.stderr, /await isn't allowed in non-async function/); strictEqual(result.code, 1); }); + +test("should work with commonjs typescript files using strip", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-strip.mjs", + fixturesPath("commonjs.ts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from CommonJS TypeScript!/); + strictEqual(result.code, 0); +}); + +test("should work with commonjs typescript files using transform", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-transform.mjs", + fixturesPath("commonjs.ts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from CommonJS TypeScript!/); + strictEqual(result.code, 0); +}); + +test("should work with .cts files using strip", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-strip.mjs", + fixturesPath("commonjs.cts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from CommonJS TypeScript \(\.cts\)!/); + strictEqual(result.code, 0); +}); + +test("should work with .cts files using transform", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-transform.mjs", + fixturesPath("commonjs.cts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from CommonJS TypeScript \(\.cts\)!/); + strictEqual(result.code, 0); +}); + +test("should work with esm typescript files using strip", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-strip.mjs", + fixturesPath("esm/esm.ts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from ESM TypeScript!/); + strictEqual(result.code, 0); +}); + +test("should work with esm typescript files using transform", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-transform.mjs", + fixturesPath("esm/esm.ts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from ESM TypeScript!/); + strictEqual(result.code, 0); +}); + +test("should work with .mts files using strip", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-strip.mjs", + fixturesPath("esm.mts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from ESM TypeScript!/); + strictEqual(result.code, 0); +}); + +test("should work with .mts files using transform", async () => { + const result = await spawnPromisified(process.execPath, [ + "--no-warnings", + "--import=./dist/register-transform.mjs", + fixturesPath("esm.mts"), + ]); + + strictEqual(result.stderr, ""); + match(result.stdout, /Hello from ESM TypeScript!/); + strictEqual(result.code, 0); +});