diff --git a/src/api.ts b/src/api.ts index e0901bc..44679e8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -2,6 +2,7 @@ import module from 'node:module' import process from 'node:process' import { createHooks } from './hooks' import { createRpc } from './rpc' +import { sharedPluginContext } from './utils/context' import { debug } from './utils/debug' import type { PluginContext } from './plugin' import type { UnloaderConfig } from './utils/config' @@ -40,6 +41,7 @@ export function registerSync(inlineConfig?: UnloaderConfig): () => void { const { init, resolve, load, deactivate } = createHooks() const context: PluginContext = { + ...sharedPluginContext, log: (message) => console.info(message), debug, } diff --git a/src/hooks.ts b/src/hooks.ts index 61be3c5..4c82318 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -67,12 +67,12 @@ export function createHooks(): { for (const plugin of config.plugins || []) { const { handler } = normalizePluginHook(plugin, 'options') - config = handler?.(config) || config + config = handler?.call(context, config) || config } for (const plugin of config.plugins || []) { const { handler } = normalizePluginHook(plugin, 'buildStart') - await handler?.(context) + await handler?.call(context) context.debug(`loaded plugin: %s`, plugin.name) } @@ -111,7 +111,10 @@ export function createHooks(): { } const result = await handler?.call( - { resolve: isAsync ? resolve : resolve.sync }, + { + resolve: isAsync ? resolve : resolve.sync, + ...pluginContext, + }, id, urlToPath(context.parentURL), { @@ -172,7 +175,7 @@ export function createHooks(): { continue } - const loadResult = await handler?.(id, { + const loadResult = await handler?.call(pluginContext!, id, { format: context.format, conditions: context.conditions, attributes: context.importAttributes, @@ -214,7 +217,7 @@ export function createHooks(): { } const transformResult: ModuleSource | LoadResult | FalsyValue = - await handler?.(code, id, { + await handler?.call(pluginContext!, code, id, { format: result.format, conditions: context.conditions, attributes: context.importAttributes, diff --git a/src/plugin.ts b/src/plugin.ts index 4946da7..7e04042 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -44,6 +44,8 @@ export interface PluginContext { port?: MessagePort log: (message: any, transferList?: Transferable[]) => void debug: (...args: any[]) => void + error: (message: string | Error) => never + meta: { unloaderVersion: string } } export type ConditionalAwaitable = @@ -75,21 +77,26 @@ export type HookFilterExtension> = : {} export interface FunctionPluginHooks { - options: (config: UnloaderConfig) => UnloaderConfig | FalsyValue - buildStart: (context: PluginContext) => ConditionalAwaitable + options: ( + this: PluginContext, + config: UnloaderConfig, + ) => UnloaderConfig | FalsyValue + buildStart: (this: PluginContext) => ConditionalAwaitable resolveId: ( - this: { resolve: ResolveFn }, + this: PluginContext & { resolve: ResolveFn }, source: string, importer: string | undefined, options: ResolveMeta, ) => ConditionalAwaitable load: ( + this: PluginContext, id: string, options: ResolveMeta & { format: ModuleFormat | (string & {}) | null | undefined }, ) => ConditionalAwaitable transform: ( + this: PluginContext, code: ModuleSource, id: string, options: ResolveMeta & { format: string | null | undefined }, diff --git a/src/utils/context.ts b/src/utils/context.ts new file mode 100644 index 0000000..7e6106e --- /dev/null +++ b/src/utils/context.ts @@ -0,0 +1,11 @@ +import { version } from '../../package.json' +import type { PluginContext } from '../plugin' + +export const sharedPluginContext: Pick = { + error: (message) => { + throw typeof message === 'string' ? new Error(message) : message + }, + meta: { + unloaderVersion: version, + }, +} diff --git a/src/worker.ts b/src/worker.ts index 7a86176..47434b3 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,6 +1,7 @@ import process from 'node:process' import { createBirpc, type BirpcReturn } from 'birpc' import { createHooks } from './hooks' +import { sharedPluginContext } from './utils/context' import type { PluginContext } from './plugin' import type { MainFunctions } from './rpc' import type { UnloaderConfig } from './utils/config' @@ -31,6 +32,7 @@ export const initialize: InitializeHook = async (_data: Data) => { }) const context: PluginContext = { + ...sharedPluginContext, port, log: (...args) => rpc.log(...args), debug: (...args) => rpc.debug(...args), diff --git a/tests/demo.ts b/tests/demo.ts index ec724a5..b2620b9 100644 --- a/tests/demo.ts +++ b/tests/demo.ts @@ -1,5 +1,6 @@ // @ts-check +import assert from 'node:assert' import path from 'node:path' import { readFile } from '@quansync/fs' import MagicString from 'magic-string' @@ -14,10 +15,12 @@ export function demoPlugin(): Plugin { return { name: 'demo-plugin', options(config) { + assert(typeof this.meta.unloaderVersion === 'string') config.sourcemap = true }, - buildStart(_context) { - context = _context + buildStart() { + // eslint-disable-next-line @typescript-eslint/no-this-alias + context = this context.log('[plugin] build start') }, resolveId: {