index.d.ts 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { SourceMapInput, EmittedAsset, AcornNode, Plugin, PluginContextMeta } from 'rollup';
  2. export { Plugin as RollupPlugin } from 'rollup';
  3. import { Compiler, WebpackPluginInstance } from 'webpack';
  4. export { Compiler as WebpackCompiler } from 'webpack';
  5. import { Plugin as Plugin$1 } from 'vite';
  6. export { Plugin as VitePlugin } from 'vite';
  7. import { Plugin as Plugin$2, PluginBuild } from 'esbuild';
  8. export { Plugin as EsbuildPlugin } from 'esbuild';
  9. import VirtualModulesPlugin from 'webpack-virtual-modules';
  10. type Thenable<T> = T | Promise<T>;
  11. type TransformResult = string | {
  12. code: string;
  13. map?: SourceMapInput | null;
  14. } | null | undefined;
  15. interface ExternalIdResult {
  16. id: string;
  17. external?: boolean;
  18. }
  19. interface UnpluginBuildContext {
  20. addWatchFile: (id: string) => void;
  21. emitFile: (emittedFile: EmittedAsset) => void;
  22. getWatchFiles: () => string[];
  23. parse: (input: string, options?: any) => AcornNode;
  24. }
  25. interface UnpluginOptions {
  26. name: string;
  27. enforce?: 'post' | 'pre' | undefined;
  28. buildStart?: (this: UnpluginBuildContext) => Promise<void> | void;
  29. buildEnd?: (this: UnpluginBuildContext) => Promise<void> | void;
  30. transform?: (this: UnpluginBuildContext & UnpluginContext, code: string, id: string) => Thenable<TransformResult>;
  31. load?: (this: UnpluginBuildContext & UnpluginContext, id: string) => Thenable<TransformResult>;
  32. resolveId?: (id: string, importer: string | undefined, options: {
  33. isEntry: boolean;
  34. }) => Thenable<string | ExternalIdResult | null | undefined>;
  35. watchChange?: (this: UnpluginBuildContext, id: string, change: {
  36. event: 'create' | 'update' | 'delete';
  37. }) => void;
  38. writeBundle?: (this: void) => Promise<void> | void;
  39. /**
  40. * Custom predicate function to filter modules to be loaded.
  41. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  42. */
  43. loadInclude?: (id: string) => boolean | null | undefined;
  44. /**
  45. * Custom predicate function to filter modules to be transformed.
  46. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  47. */
  48. transformInclude?: (id: string) => boolean | null | undefined;
  49. rollup?: Partial<Plugin>;
  50. webpack?: (compiler: Compiler) => void;
  51. vite?: Partial<Plugin$1>;
  52. esbuild?: {
  53. onResolveFilter?: RegExp;
  54. onLoadFilter?: RegExp;
  55. setup?: Plugin$2['setup'];
  56. };
  57. }
  58. interface ResolvedUnpluginOptions extends UnpluginOptions {
  59. __vfs?: VirtualModulesPlugin;
  60. __vfsModules?: Set<string>;
  61. __virtualModulePrefix: string;
  62. }
  63. type UnpluginFactory<UserOptions, Nested extends boolean = boolean> = (options: UserOptions, meta: UnpluginContextMeta) => Nested extends true ? Array<UnpluginOptions> : UnpluginOptions;
  64. type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions ? (options?: UserOptions) => Return : (options: UserOptions) => Return;
  65. interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
  66. rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<Plugin> : Plugin>;
  67. vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<Plugin$1> : Plugin$1>;
  68. webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>;
  69. esbuild: UnpluginFactoryOutput<UserOptions, Plugin$2>;
  70. raw: UnpluginFactory<UserOptions, Nested>;
  71. }
  72. type UnpluginContextMeta = Partial<PluginContextMeta> & ({
  73. framework: 'rollup' | 'vite';
  74. } | {
  75. framework: 'webpack';
  76. webpack: {
  77. compiler: Compiler;
  78. };
  79. } | {
  80. framework: 'esbuild';
  81. build?: PluginBuild;
  82. /** Set the host plugin name of esbuild when returning multiple plugins */
  83. esbuildHostName?: string;
  84. });
  85. interface UnpluginContext {
  86. error(message: any): void;
  87. warn(message: any): void;
  88. }
  89. declare module 'webpack' {
  90. interface Compiler {
  91. $unpluginContext: Record<string, ResolvedUnpluginOptions>;
  92. }
  93. }
  94. declare function createUnplugin<UserOptions, Nested extends boolean = boolean>(factory: UnpluginFactory<UserOptions, Nested>): UnpluginInstance<UserOptions, Nested>;
  95. export { ExternalIdResult, ResolvedUnpluginOptions, Thenable, TransformResult, UnpluginBuildContext, UnpluginContext, UnpluginContextMeta, UnpluginFactory, UnpluginFactoryOutput, UnpluginInstance, UnpluginOptions, createUnplugin };