Skip to content

Commit a09646f

Browse files
fix: use detected format
1 parent fdea1dc commit a09646f

File tree

8 files changed

+131
-6
lines changed

8 files changed

+131
-6
lines changed

src/strip-loader.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ export async function load(
1313
) {
1414
const { format } = context;
1515
if (format?.endsWith("-typescript")) {
16-
// Use format 'module' so it returns the source as-is, without stripping the types.
17-
// Format 'commonjs' would not return the source for historical reasons.
1816
try {
1917
const { source } = await nextLoad(url, {
2018
...context,
21-
format: "module",
19+
format,
2220
});
2321
// biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source
2422
const { code } = transformSync(source!.toString(), {

src/transform-loader.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ export async function load(
1515
const { format } = context;
1616
if (format?.endsWith("-typescript")) {
1717
try {
18-
// Use format 'module' so it returns the source as-is, without stripping the types.
19-
// Format 'commonjs' would not return the source for historical reasons.
2018
const { source } = await nextLoad(url, {
2119
...context,
22-
format: "module",
20+
format,
2321
});
2422

2523
// biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source

test/fixtures/commonjs.cts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const message: string = "Hello from CommonJS TypeScript (.cts)!";
2+
3+
function greet(name: string): void {
4+
console.log(`${message} - ${name}`);
5+
}
6+
7+
module.exports = { greet };
8+
9+
greet("Test");

test/fixtures/commonjs.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const message: string = "Hello from CommonJS TypeScript!";
2+
3+
function greet(name: string): void {
4+
console.log(`${message} - ${name}`);
5+
}
6+
7+
module.exports = { greet };
8+
9+
greet("Test");

test/fixtures/esm.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const message: string = "Hello from ESM TypeScript!";
2+
3+
export function greet(name: string): void {
4+
console.log(`${message} - ${name}`);
5+
}
6+
7+
greet("Test");

test/fixtures/esm/esm.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const message: string = "Hello from ESM TypeScript!";
2+
3+
export function greet(name: string): void {
4+
console.log(`${message} - ${name}`);
5+
}
6+
7+
greet("Test");

test/fixtures/esm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/loader.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,99 @@ test("should throw syntax error for invalid typescript", async (t) => {
9999
match(result.stderr, /await isn't allowed in non-async function/);
100100
strictEqual(result.code, 1);
101101
});
102+
103+
test("should work with commonjs typescript files using strip", async () => {
104+
const result = await spawnPromisified(process.execPath, [
105+
"--no-warnings",
106+
"--import=./dist/register-strip.mjs",
107+
fixturesPath("commonjs.ts"),
108+
]);
109+
110+
strictEqual(result.stderr, "");
111+
match(result.stdout, /Hello from CommonJS TypeScript!/);
112+
strictEqual(result.code, 0);
113+
});
114+
115+
test("should work with commonjs typescript files using transform", async () => {
116+
const result = await spawnPromisified(process.execPath, [
117+
"--no-warnings",
118+
"--import=./dist/register-transform.mjs",
119+
fixturesPath("commonjs.ts"),
120+
]);
121+
122+
strictEqual(result.stderr, "");
123+
match(result.stdout, /Hello from CommonJS TypeScript!/);
124+
strictEqual(result.code, 0);
125+
});
126+
127+
test("should work with .cts files using strip", async () => {
128+
const result = await spawnPromisified(process.execPath, [
129+
"--no-warnings",
130+
"--import=./dist/register-strip.mjs",
131+
fixturesPath("commonjs.cts"),
132+
]);
133+
134+
strictEqual(result.stderr, "");
135+
match(result.stdout, /Hello from CommonJS TypeScript \(\.cts\)!/);
136+
strictEqual(result.code, 0);
137+
});
138+
139+
test("should work with .cts files using transform", async () => {
140+
const result = await spawnPromisified(process.execPath, [
141+
"--no-warnings",
142+
"--import=./dist/register-transform.mjs",
143+
fixturesPath("commonjs.cts"),
144+
]);
145+
146+
strictEqual(result.stderr, "");
147+
match(result.stdout, /Hello from CommonJS TypeScript \(\.cts\)!/);
148+
strictEqual(result.code, 0);
149+
});
150+
151+
test("should work with esm typescript files using strip", async () => {
152+
const result = await spawnPromisified(process.execPath, [
153+
"--no-warnings",
154+
"--import=./dist/register-strip.mjs",
155+
fixturesPath("esm/esm.ts"),
156+
]);
157+
158+
strictEqual(result.stderr, "");
159+
match(result.stdout, /Hello from ESM TypeScript!/);
160+
strictEqual(result.code, 0);
161+
});
162+
163+
test("should work with esm typescript files using transform", async () => {
164+
const result = await spawnPromisified(process.execPath, [
165+
"--no-warnings",
166+
"--import=./dist/register-transform.mjs",
167+
fixturesPath("esm/esm.ts"),
168+
]);
169+
170+
strictEqual(result.stderr, "");
171+
match(result.stdout, /Hello from ESM TypeScript!/);
172+
strictEqual(result.code, 0);
173+
});
174+
175+
test("should work with .mts files using strip", async () => {
176+
const result = await spawnPromisified(process.execPath, [
177+
"--no-warnings",
178+
"--import=./dist/register-strip.mjs",
179+
fixturesPath("esm.mts"),
180+
]);
181+
182+
strictEqual(result.stderr, "");
183+
match(result.stdout, /Hello from ESM TypeScript!/);
184+
strictEqual(result.code, 0);
185+
});
186+
187+
test("should work with .mts files using transform", async () => {
188+
const result = await spawnPromisified(process.execPath, [
189+
"--no-warnings",
190+
"--import=./dist/register-transform.mjs",
191+
fixturesPath("esm.mts"),
192+
]);
193+
194+
strictEqual(result.stderr, "");
195+
match(result.stdout, /Hello from ESM TypeScript!/);
196+
strictEqual(result.code, 0);
197+
});

0 commit comments

Comments
 (0)