v0.17.1
-
Make it possible to cancel a build (#2725)
The context object introduced in version 0.17.0 has a new
cancel()
method. You can use it to cancel a long-running build so that you can start a new one without needing to wait for the previous one to finish. When this happens, the previous build should always have at least one error and have no output files (i.e. it will be a failed build).Using it might look something like this:
-
JS:
let ctx = await esbuild.context({ // ... }) let rebuildWithTimeLimit = timeLimit => { let timeout = setTimeout(() => ctx.cancel(), timeLimit) return ctx.rebuild().finally(() => clearTimeout(timeout)) } let build = await rebuildWithTimeLimit(500)
-
Go:
ctx, err := api.Context(api.BuildOptions{ // ... }) if err != nil { return } rebuildWithTimeLimit := func(timeLimit time.Duration) api.BuildResult { t := time.NewTimer(timeLimit) go func() { <-t.C ctx.Cancel() }() result := ctx.Rebuild() t.Stop() return result } build := rebuildWithTimeLimit(500 * time.Millisecond)
This API is a quick implementation and isn't maximally efficient, so the build may continue to do some work for a little bit before stopping. For example, I have added stop points between each top-level phase of the bundler and in the main module graph traversal loop, but I haven't added fine-grained stop points within the internals of the linker. How quickly esbuild stops can be improved in future releases. This means you'll want to wait for
cancel()
and/or the previousrebuild()
to finish (i.e. await the returned promise in JavaScript) before starting a new build, otherwiserebuild()
will give you the just-canceled build that still hasn't ended yet. Note thatonEnd
callbacks will still be run regardless of whether or not the build was canceled. -
-
Fix server-sent events without
servedir
(#2827)The server-sent events for live reload were incorrectly using
servedir
to calculate the path to modified output files. This means events couldn't be sent whenservedir
wasn't specified. This release uses the internal output directory (which is always present) instead ofservedir
(which might be omitted), so live reload should now work whenservedir
is not specified. -
Custom entry point output paths now work with the
copy
loader (#2828)Entry points can optionally provide custom output paths to change the path of the generated output file. For example,
esbuild foo=abc.js bar=xyz.js --outdir=out
generates the filesout/foo.js
andout/bar.js
. However, this previously didn't work when using thecopy
loader due to an oversight. This bug has been fixed. For example, you can now doesbuild foo=abc.html bar=xyz.html --outdir=out --loader:.html=copy
to generate the filesout/foo.html
andout/bar.html
. -
The JS API can now take an array of objects (#2828)
Previously it was not possible to specify two entry points with the same custom output path using the JS API, although it was possible to do this with the Go API and the CLI. This will not cause a collision if both entry points use different extensions (e.g. if one uses
.js
and the other uses.css
). You can now pass the JS API an array of objects to work around this API limitation:// The previous API didn't let you specify duplicate output paths let result = await esbuild.build({ entryPoints: { // This object literal contains a duplicate key, so one is ignored 'dist': 'foo.js', 'dist': 'bar.css', }, }) // You can now specify duplicate output paths as an array of objects let result = await esbuild.build({ entryPoints: [ { in: 'foo.js', out: 'dist' }, { in: 'bar.css', out: 'dist' }, ], })