esbuild-extra v0.4.0
esbuild-extra
The lighter alternative to @chialab/esbuild-rna that extends esbuild plugins with the following new features:
onTransformbuild hook for co-operative file transformation (with sourcemap support)onResolvedbuild hook for manipulation ofonResolveresultsloadbuild method for loading a file viaonLoadandonTransformhooksemitChunkandemitFilebuild methods for dynamically generated outputsresolveLocallyFirstbuild method for path resolution where local files are preferred, but plugin-resolved paths are used otherwise
That's it, for now!
This package is ~6x smaller than @chialab/esbuild-rna according to PackagePhobia (see ~1 MB here and ~6 MB here).
Usage
Apart from TypeScript typings, the only exports are wrapPlugins and getBuildExtensions.
The wrapPlugins function can be used by build tools to provide the features of esbuild-extra to every esbuild plugin. In this case, I also recommend importing esbuild-extra/global inan ambient .d.ts file to transparently propagate our PluginBuild extensions to downstream TypeScript users.
import { wrapPlugins } from 'esbuild-extra'
// It returns a new object where everything is identical except the plugins are wrapped!
esbuildOptions = wrapPlugins(esbuildOptions)If you're writing a standalone esbuild plugin, you'll want to use getBuildExtensions instead. Just note that your plugin won't play nicely with other plugins with onLoad hooks if they don't also use this package (but everything works fine if the other plugin isn't trying to load the files you want to transform).
import { getBuildExtensions } from 'esbuild-extra'
export default {
name: 'my-esbuild-plugin',
setup(build) {
const { onTransform } = getBuildExtensions(build, 'my-esbuild-plugin')
onTransform({ loaders: ['tsx'] }, async args => {
// Transform args.code and return a { code, map } object when you're ready!
})
},
}onTransform hooks
Here are some tips about the onTransform feature.
If no
namespaceis defined for anonTransformhook, it will be applied to every compatible file regardless of the file's namespace. You can usenamespace: "file"to only affect real files without a custom namespace.If an
onTransformhook is targeted at.jsfiles, it will also be applied to.jsx,.ts, and.tsxfiles, but only after anyonTransformhooks targeting those specific extensions are applied and also after the file is transpiled to plain JavaScript. Similarly, this works foronTransformhooks targeting.jsxfiles or.tsfiles (both applied to.tsxfiles after transpilation).
Prior art
License
MIT