-
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
aa480d2
commit 1f97f37
Showing
5 changed files
with
25 additions
and
24 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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import swc from "../lib/wasm.js"; | ||
import type { LoadHook } from "node:module"; | ||
import { readFile } from "node:fs/promises"; | ||
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.includes("typescript")) { | ||
const { source = "" } = await nextLoad(url, context); | ||
const { code } = transformSync(source.toString(), { mode: "transform" }); | ||
return { | ||
source: swc.transformSync(data).code, | ||
shortCircuit: true, // Skips bundled transpilation | ||
format: format.replace("-typescript", ""), | ||
source: code, | ||
shortCircuit: true, | ||
}; | ||
} | ||
return { source: await nextLoad(source, context) }; | ||
}; | ||
return { source: await 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 |
---|---|---|
@@ -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