diff --git a/README.md b/README.md index 5c2f8cc4..021cd981 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,10 @@ export const unplugin = createUnplugin((options: UserOptions, meta) => { // from file extension (eg: .js -> "js", .jsx -> 'jsx') // loader?: (Loader | (code: string, id: string) => Loader) + // Read and/or modify build.initialOptions + // [https://esbuild.github.io/plugins/#build-options] + // config?: (initialOptions: BuildOptions) => void + // Or you can completely replace the setup logic // setup?: EsbuildPlugin.setup, }, diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index b1605159..3201b220 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -29,6 +29,9 @@ export function getEsbuildPlugin>( const context: UnpluginBuildContext = createEsbuildContext(initialOptions) + if (plugin.esbuild?.config) + plugin.esbuild.config.call(context, initialOptions) + if (plugin.buildStart) onStart(() => plugin.buildStart!.call(context)) diff --git a/src/types.ts b/src/types.ts index 6d3a0621..1a9be710 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ import type { AstNode, EmittedAsset, PluginContextMeta as RollupContextMeta, Plugin as RollupPlugin, SourceMapInput } from 'rollup' import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack' import type { Plugin as VitePlugin } from 'vite' -import type { Plugin as EsbuildPlugin, Loader, PluginBuild } from 'esbuild' +import type { BuildOptions, Plugin as EsbuildPlugin, Loader, PluginBuild } from 'esbuild' import type { Compiler as RspackCompiler, RspackPluginInstance } from '@rspack/core' import type VirtualModulesPlugin from 'webpack-virtual-modules' @@ -76,6 +76,7 @@ export interface UnpluginOptions { onLoadFilter?: RegExp setup?: EsbuildPlugin['setup'] loader?: Loader | ((code: string, id: string) => Loader) + config?: (options: BuildOptions) => void } }