feat(plugin)!: a plugin is a named object, not a function
BREAKING CHANGE: `Plugin` is now `{ name, mount(api) }`. `fn.name` was the only
source for the name that plugin:mounted reports, and it is empty for a returned
arrow, "plugin" for a const, and whatever a minifier decides. A failed mount now
names the plugin and keeps the original error as its cause, and two plugins with
the same name fail createApp.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+152
-6
No files matched your search
+12
-1
@@ -600,8 +600,19 @@ function buildTeardownRegistry(
|
||||
function mountPlugins(plugins: Plugin[] | undefined, bus: Bus, registry: Registry, teardown: TeardownRegistry): void {
|
||||
const extraSteps: ScopeNode[] = [];
|
||||
const scope: ScopeApi = { add: (node) => extraSteps.push(node) };
|
||||
// The name is the plugin's identity in `plugin:mounted` and in a mount failure, so two of them
|
||||
// make both ambiguous. Two instances of one plugin are legitimate — they differ by `provides`,
|
||||
// and the convention derives the name from it (.specs/2026-09-15, rule 2).
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const plugin of plugins ?? []) mountPlugin(plugin, bus, scope, (fn) => teardown.add(fn));
|
||||
for (const plugin of plugins ?? []) {
|
||||
if (seen.has(plugin.name)) {
|
||||
throw new Error(`two plugins are named "${plugin.name}" — pass a different \`provides\` to one of them`);
|
||||
}
|
||||
|
||||
seen.add(plugin.name);
|
||||
mountPlugin(plugin, bus, scope, (fn) => teardown.add(fn));
|
||||
}
|
||||
|
||||
for (const scopeNode of extraSteps) {
|
||||
const node = { name: scopeNode.name, needs: scopeNode.needs, provides: scopeNode.provides, origin: 'plugin' };
|
||||
|
||||
+22
-4
@@ -37,8 +37,18 @@ export interface PluginApi {
|
||||
*/
|
||||
onShutdown(fn: TeardownFn): void;
|
||||
}
|
||||
/** A plugin: a function that wires itself up through the provided PluginApi. */
|
||||
export type Plugin = (api: PluginApi) => void;
|
||||
/**
|
||||
* A plugin: a named object that wires itself up through the provided PluginApi.
|
||||
*
|
||||
* The name is a field rather than `fn.name` because `fn.name` cannot be relied on: an arrow
|
||||
* returned straight from a factory has `""`, `const plugin = …` reports `"plugin"`, and a minifier
|
||||
* rewrites both. The name is what `plugin:mounted` reports and what a failed mount is blamed on, so
|
||||
* it has to be the author's word, not the bundler's.
|
||||
*/
|
||||
export interface Plugin {
|
||||
readonly name: string;
|
||||
mount(api: PluginApi): void;
|
||||
}
|
||||
|
||||
/** Runs a plugin against a restricted API, then emits `plugin:mounted`. */
|
||||
export function mountPlugin(plugin: Plugin, bus: Bus, scope: ScopeApi, onShutdown: (fn: TeardownFn) => void): void {
|
||||
@@ -53,6 +63,14 @@ export function mountPlugin(plugin: Plugin, bus: Bus, scope: ScopeApi, onShutdow
|
||||
// before could delay the process. It cannot delay it without bound — `close()`'s deadline still
|
||||
// caps the whole shutdown.
|
||||
const api: PluginApi = { bus: { on: bus.on.bind(bus) }, scope, onShutdown };
|
||||
plugin(api);
|
||||
bus.emit('plugin:mounted', { name: plugin.name || 'anonymous' });
|
||||
|
||||
try {
|
||||
plugin.mount(api);
|
||||
} catch (error) {
|
||||
// Without the name this surfaces as somebody else's stack trace: mounting happens inside
|
||||
// createApp, so the application sees a throw from a line it never wrote.
|
||||
throw new Error(`plugin "${plugin.name}" failed to mount: ${(error as Error).message}`, { cause: error });
|
||||
}
|
||||
|
||||
bus.emit('plugin:mounted', { name: plugin.name });
|
||||
}
|
||||
Reference in new issue
Block a user