Created
September 3, 2022 08:16
-
-
Save lawrencecchen/d63fc97cbafcbf21f6c10e2a49620128 to your computer and use it in GitHub Desktop.
react hook wrapper for swc
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 { 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