Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/strip-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(), {
Expand Down
4 changes: 1 addition & 3 deletions src/transform-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/commonjs.cts
Original file line number Diff line number Diff line change
@@ -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");
9 changes: 9 additions & 0 deletions test/fixtures/commonjs.ts
Original file line number Diff line number Diff line change
@@ -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");
7 changes: 7 additions & 0 deletions test/fixtures/esm.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const message: string = "Hello from ESM TypeScript!";

export function greet(name: string): void {
console.log(`${message} - ${name}`);
}

greet("Test");
7 changes: 7 additions & 0 deletions test/fixtures/esm/esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const message: string = "Hello from ESM TypeScript!";

export function greet(name: string): void {
console.log(`${message} - ${name}`);
}

greet("Test");
1 change: 1 addition & 0 deletions test/fixtures/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
96 changes: 96 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading