From c41cfc92271dcaa3d6c8befda57a07bce711a4a2 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 19 Oct 2023 11:59:09 +0200 Subject: [PATCH] chore: import esbuild lazily (#1959) --- src/build/deps.ts | 13 ---------- src/build/esbuild.ts | 61 ++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/build/deps.ts b/src/build/deps.ts index 81cdb341..13dcd21d 100644 --- a/src/build/deps.ts +++ b/src/build/deps.ts @@ -7,17 +7,4 @@ export { } from "https://deno.land/std@0.193.0/path/mod.ts"; export { escape as regexpEscape } from "https://deno.land/std@0.193.0/regexp/escape.ts"; -// -- esbuild -- -// @deno-types="https://deno.land/x/esbuild@v0.19.4/mod.d.ts" -import * as esbuildWasm from "https://deno.land/x/esbuild@v0.19.4/wasm.js"; -import * as esbuildNative from "https://deno.land/x/esbuild@v0.19.4/mod.js"; -// @ts-ignore trust me -const esbuild: typeof esbuildWasm = - // deno-lint-ignore no-deprecated-deno-api - Deno.run === undefined || Deno.env.get("FRESH_ESBUILD_LOADER") === "portable" - ? esbuildWasm - : esbuildNative; -const esbuildWasmURL = new URL("./esbuild_v0.19.4.wasm", import.meta.url).href; -export { esbuild, esbuildWasm as esbuildTypes, esbuildWasmURL }; - export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.2/mod.ts"; diff --git a/src/build/esbuild.ts b/src/build/esbuild.ts index d82c6c4a..06af3b73 100644 --- a/src/build/esbuild.ts +++ b/src/build/esbuild.ts @@ -1,12 +1,9 @@ import { - denoPlugins, - esbuild, - esbuildTypes, - esbuildWasmURL, - fromFileUrl, - regexpEscape, - relative, -} from "./deps.ts"; + type BuildOptions, + type OnLoadOptions, + type Plugin, +} from "https://deno.land/x/esbuild@v0.19.4/mod.js"; +import { denoPlugins, fromFileUrl, regexpEscape, relative } from "./deps.ts"; import { Builder, BuildSnapshot } from "./mod.ts"; export interface EsbuildBuilderOptions { @@ -38,14 +35,34 @@ export class EsbuildBuilder implements Builder { async build(): Promise { const opts = this.#options; - try { - await initEsbuild(); + // Lazily initialize esbuild + // @deno-types="https://deno.land/x/esbuild@v0.19.4/mod.d.ts" + const esbuild = + // deno-lint-ignore no-deprecated-deno-api + Deno.run === undefined || + Deno.env.get("FRESH_ESBUILD_LOADER") === "portable" + ? await import("https://deno.land/x/esbuild@v0.19.4/wasm.js") + : await import("https://deno.land/x/esbuild@v0.19.4/mod.js"); + const esbuildWasmURL = + new URL("./esbuild_v0.19.4.wasm", import.meta.url).href; + + // deno-lint-ignore no-deprecated-deno-api + if (Deno.run === undefined) { + await esbuild.initialize({ + wasmURL: esbuildWasmURL, + worker: false, + }); + } else { + await esbuild.initialize({}); + } + + try { const absWorkingDir = opts.absoluteWorkingDir; // In dev-mode we skip identifier minification to be able to show proper // component names in Preact DevTools instead of single characters. - const minifyOptions: Partial = opts.dev + const minifyOptions: Partial = opts.dev ? { minifyIdentifiers: false, minifySyntax: true, @@ -104,7 +121,7 @@ export class EsbuildBuilder implements Builder { return new EsbuildSnapshot(files, dependencies); } finally { - stopEsbuild(); + esbuild.stop(); } } } @@ -114,26 +131,10 @@ const JSX_RUNTIME_MODE = { "react-jsx": "automatic", } as const; -async function initEsbuild() { - // deno-lint-ignore no-deprecated-deno-api - if (Deno.run === undefined) { - await esbuild.initialize({ - wasmURL: esbuildWasmURL, - worker: false, - }); - } else { - await esbuild.initialize({}); - } -} - -function stopEsbuild() { - esbuild.stop(); -} - -function buildIdPlugin(buildId: string): esbuildTypes.Plugin { +function buildIdPlugin(buildId: string): Plugin { const file = import.meta.resolve("../runtime/build_id.ts"); const url = new URL(file); - let options: esbuildTypes.OnLoadOptions; + let options: OnLoadOptions; if (url.protocol === "file:") { const path = fromFileUrl(url); const filter = new RegExp(`^${regexpEscape(path)}$`);