index.d.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import VirtualModulesPlugin from "webpack-virtual-modules";
  2. import { CompilationContext, JsPlugin } from "@farmfe/core";
  3. import { Compilation, Compiler as RspackCompiler, LoaderContext, RspackPluginInstance } from "@rspack/core";
  4. import { BuildOptions, Loader, Plugin as EsbuildPlugin, PluginBuild } from "esbuild";
  5. import { Plugin as RolldownPlugin } from "rolldown";
  6. import { AstNode, EmittedAsset, Plugin as RollupPlugin, PluginContextMeta, SourceMapInput } from "rollup";
  7. import { Plugin as UnloaderPlugin } from "unloader";
  8. import { Plugin as VitePlugin } from "vite";
  9. import { Compilation as Compilation$1, Compiler as WebpackCompiler, LoaderContext as LoaderContext$1, WebpackPluginInstance } from "webpack";
  10. //#region src/types.d.ts
  11. type Thenable<T> = T | Promise<T>;
  12. /**
  13. * Null or whatever
  14. */
  15. type Nullable<T> = T | null | undefined;
  16. /**
  17. * Array, or not yet
  18. */
  19. type Arrayable<T> = T | Array<T>;
  20. interface SourceMapCompact {
  21. file?: string;
  22. mappings: string;
  23. names: string[];
  24. sourceRoot?: string;
  25. sources: string[];
  26. // In magic-string v0.27.0, `sourcesContent` becomes nullable, while rollup haven't catch up yet
  27. sourcesContent?: (string | null)[];
  28. version: number;
  29. }
  30. type TransformResult = string | {
  31. code: string;
  32. map?: SourceMapInput | SourceMapCompact | null;
  33. } | null | undefined | void;
  34. interface ExternalIdResult {
  35. id: string;
  36. external?: boolean;
  37. }
  38. type NativeBuildContext = {
  39. framework: "webpack";
  40. compiler: WebpackCompiler;
  41. compilation?: Compilation$1;
  42. loaderContext?: LoaderContext$1<{
  43. unpluginName: string;
  44. }>;
  45. } | {
  46. framework: "esbuild";
  47. build: PluginBuild;
  48. } | {
  49. framework: "rspack";
  50. compiler: RspackCompiler;
  51. compilation: Compilation;
  52. loaderContext?: LoaderContext;
  53. } | {
  54. framework: "farm";
  55. context: CompilationContext;
  56. };
  57. interface UnpluginBuildContext {
  58. addWatchFile: (id: string) => void;
  59. emitFile: (emittedFile: EmittedAsset) => void;
  60. getWatchFiles: () => string[];
  61. parse: (input: string, options?: any) => AstNode;
  62. getNativeBuildContext?: () => NativeBuildContext;
  63. }
  64. type StringOrRegExp = string | RegExp;
  65. type FilterPattern = Arrayable<StringOrRegExp>;
  66. type StringFilter = FilterPattern | {
  67. include?: FilterPattern;
  68. exclude?: FilterPattern;
  69. };
  70. interface HookFilter {
  71. id?: StringFilter;
  72. code?: StringFilter;
  73. }
  74. interface ObjectHook<T extends HookFnMap[keyof HookFnMap], F extends keyof HookFilter> {
  75. filter?: Pick<HookFilter, F>;
  76. handler: T;
  77. }
  78. type Hook<T extends HookFnMap[keyof HookFnMap], F extends keyof HookFilter> = T | ObjectHook<T, F>;
  79. interface HookFnMap {
  80. // Build Hooks
  81. buildStart: (this: UnpluginBuildContext) => Thenable<void>;
  82. buildEnd: (this: UnpluginBuildContext) => Thenable<void>;
  83. transform: (this: UnpluginBuildContext & UnpluginContext, code: string, id: string) => Thenable<TransformResult>;
  84. load: (this: UnpluginBuildContext & UnpluginContext, id: string) => Thenable<TransformResult>;
  85. resolveId: (this: UnpluginBuildContext & UnpluginContext, id: string, importer: string | undefined, options: {
  86. isEntry: boolean;
  87. }) => Thenable<string | ExternalIdResult | null | undefined>;
  88. // Output Generation Hooks
  89. writeBundle: (this: void) => Thenable<void>;
  90. }
  91. interface UnpluginOptions {
  92. name: string;
  93. enforce?: "post" | "pre" | undefined;
  94. buildStart?: HookFnMap["buildStart"];
  95. buildEnd?: HookFnMap["buildEnd"];
  96. transform?: Hook<HookFnMap["transform"], "code" | "id">;
  97. load?: Hook<HookFnMap["load"], "id">;
  98. resolveId?: Hook<HookFnMap["resolveId"], "id">;
  99. writeBundle?: HookFnMap["writeBundle"];
  100. watchChange?: (this: UnpluginBuildContext, id: string, change: {
  101. event: "create" | "update" | "delete";
  102. }) => void;
  103. /**
  104. * Custom predicate function to filter modules to be loaded.
  105. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  106. *
  107. * @deprecated Use `load.filter` instead.
  108. */
  109. loadInclude?: (id: string) => boolean | null | undefined;
  110. /**
  111. * Custom predicate function to filter modules to be transformed.
  112. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  113. *
  114. * @deprecated Use `transform.filter` instead.
  115. */
  116. transformInclude?: (id: string) => boolean | null | undefined;
  117. // framework specify extends
  118. rollup?: Partial<RollupPlugin>;
  119. webpack?: (compiler: WebpackCompiler) => void;
  120. rspack?: (compiler: RspackCompiler) => void;
  121. vite?: Partial<VitePlugin>;
  122. unloader?: Partial<UnloaderPlugin>;
  123. rolldown?: Partial<RolldownPlugin>;
  124. esbuild?: {
  125. // using regexp in esbuild improves performance
  126. onResolveFilter?: RegExp;
  127. onLoadFilter?: RegExp;
  128. loader?: Loader | ((code: string, id: string) => Loader);
  129. setup?: (build: PluginBuild) => void | Promise<void>;
  130. config?: (options: BuildOptions) => void;
  131. };
  132. farm?: Partial<JsPlugin>;
  133. }
  134. interface ResolvedUnpluginOptions extends UnpluginOptions {
  135. // injected internal objects
  136. __vfs?: VirtualModulesPlugin;
  137. __vfsModules?: Map<string, Promise<string>> | Set<string>;
  138. __virtualModulePrefix: string;
  139. }
  140. type UnpluginFactory<UserOptions, Nested extends boolean = boolean> = (options: UserOptions, meta: UnpluginContextMeta) => Nested extends true ? Array<UnpluginOptions> : UnpluginOptions;
  141. type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions ? (options?: UserOptions) => Return : (options: UserOptions) => Return;
  142. interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
  143. rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>;
  144. vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>;
  145. rolldown: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RolldownPlugin> : RolldownPlugin>;
  146. webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>;
  147. rspack: UnpluginFactoryOutput<UserOptions, RspackPluginInstance>;
  148. esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>;
  149. unloader: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<UnloaderPlugin> : UnloaderPlugin>;
  150. farm: UnpluginFactoryOutput<UserOptions, JsPlugin>;
  151. raw: UnpluginFactory<UserOptions, Nested>;
  152. }
  153. type UnpluginContextMeta = Partial<PluginContextMeta> & ({
  154. framework: "rollup" | "vite" | "rolldown" | "farm" | "unloader";
  155. } | {
  156. framework: "webpack";
  157. webpack: {
  158. compiler: WebpackCompiler;
  159. };
  160. } | {
  161. framework: "esbuild";
  162. /** Set the host plugin name of esbuild when returning multiple plugins */
  163. esbuildHostName?: string;
  164. } | {
  165. framework: "rspack";
  166. rspack: {
  167. compiler: RspackCompiler;
  168. };
  169. });
  170. interface UnpluginMessage {
  171. name?: string;
  172. id?: string;
  173. message: string;
  174. stack?: string;
  175. code?: string;
  176. plugin?: string;
  177. pluginCode?: unknown;
  178. loc?: {
  179. column: number;
  180. file?: string;
  181. line: number;
  182. };
  183. meta?: any;
  184. }
  185. interface UnpluginContext {
  186. error: (message: string | UnpluginMessage) => void;
  187. warn: (message: string | UnpluginMessage) => void;
  188. }
  189. //#endregion
  190. //#region src/define.d.ts
  191. declare function createUnplugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions, Nested>;
  192. declare function createEsbuildPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["esbuild"];
  193. declare function createRollupPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["rollup"];
  194. declare function createVitePlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["vite"];
  195. declare function createRolldownPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["rolldown"];
  196. declare function createWebpackPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["webpack"];
  197. declare function createRspackPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["rspack"];
  198. declare function createFarmPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["farm"];
  199. declare function createUnloaderPlugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions>["unloader"];
  200. //#endregion
  201. 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 };