Raghavan Vidhyasagar
Raghavan Vidhyasagar

Reputation: 513

How to trigger the first build with esbuild.context

I sometimes just want to build instead of watching how to trigger the build without ctx.watch(). In this code I trigger the build by ctx.watch(), if the watch variable is false then run ctx.dispose() to close esbuild so it just builds without watching. There should be a good and proff way of doing it but idk how.

const ctx = await esbuild.context({
    entryPoints: ["./index.js"],
    outdir: outDir,
    write: false,
    plugins: [
        {
            name: "on-end",
            setup(build) {
                build.onEnd(async (result) => {
                    await processCode(result)
                })
            },
        },
    ],
})
await ctx.watch()
if (!watch) ctx.dispose()

Upvotes: 0

Views: 278

Answers (1)

darthgoldstein
darthgoldstein

Reputation: 112

Super late answer coming your way!

tldr: Just run esbuild.build(config) instead of creating a context.

It looks like you have a watch variable instantiated perhaps from an environment variable or passed in as an argument somehow. If watch is false then that probably means that this is a one-off build run and that you don't need a context at all. The context object is useful for continuing to run the build beyond the initial compilation. In this case where you don't need a context just run esbuild.build(config).

Upvotes: 0

Related Questions