diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index b2a63cc3..071173b6 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -41,6 +41,12 @@ export interface EsbuildPluginBuild extends PluginBuild { onTransform: (options: OnTransformOptions, callback: OnTransformCallback) => void } +interface Loader { + options?: OnLoadOptions + onLoadCb?: Parameters[1] + onTransformCb?: OnTransformCallback +} + export function getEsbuildPlugin>( factory: UnpluginFactory, ): UnpluginInstance['esbuild'] { @@ -51,12 +57,7 @@ export function getEsbuildPlugin>( const plugins = toArray(factory(userOptions!, meta)) const setupPlugins: EsbuildPlugin['setup'] = async (build) => { - const setup = buildSetup(meta) - interface Loader { - options?: OnLoadOptions - onLoadCb?: Parameters[1] - onTransformCb?: OnTransformCallback - } + const setup = buildSetup() const loaders: Loader[] = [] for (const plugin of plugins) { @@ -139,20 +140,18 @@ export function getEsbuildPlugin>( } } -function buildSetup(meta: UnpluginContextMeta & { framework: 'esbuild' }) { +function buildSetup() { return (plugin: UnpluginOptions): EsbuildPlugin['setup'] => { return (_build) => { - const build = meta.build = _build as EsbuildPluginBuild + const build = _build as EsbuildPluginBuild const context = createBuildContext(build) const { onStart, onEnd, onResolve, onLoad, onTransform, initialOptions } = build const onResolveFilter = plugin.esbuild?.onResolveFilter ?? /.*/ const onLoadFilter = plugin.esbuild?.onLoadFilter ?? /.*/ - const loader = plugin.esbuild?.loader ?? guessLoader - if (plugin.esbuild?.config) - plugin.esbuild.config.call(context, initialOptions) + plugin.esbuild?.config?.call(context, initialOptions) if (plugin.buildStart) onStart(() => plugin.buildStart!.call(context)) @@ -306,7 +305,7 @@ function buildSetup(meta: UnpluginContextMeta & { framework: 'esbuild' }) { } if (plugin.esbuild?.setup) - return plugin.esbuild.setup(meta.build) + return plugin.esbuild.setup(build) } } } diff --git a/src/types.ts b/src/types.ts index 793ca80a..d0938461 100644 --- a/src/types.ts +++ b/src/types.ts @@ -96,8 +96,8 @@ export interface UnpluginOptions { // using regexp in esbuild improves performance onResolveFilter?: RegExp onLoadFilter?: RegExp - setup?: (build: EsbuildPluginBuild) => void | Promise loader?: Loader | ((code: string, id: string) => Loader) + setup?: (build: EsbuildPluginBuild) => void | Promise config?: (options: BuildOptions) => void } farm?: Partial @@ -136,8 +136,6 @@ export type UnpluginContextMeta = Partial & ({ webpack: { compiler: WebpackCompiler } } | { framework: 'esbuild' - /** @deprecated {getNativeBuildContext} */ - build?: EsbuildPluginBuild /** Set the host plugin name of esbuild when returning multiple plugins */ esbuildHostName?: string } | {