Skip to content

Instantly share code, notes, and snippets.

@lawrencecchen
Created September 3, 2022 08:16
Show Gist options
  • Save lawrencecchen/d63fc97cbafcbf21f6c10e2a49620128 to your computer and use it in GitHub Desktop.
Save lawrencecchen/d63fc97cbafcbf21f6c10e2a49620128 to your computer and use it in GitHub Desktop.
react hook wrapper for swc
import { useEffect, useState } from "react";
import * as swc from "@swc/wasm-web";
let __initialized = false;
type SWC = Omit<typeof swc, "default" | "initSync">;
let __swc: SWC;
export default function useSwc() {
const [initialized, setInitialized] = useState(__initialized);
const [_swc, setSwc] = useState<SWC>(__swc);
useEffect(() => {
async function importAndRunSwcOnMount() {
await swc.default();
setInitialized(true);
__initialized = true;
setSwc(swc);
__swc = swc;
}
if (!initialized) {
importAndRunSwcOnMount();
}
}, []);
if (initialized) {
return { initialized: true, ..._swc };
}
return {
initialized,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment