index.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 } from 'esbuild';
  8. export { Plugin as EsbuildPlugin } from 'esbuild';
  9. import VirtualModulesPlugin from 'webpack-virtual-modules';
  10. declare type Thenable<T> = T | Promise<T>;
  11. declare type TransformResult = string | {
  12. code: string;
  13. map?: SourceMapInput | null;
  14. } | null | undefined;
  15. declare type 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. /**
  39. * Custom predicate function to filter modules to be loaded.
  40. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  41. */
  42. loadInclude?: (id: string) => boolean | null | undefined;
  43. /**
  44. * Custom predicate function to filter modules to be transformed.
  45. * When omitted, all modules will be included (might have potential perf impact on Webpack).
  46. */
  47. transformInclude?: (id: string) => boolean | null | undefined;
  48. rollup?: Partial<Plugin>;
  49. webpack?: (compiler: Compiler) => void;
  50. vite?: Partial<Plugin$1>;
  51. esbuild?: {
  52. onResolveFilter?: RegExp;
  53. onLoadFilter?: RegExp;
  54. setup?: Plugin$2['setup'];
  55. };
  56. }
  57. interface ResolvedUnpluginOptions extends UnpluginOptions {
  58. __vfs?: VirtualModulesPlugin;
  59. __vfsModules?: Set<string>;
  60. __virtualModulePrefix: string;
  61. }
  62. declare type UnpluginFactory<UserOptions> = (options: UserOptions, meta: UnpluginContextMeta) => UnpluginOptions;
  63. declare type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions ? (options?: UserOptions) => Return : (options: UserOptions) => Return;
  64. interface UnpluginInstance<UserOptions> {
  65. rollup: UnpluginFactoryOutput<UserOptions, Plugin>;
  66. webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>;
  67. vite: UnpluginFactoryOutput<UserOptions, Plugin$1>;
  68. esbuild: UnpluginFactoryOutput<UserOptions, Plugin$2>;
  69. raw: UnpluginFactory<UserOptions>;
  70. }
  71. interface UnpluginContextMeta extends Partial<PluginContextMeta> {
  72. framework: 'rollup' | 'vite' | 'webpack' | 'esbuild';
  73. webpack?: {
  74. compiler: Compiler;
  75. };
  76. }
  77. interface UnpluginContext {
  78. error(message: any): void;
  79. warn(message: any): void;
  80. }
  81. declare module 'webpack' {
  82. interface Compiler {
  83. $unpluginContext: Record<string, ResolvedUnpluginOptions>;
  84. }
  85. }
  86. declare function createUnplugin<UserOptions>(factory: UnpluginFactory<UserOptions>): UnpluginInstance<UserOptions>;
  87. export { ExternalIdResult, ResolvedUnpluginOptions, Thenable, TransformResult, UnpluginBuildContext, UnpluginContext, UnpluginContextMeta, UnpluginFactory, UnpluginFactoryOutput, UnpluginInstance, UnpluginOptions, createUnplugin };