Skip to content

Commit

Permalink
feat!: drop meta.build for esbuild
Browse files Browse the repository at this point in the history
Use `this.getNativeBuildContext` instead
  • Loading branch information
sxzz committed Nov 29, 2024
1 parent b35b88b commit c980632
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 11 additions & 12 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface EsbuildPluginBuild extends PluginBuild {
onTransform: (options: OnTransformOptions, callback: OnTransformCallback) => void
}

interface Loader {
options?: OnLoadOptions
onLoadCb?: Parameters<PluginBuild['onLoad']>[1]
onTransformCb?: OnTransformCallback
}

export function getEsbuildPlugin<UserOptions = Record<string, never>>(
factory: UnpluginFactory<UserOptions>,
): UnpluginInstance<UserOptions>['esbuild'] {
Expand All @@ -51,12 +57,7 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
const plugins = toArray(factory(userOptions!, meta))

const setupPlugins: EsbuildPlugin['setup'] = async (build) => {
const setup = buildSetup(meta)
interface Loader {
options?: OnLoadOptions
onLoadCb?: Parameters<PluginBuild['onLoad']>[1]
onTransformCb?: OnTransformCallback
}
const setup = buildSetup()
const loaders: Loader[] = []

for (const plugin of plugins) {
Expand Down Expand Up @@ -139,20 +140,18 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
}
}

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))
Expand Down Expand Up @@ -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)
}
}
}
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export interface UnpluginOptions {
// using regexp in esbuild improves performance
onResolveFilter?: RegExp
onLoadFilter?: RegExp
setup?: (build: EsbuildPluginBuild) => void | Promise<void>
loader?: Loader | ((code: string, id: string) => Loader)
setup?: (build: EsbuildPluginBuild) => void | Promise<void>
config?: (options: BuildOptions) => void
}
farm?: Partial<FarmPlugin>
Expand Down Expand Up @@ -136,8 +136,6 @@ export type UnpluginContextMeta = Partial<RollupContextMeta> & ({
webpack: { compiler: WebpackCompiler }
} | {
framework: 'esbuild'
/** @deprecated {getNativeBuildContext} */
build?: EsbuildPluginBuild
/** Set the host plugin name of esbuild when returning multiple plugins */
esbuildHostName?: string
} | {
Expand Down

0 comments on commit c980632

Please sign in to comment.