Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Mar 21, 2024
1 parent 0ea8b0d commit c797716
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/build/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export {
toFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts";
export { escape as regexpEscape } from "https://deno.land/[email protected]/regexp/escape.ts";
export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.9.0/mod.ts";
export { denoPlugins } from "jsr:@luca/esbuild-deno-loader@0.10.3";
export { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
48 changes: 27 additions & 21 deletions src/build/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type BuildOptions,
type OnLoadOptions,
type Plugin,
} from "https://deno.land/x/esbuild@v0.19.11/mod.js";
} from "https://deno.land/x/esbuild@v0.20.2/mod.js";
import { denoPlugins, fromFileUrl, regexpEscape, relative } from "./deps.ts";
import { Builder, BuildSnapshot } from "./mod.ts";

Expand All @@ -23,6 +23,30 @@ export interface EsbuildBuilderOptions {
basePath?: string;
}

let esbuild: typeof import("https://deno.land/x/[email protected]/mod.js");

export async function initializeEsbuild() {
esbuild =
// deno-lint-ignore no-deprecated-deno-api
Deno.run === undefined ||
Deno.env.get("FRESH_ESBUILD_LOADER") === "portable"
? await import("https://deno.land/x/[email protected]/wasm.js")
: await import("https://deno.land/x/[email protected]/mod.js");
const esbuildWasmURL =
new URL("./esbuild_v0.20.2.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({});
}
return esbuild;
}

export class EsbuildBuilder implements Builder {
#options: EsbuildBuilderOptions;

Expand All @@ -34,25 +58,7 @@ export class EsbuildBuilder implements Builder {
const opts = this.#options;

// Lazily initialize esbuild
// @deno-types="https://deno.land/x/[email protected]/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/[email protected]/wasm.js")
: await import("https://deno.land/x/[email protected]/mod.js");
const esbuildWasmURL =
new URL("./esbuild_v0.19.11.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({});
}
const esbuild = await initializeEsbuild();

try {
const absWorkingDir = opts.absoluteWorkingDir;
Expand Down Expand Up @@ -129,7 +135,7 @@ export class EsbuildBuilder implements Builder {

return new EsbuildSnapshot(files, dependencies);
} finally {
esbuild.stop();
await esbuild.stop();
}
}
}
Expand Down
Binary file not shown.

0 comments on commit c797716

Please sign in to comment.