You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose to extend the plugin api with an onProcessed callback, that is triggered when a file was just bundled and before it is written to disk.
The purpose of this new callback would be to change the bundled content of a file before writing it to disk. This could be used to run third party tools like compressors, or manglers. Additionally it can be utilized to add custom header/footer comments, that depend on context information like the name of the source file, or where the file will be written to, when the build completes.
Example usage of the API
esbuild.mjs
import * as esbuild from 'esbuild'
let exampleOnProcessedCallback = {
name: 'example',
setup(build) {
build.onProcessed({ filter: /\.ts$/ }, async (args) => {
return {
contents: `${args.contents}\n// this file ${args.buildFilename} will be outputted to ${args.buildPath}. It was generated from ${args.sourceFilename}, that is located at ${args.sourcePath}.`,
}
})
},
}
await esbuild.build({
entryPoints: ['src/a.ts', 'src/b.ts'],
bundle: true,
outdir: 'out',
plugins: [exampleOnProcessedCallback],
})
source files
src/a.ts:
const a: string = "log from file a";
console.log(a)
src/b.ts:
const b: string = "log from file b";
console.log(b)
output files, after running esbuild.mjs
out/a.js:
const a = "log from file a";
console.log(a)
// this file a.js will be outputted to out/a.js. It was generated from a.ts, that is located at src/a.ts.
out/b.js:
const b: string = "log from file b";
console.log(b)
// this file b.js will be outputted to out/b.js. It was generated from b.ts, that is located at src/b.ts.
on-processed API
on-processed options
The on-processed callback shares the same options as the on-load callback, thus giving the user the option to pick which files the callback should be called on.
interface OnProcessedArgs {
contents: string; // the bundled file as a string
buildFilename: string; // filename of the build artifact
buildPath: string; // relative path to the build artifact
sourceFilename: string; // filename of the source file
sourcePath: string; // relative path to the source file
// additional arguments...
}
on-processed results
interface OnProcessedResult {
contents: string; // string that will be written into build artifact
// additional result properties...
}
Remarks
I have no knowledge of the esbuild internals, or Go as a language. This means, I am in need of your help as a community, both to further develop this proposal and to hopefully get it added to esbuild.
This is the first serious proposal I ever wrote. I'd appreciate it, if you could give some feedback, how good, or bad I did and what I should work on.
The text was updated successfully, but these errors were encountered:
I propose to extend the plugin api with an
onProcessed
callback, that is triggered when a file was just bundled and before it is written to disk.The purpose of this new callback would be to change the bundled content of a file before writing it to disk. This could be used to run third party tools like compressors, or manglers. Additionally it can be utilized to add custom header/footer comments, that depend on context information like the name of the source file, or where the file will be written to, when the build completes.
Example usage of the API
esbuild.mjs
source files
src/a.ts
:src/b.ts
:output files, after running
esbuild.mjs
out/a.js
:out/b.js
:on-processed API
on-processed options
The on-processed callback shares the same options as the on-load callback, thus giving the user the option to pick which files the callback should be called on.
on-processed arguments
on-processed results
Remarks
The text was updated successfully, but these errors were encountered: