denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.59k stars 650 forks source link

Reuse esbuild runtime #858

Open nestarz opened 2 years ago

nestarz commented 2 years ago

I would like to reuse the esbuild runtime because I can't init it twice or an error occurs. Can we expose the function, or check if esbuild has already been initialized ? If so, I can make a PR.

This fails because fresh may have already initialized the runtime, and if not, fresh will crash because it doesn't know I did it:

Deno.run === undefined
  ? await esbuild.initialize({
      wasmURL: "https://deno.land/x/esbuild@v0.14.51/esbuild.wasm",
      worker: false,
    })
  : await esbuild.initialize({})

Current hack:

import { esbuild } from "$fresh/src/server/deps.ts";
import { Bundler } from "$fresh/src/server/bundle.ts";

// @ts-ignore trust me
await new Bundler([], []).bundle().catch(() => false);

// ...
const { code } = await esbuild.transform("let x = 1;")
dagda1 commented 1 year ago

It would be really cool if there were a way to hook into the build. I want to use vanilla-extract which has an esbuild plugin.

@nestarz, where in fresh do you put your current hack?

lucacasonato commented 1 year ago

@nestarz Feel free to open a PR that prevents init if init has already happened.

nestarz commented 1 year ago

It would be really cool if there were a way to hook into the build. I want to use vanilla-extract which has an esbuild plugin.

@nestarz, where in fresh do you put your current hack?

@dagda1 I use this hack just before any use of esbuild in my scripts (so it is not in fresh codebase).

@lucacasonato I opened a PR that prevents error if init has already happened.

iuioiua commented 1 year ago

Am I right in understanding that esbuild.initialize() already only triggers once? If so, I'll close this issue and #980.

nestarz commented 1 year ago

Am I right in understanding that esbuild.initialize() already only triggers once? If so, I'll close this issue and #980.

It does, but if any other library or the user use esbuild and initialize it before, it will throw an unhandled exception caused by the code in fresh. Hence #980