import { CompilationContext, JsPlugin } from "@farmfe/core"; import { Compilation, Compiler as RspackCompiler, LoaderContext, RspackPluginInstance } from "@rspack/core"; import { BuildOptions, Loader, Plugin as EsbuildPlugin, PluginBuild } from "esbuild"; import { Plugin as RolldownPlugin } from "rolldown"; import { AstNode, EmittedAsset, Plugin as RollupPlugin, PluginContextMeta, SourceMapInput } from "rollup"; import { Plugin as UnloaderPlugin } from "unloader"; import { Plugin as VitePlugin } from "vite"; import { Compilation as Compilation$1, Compiler as WebpackCompiler, LoaderContext as LoaderContext$1, WebpackPluginInstance } from "webpack"; import VirtualModulesPlugin from "webpack-virtual-modules"; //#region src/types.d.ts type Thenable = T | Promise; /** * Null or whatever */ type Nullable = T | null | undefined; /** * Array, or not yet */ type Arrayable = T | Array; interface SourceMapCompact { file?: string; mappings: string; names: string[]; sourceRoot?: string; sources: string[]; // In magic-string v0.27.0, `sourcesContent` becomes nullable, while rollup haven't catch up yet sourcesContent?: (string | null)[]; version: number; } type TransformResult = string | { code: string; map?: SourceMapInput | SourceMapCompact | null; } | null | undefined | void; interface ExternalIdResult { id: string; external?: boolean; } type NativeBuildContext = { framework: "webpack"; compiler: WebpackCompiler; compilation?: Compilation$1; loaderContext?: LoaderContext$1<{ unpluginName: string; }>; } | { framework: "esbuild"; build: PluginBuild; } | { framework: "rspack"; compiler: RspackCompiler; compilation: Compilation; loaderContext?: LoaderContext; } | { framework: "farm"; context: CompilationContext; }; interface UnpluginBuildContext { addWatchFile: (id: string) => void; emitFile: (emittedFile: EmittedAsset) => void; getWatchFiles: () => string[]; parse: (input: string, options?: any) => AstNode; getNativeBuildContext?: () => NativeBuildContext; } type StringOrRegExp = string | RegExp; type FilterPattern = Arrayable; type StringFilter = FilterPattern | { include?: FilterPattern; exclude?: FilterPattern; }; interface HookFilter { id?: StringFilter; code?: StringFilter; } interface ObjectHook { filter?: Pick; handler: T; } type Hook = T | ObjectHook; interface HookFnMap { // Build Hooks buildStart: (this: UnpluginBuildContext) => Thenable; buildEnd: (this: UnpluginBuildContext) => Thenable; transform: (this: UnpluginBuildContext & UnpluginContext, code: string, id: string) => Thenable; load: (this: UnpluginBuildContext & UnpluginContext, id: string) => Thenable; resolveId: (this: UnpluginBuildContext & UnpluginContext, id: string, importer: string | undefined, options: { isEntry: boolean; }) => Thenable; // Output Generation Hooks writeBundle: (this: void) => Thenable; } interface UnpluginOptions { name: string; enforce?: "post" | "pre" | undefined; buildStart?: HookFnMap["buildStart"]; buildEnd?: HookFnMap["buildEnd"]; transform?: Hook; load?: Hook; resolveId?: Hook; writeBundle?: HookFnMap["writeBundle"]; watchChange?: (this: UnpluginBuildContext, id: string, change: { event: "create" | "update" | "delete"; }) => void; /** * Custom predicate function to filter modules to be loaded. * When omitted, all modules will be included (might have potential perf impact on Webpack). * * @deprecated Use `load.filter` instead. */ loadInclude?: (id: string) => boolean | null | undefined; /** * Custom predicate function to filter modules to be transformed. * When omitted, all modules will be included (might have potential perf impact on Webpack). * * @deprecated Use `transform.filter` instead. */ transformInclude?: (id: string) => boolean | null | undefined; // framework specify extends rollup?: Partial; webpack?: (compiler: WebpackCompiler) => void; rspack?: (compiler: RspackCompiler) => void; vite?: Partial; unloader?: Partial; rolldown?: Partial; esbuild?: { // using regexp in esbuild improves performance onResolveFilter?: RegExp; onLoadFilter?: RegExp; loader?: Loader | ((code: string, id: string) => Loader); setup?: (build: PluginBuild) => void | Promise; config?: (options: BuildOptions) => void; }; farm?: Partial; } interface ResolvedUnpluginOptions extends UnpluginOptions { // injected internal objects __vfs?: VirtualModulesPlugin; __vfsModules?: Map> | Set; __virtualModulePrefix: string; } type UnpluginFactory = (options: UserOptions, meta: UnpluginContextMeta) => Nested extends true ? Array : UnpluginOptions; type UnpluginFactoryOutput = undefined extends UserOptions ? (options?: UserOptions) => Return : (options: UserOptions) => Return; interface UnpluginInstance { rollup: UnpluginFactoryOutput : RollupPlugin>; vite: UnpluginFactoryOutput : VitePlugin>; rolldown: UnpluginFactoryOutput : RolldownPlugin>; webpack: UnpluginFactoryOutput; rspack: UnpluginFactoryOutput; esbuild: UnpluginFactoryOutput; unloader: UnpluginFactoryOutput : UnloaderPlugin>; farm: UnpluginFactoryOutput; raw: UnpluginFactory; } type UnpluginContextMeta = Partial & ({ framework: "rollup" | "vite" | "rolldown" | "farm" | "unloader"; } | { framework: "webpack"; webpack: { compiler: WebpackCompiler; }; } | { framework: "esbuild"; /** Set the host plugin name of esbuild when returning multiple plugins */ esbuildHostName?: string; } | { framework: "rspack"; rspack: { compiler: RspackCompiler; }; }); interface UnpluginMessage { name?: string; id?: string; message: string; stack?: string; code?: string; plugin?: string; pluginCode?: unknown; loc?: { column: number; file?: string; line: number; }; meta?: any; } interface UnpluginContext { error: (message: string | UnpluginMessage) => void; warn: (message: string | UnpluginMessage) => void; } //#endregion //#region src/define.d.ts declare function createUnplugin(factory: UnpluginFactory): UnpluginInstance; declare function createEsbuildPlugin(factory: UnpluginFactory): UnpluginInstance["esbuild"]; declare function createRollupPlugin(factory: UnpluginFactory): UnpluginInstance["rollup"]; declare function createVitePlugin(factory: UnpluginFactory): UnpluginInstance["vite"]; declare function createRolldownPlugin(factory: UnpluginFactory): UnpluginInstance["rolldown"]; declare function createWebpackPlugin(factory: UnpluginFactory): UnpluginInstance["webpack"]; declare function createRspackPlugin(factory: UnpluginFactory): UnpluginInstance["rspack"]; declare function createFarmPlugin(factory: UnpluginFactory): UnpluginInstance["farm"]; declare function createUnloaderPlugin(factory: UnpluginFactory): UnpluginInstance["unloader"]; //#endregion export { Arrayable, EsbuildPlugin, ExternalIdResult, FilterPattern, Hook, HookFilter, HookFnMap, NativeBuildContext, Nullable, ObjectHook, ResolvedUnpluginOptions, RolldownPlugin, RollupPlugin, RspackCompiler, RspackPluginInstance, SourceMapCompact, StringFilter, StringOrRegExp, Thenable, TransformResult, UnloaderPlugin, UnpluginBuildContext, UnpluginContext, UnpluginContextMeta, UnpluginFactory, UnpluginFactoryOutput, UnpluginInstance, UnpluginMessage, UnpluginOptions, VitePlugin, WebpackCompiler, WebpackPluginInstance, createEsbuildPlugin, createFarmPlugin, createRolldownPlugin, createRollupPlugin, createRspackPlugin, createUnloaderPlugin, createUnplugin, createVitePlugin, createWebpackPlugin };