chore: import esbuild lazily (#1959)

This commit is contained in:
Marvin Hagemeister
2023-10-19 11:59:09 +02:00
committed by GitHub
parent 7896005329
commit c41cfc9227
2 changed files with 31 additions and 43 deletions
-13
View File
@@ -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";
+31 -30
View File
@@ -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<EsbuildSnapshot> {
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<esbuildTypes.BuildOptions> = opts.dev
const minifyOptions: Partial<BuildOptions> = 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)}$`);