Skip to content

Instantly share code, notes, and snippets.

@danny-andrews
Created August 24, 2022 18:49
Show Gist options
  • Save danny-andrews/375f5d393f0de02b164fd3ae3b0e248d to your computer and use it in GitHub Desktop.
Save danny-andrews/375f5d393f0de02b164fd3ae3b0e248d to your computer and use it in GitHub Desktop.
Node ESM Loader for JSX
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