@ 261027.7

260129.1 · cf1e0975490823b1cf772c65c100ee82b55502c3

fix: use detected format

reading declarations

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(), {

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

test/fixtures/commonjs.cts

new file mode 100644@@ -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");

test/fixtures/commonjs.ts

new file mode 100644@@ -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");

test/fixtures/esm.mts

new file mode 100644@@ -0,0 +1,7 @@+const message: string = "Hello from ESM TypeScript!";++export function greet(name: string): void {+    console.log(`${message} - ${name}`);+}++greet("Test");

test/fixtures/esm/esm.ts

new file mode 100644@@ -0,0 +1,7 @@+const message: string = "Hello from ESM TypeScript!";++export function greet(name: string): void {+    console.log(`${message} - ${name}`);+}++greet("Test");

test/fixtures/esm/package.json

new file mode 100644@@ -0,0 +1 @@+{}\ No newline at end of file

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);+});