esbuild 0.11.15
-
Provide options for how to handle legal comments (#919)
A "legal comment" is considered to be any comment that contains
@licenseor@preserveor that starts with//!or/*!. These comments are preserved in output files by esbuild since that follows the intent of the original authors of the code.However, some people want to remove the automatically-generated license information before they distribute their code. To facilitate this, esbuild now provides several options for how to handle legal comments (via
--legal-comments=in the CLI andlegalCommentsin the JS API):none: Do not preserve any legal commentsinline: Preserve all statement-level legal commentseof: Move all statement-level legal comments to the end of the filelinked: Move all statement-level legal comments to a.LEGAL.txtfile and link to them with a commentexternal: Move all statement-level legal comments to a.LEGAL.txtfile but to not link to them
The default behavior is
eofwhen bundling andinlineotherwise. -
Add
onStartandonEndcallbacks to the plugin APIPlugins can now register callbacks to run when a build is started and ended:
const result = await esbuild.build({ ... incremental: true, plugins: [{ name: 'example', setup(build) { build.onStart(() => console.log('build started')) build.onEnd(result => console.log('build ended', result)) }, }], }) await result.rebuild()One benefit of
onStartandonEndis that they are run for all builds including rebuilds (relevant for incremental mode, watch mode, or serve mode), so they should be a good place to do work related to the build lifecycle.More details:
-
build.onStart()You should not use an
onStartcallback for initialization since it can be run multiple times. If you want to initialize something, just put your plugin initialization code directly inside thesetupfunction instead.The
onStartcallback can beasyncand can return a promise. However, the build does not wait for the promise to be resolved before starting, so a slowonStartcallback will not necessarily slow down the build. AllonStartcallbacks are also run concurrently, not consecutively. The returned promise is purely for error reporting, and matters when theonStartcallback needs to do an asynchronous operation that may fail. If your plugin needs to wait for an asynchronous task inonStartto complete before anyonResolveoronLoadcallbacks are run, you will need to have youronResolveoronLoadcallbacks block on that task fromonStart.Note that
onStartcallbacks do not have the ability to mutatebuild.initialOptions. The initial options can only be modified within thesetupfunction and are consumed once thesetupfunction returns. All rebuilds use the same initial options so the initial options are never re-consumed, and modifications tobuild.initialOptionsthat are done withinonStartare ignored. -
build.onEnd()All
onEndcallbacks are run in serial and each callback is given access to the final build result. It can modify the build result before returning and can delay the end of the build by returning a promise. If you want to be able to inspect the build graph, you should setbuild.initialOptions.metafile = trueand the build graph will be returned as themetafileproperty on the build result object.
-