diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index f2cfd934..1c7eba97 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -1,15 +1,10 @@ -import * as esbuild from "esbuild-wasm"; +import { getEsbuild } from "@/lib/esbuild"; import { esmPlugin } from "./esmPlugin"; import { fsPlugin } from "./fsPlugin"; import type { JSRuntimeFS } from '@/lib/JSRuntime'; -const esbuildInitPromise = esbuild.initialize({ - worker: false, - wasmURL: "https://esm.sh/esbuild-wasm@0.25.8/esbuild.wasm", -}); - export interface BuildProjectOptions { fs: JSRuntimeFS; projectPath: string; @@ -20,7 +15,7 @@ export interface BuildProjectOptions { export async function buildProject( options: BuildProjectOptions, ): Promise> { - await esbuildInitPromise; + const esbuild = await getEsbuild(); const { fs, projectPath, domParser, target } = options; diff --git a/src/lib/esbuild.ts b/src/lib/esbuild.ts new file mode 100644 index 00000000..9ee838c4 --- /dev/null +++ b/src/lib/esbuild.ts @@ -0,0 +1,23 @@ +import type _ESBuild from "esbuild-wasm"; + +type ESBuild = typeof _ESBuild; + +let promise: Promise | undefined; + +export function getEsbuild(): Promise { + if (!promise) { + promise = loadEsbuild(); + } + return promise; +} + +async function loadEsbuild(): Promise { + const { default: esbuild } = await import("esbuild-wasm"); + + await esbuild.initialize({ + worker: false, + wasmURL: "https://esm.sh/esbuild-wasm@0.25.8/esbuild.wasm", + }); + + return esbuild; +} \ No newline at end of file