Skip to content

Commit

Permalink
feat: make plugin event subscribers chainable
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Apr 30, 2024
1 parent 3045292 commit 265a4d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,13 +1292,15 @@ let handlePlugins = async (
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, 'onStart')
onStartCallbacks.push({ name: name!, callback, note: registeredNote })
plugin.onStart = true
return this
},

onEnd(callback) {
let registeredText = `This error came from the "onEnd" callback registered here:`
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, 'onEnd')
onEndCallbacks.push({ name: name!, callback, note: registeredNote })
plugin.onEnd = true
return this
},

onResolve(options, callback) {
Expand All @@ -1312,6 +1314,7 @@ let handlePlugins = async (
let id = nextCallbackID++
onResolveCallbacks[id] = { name: name!, callback, note: registeredNote }
plugin.onResolve.push({ id, filter: filter.source, namespace: namespace || '' })
return this
},

onLoad(options, callback) {
Expand All @@ -1325,10 +1328,12 @@ let handlePlugins = async (
let id = nextCallbackID++
onLoadCallbacks[id] = { name: name!, callback, note: registeredNote }
plugin.onLoad.push({ id, filter: filter.source, namespace: namespace || '' })
return this
},

onDispose(callback) {
onDisposeCallbacks.push(callback)
return this
},

esbuild: streamIn.esbuild,
Expand Down
10 changes: 5 additions & 5 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,22 @@ export interface PluginBuild {

/** Documentation: https://esbuild.github.io/plugins/#on-start */
onStart(callback: () =>
(OnStartResult | null | void | Promise<OnStartResult | null | void>)): void
(OnStartResult | null | void | Promise<OnStartResult | null | void>)): PluginBuild

/** Documentation: https://esbuild.github.io/plugins/#on-end */
onEnd(callback: (result: BuildResult) =>
(OnEndResult | null | void | Promise<OnEndResult | null | void>)): void
(OnEndResult | null | void | Promise<OnEndResult | null | void>)): PluginBuild

/** Documentation: https://esbuild.github.io/plugins/#on-resolve */
onResolve(options: OnResolveOptions, callback: (args: OnResolveArgs) =>
(OnResolveResult | null | undefined | Promise<OnResolveResult | null | undefined>)): void
(OnResolveResult | null | undefined | Promise<OnResolveResult | null | undefined>)): PluginBuild

/** Documentation: https://esbuild.github.io/plugins/#on-load */
onLoad(options: OnLoadOptions, callback: (args: OnLoadArgs) =>
(OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>)): void
(OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>)): PluginBuild

/** Documentation: https://esbuild.github.io/plugins/#on-dispose */
onDispose(callback: () => void): void
onDispose(callback: () => void): PluginBuild

// This is a full copy of the esbuild library in case you need it
esbuild: {
Expand Down
16 changes: 16 additions & 0 deletions scripts/plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3251,6 +3251,22 @@ let syncTests = {
await onDisposePromise
assert.strictEqual(onDisposeWasCalled, true)
},

async pluginSubscribersChainable({ esbuild }) {
let noop = () => {}
await esbuild.build({
write: false,
plugins: [{
name: 'x', setup(build) {
assert.strictEqual(build.onStart(noop), build)
assert.strictEqual(build.onEnd(noop), build)
assert.strictEqual(build.onLoad({ filter: /./ }, noop), build)
assert.strictEqual(build.onResolve({ filter: /./ }, noop), build)
assert.strictEqual(build.onDispose(noop), build)
}
}]
})
},
}

async function main() {
Expand Down

0 comments on commit 265a4d6

Please sign in to comment.