Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make plugin event subscribers chainable #3745

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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