Created
August 24, 2022 18:49
-
-
Save danny-andrews/375f5d393f0de02b164fd3ae3b0e248d to your computer and use it in GitHub Desktop.
Node ESM Loader for JSX
This file contains hidden or 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
import esbuild from "esbuild"; | |
import { readFile } from "node:fs/promises"; | |
import { fileURLToPath } from "node:url"; | |
export async function load(url, context, nextLoad) { | |
const source = await readFile(fileURLToPath(url), "utf-8"); | |
if (/\.jsx$/.test(url)) { | |
const transformedSource = await esbuild.transform(source, { | |
loader: "jsx", | |
}); | |
return { | |
format: "module", | |
source: transformedSource.code, | |
}; | |
} | |
return nextLoad(url, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment