-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95b0e0e
commit a712b1a
Showing
8 changed files
with
73 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { copy } = require("esbuild-plugin-copy"); | ||
const esbuild = require("esbuild"); | ||
|
||
esbuild.build({ | ||
entryPoints: ["src/index.ts"], | ||
bundle: true, | ||
platform: "node", | ||
target: "node20", | ||
outdir: "dist", | ||
plugins: [ | ||
copy({ | ||
assets: { | ||
from: ["./src/register/register.mjs"], | ||
to: ["."], | ||
}, | ||
}), | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import swc from "../lib/wasm.js"; | ||
import type { LoadHook } from "node:module"; | ||
import { readFile } from "node:fs/promises"; | ||
import assert from "node:assert"; | ||
import type { LoadFnOutput, LoadHookContext } from "node:module"; | ||
import { transformSync } from "./index.ts"; | ||
|
||
export const load: LoadHook = async (source, context, nextLoad) => { | ||
if (context.format?.includes("typescript")) { | ||
const data = await readFile(source, "utf8"); | ||
type NextLoad = ( | ||
url: string, | ||
context?: LoadHookContext, | ||
) => LoadFnOutput | Promise<LoadFnOutput>; | ||
|
||
export async function load( | ||
url: string, | ||
context: LoadHookContext, | ||
nextLoad: NextLoad, | ||
) { | ||
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. | ||
const { source } = await nextLoad(url, { | ||
...context, | ||
format: "module", | ||
}); | ||
if (source == null) | ||
throw new Error("Source code cannot be null or undefined"); | ||
const { code } = transformSync(source.toString(), { mode: "strip-only" }); | ||
return { | ||
source: swc.transformSync(data).code, | ||
shortCircuit: true, // Skips bundled transpilation | ||
format: format.replace("-typescript", ""), | ||
source: code, | ||
}; | ||
} | ||
return { source: await nextLoad(source, context) }; | ||
}; | ||
return nextLoad(url, context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { register } from "node:module"; | ||
|
||
register("./index.js", import.meta.url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
enum Foo { | ||
A = "Hello, TypeScript!" | ||
A = "Hello, TypeScript!", | ||
} | ||
console.log(Foo.A); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters