rollup.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import * as ESTree from 'estree';
  2. import { EventEmitter } from 'events';
  3. export const VERSION: string;
  4. export interface RollupError extends RollupLogProps {
  5. parserError?: Error;
  6. stack?: string;
  7. watchFiles?: string[];
  8. }
  9. export interface RollupWarning extends RollupLogProps {
  10. chunkName?: string;
  11. cycle?: string[];
  12. exporter?: string;
  13. exportName?: string;
  14. guess?: string;
  15. importer?: string;
  16. missing?: string;
  17. modules?: string[];
  18. names?: string[];
  19. reexporter?: string;
  20. source?: string;
  21. sources?: string[];
  22. }
  23. export interface RollupLogProps {
  24. code?: string;
  25. frame?: string;
  26. hook?: string;
  27. id?: string;
  28. loc?: {
  29. column: number;
  30. file?: string;
  31. line: number;
  32. };
  33. message: string;
  34. name?: string;
  35. plugin?: string;
  36. pluginCode?: string;
  37. pos?: number;
  38. url?: string;
  39. }
  40. export type SourceMapSegment =
  41. | [number]
  42. | [number, number, number, number]
  43. | [number, number, number, number, number];
  44. export interface ExistingDecodedSourceMap {
  45. file?: string;
  46. mappings: SourceMapSegment[][];
  47. names: string[];
  48. sourceRoot?: string;
  49. sources: string[];
  50. sourcesContent?: string[];
  51. version: number;
  52. }
  53. export interface ExistingRawSourceMap {
  54. file?: string;
  55. mappings: string;
  56. names: string[];
  57. sourceRoot?: string;
  58. sources: string[];
  59. sourcesContent?: string[];
  60. version: number;
  61. }
  62. export type DecodedSourceMapOrMissing =
  63. | {
  64. mappings?: never;
  65. missing: true;
  66. plugin: string;
  67. }
  68. | ExistingDecodedSourceMap;
  69. export interface SourceMap {
  70. file: string;
  71. mappings: string;
  72. names: string[];
  73. sources: string[];
  74. sourcesContent: string[];
  75. version: number;
  76. toString(): string;
  77. toUrl(): string;
  78. }
  79. export type SourceMapInput = ExistingRawSourceMap | string | null | { mappings: '' };
  80. export interface SourceDescription {
  81. ast?: ESTree.Program;
  82. code: string;
  83. map?: SourceMapInput;
  84. moduleSideEffects?: boolean | null;
  85. syntheticNamedExports?: boolean;
  86. }
  87. export interface TransformSourceDescription extends SourceDescription {
  88. dependencies?: string[];
  89. }
  90. export interface TransformModuleJSON {
  91. ast: ESTree.Program;
  92. code: string;
  93. // note if plugins use new this.cache to opt-out auto transform cache
  94. customTransformCache: boolean;
  95. moduleSideEffects: boolean | null;
  96. originalCode: string;
  97. originalSourcemap: ExistingDecodedSourceMap | null;
  98. resolvedIds?: ResolvedIdMap;
  99. sourcemapChain: DecodedSourceMapOrMissing[];
  100. syntheticNamedExports: boolean | null;
  101. transformDependencies: string[];
  102. }
  103. export interface ModuleJSON extends TransformModuleJSON {
  104. dependencies: string[];
  105. id: string;
  106. transformFiles: EmittedFile[] | undefined;
  107. }
  108. export interface PluginCache {
  109. delete(id: string): boolean;
  110. get<T = any>(id: string): T;
  111. has(id: string): boolean;
  112. set<T = any>(id: string, value: T): void;
  113. }
  114. export interface MinimalPluginContext {
  115. meta: PluginContextMeta;
  116. }
  117. export interface EmittedAsset {
  118. fileName?: string;
  119. name?: string;
  120. source?: string | Buffer;
  121. type: 'asset';
  122. }
  123. export interface EmittedChunk {
  124. fileName?: string;
  125. id: string;
  126. name?: string;
  127. type: 'chunk';
  128. }
  129. export type EmittedFile = EmittedAsset | EmittedChunk;
  130. export type EmitAsset = (name: string, source?: string | Buffer) => string;
  131. export type EmitChunk = (id: string, options?: { name?: string }) => string;
  132. export type EmitFile = (emittedFile: EmittedFile) => string;
  133. export interface PluginContext extends MinimalPluginContext {
  134. addWatchFile: (id: string) => void;
  135. cache: PluginCache;
  136. /** @deprecated Use `this.emitFile` instead */
  137. emitAsset: EmitAsset;
  138. /** @deprecated Use `this.emitFile` instead */
  139. emitChunk: EmitChunk;
  140. emitFile: EmitFile;
  141. error: (err: RollupError | string, pos?: number | { column: number; line: number }) => never;
  142. /** @deprecated Use `this.getFileName` instead */
  143. getAssetFileName: (assetReferenceId: string) => string;
  144. /** @deprecated Use `this.getFileName` instead */
  145. getChunkFileName: (chunkReferenceId: string) => string;
  146. getFileName: (fileReferenceId: string) => string;
  147. getModuleInfo: (
  148. moduleId: string
  149. ) => {
  150. hasModuleSideEffects: boolean;
  151. id: string;
  152. importedIds: string[];
  153. isEntry: boolean;
  154. isExternal: boolean;
  155. };
  156. /** @deprecated Use `this.resolve` instead */
  157. isExternal: IsExternal;
  158. moduleIds: IterableIterator<string>;
  159. parse: (input: string, options: any) => ESTree.Program;
  160. resolve: (
  161. source: string,
  162. importer: string,
  163. options?: { skipSelf: boolean }
  164. ) => Promise<ResolvedId | null>;
  165. /** @deprecated Use `this.resolve` instead */
  166. resolveId: (source: string, importer: string) => Promise<string | null>;
  167. setAssetSource: (assetReferenceId: string, source: string | Buffer) => void;
  168. warn: (warning: RollupWarning | string, pos?: number | { column: number; line: number }) => void;
  169. /** @deprecated Use `this.addWatchFile` and the `watchChange` hook instead */
  170. watcher: EventEmitter;
  171. }
  172. export interface PluginContextMeta {
  173. rollupVersion: string;
  174. }
  175. export interface ResolvedId {
  176. external: boolean;
  177. id: string;
  178. moduleSideEffects: boolean;
  179. syntheticNamedExports: boolean;
  180. }
  181. export interface ResolvedIdMap {
  182. [key: string]: ResolvedId;
  183. }
  184. interface PartialResolvedId {
  185. external?: boolean;
  186. id: string;
  187. moduleSideEffects?: boolean | null;
  188. syntheticNamedExports?: boolean;
  189. }
  190. export type ResolveIdResult = string | false | null | undefined | PartialResolvedId;
  191. export type ResolveIdHook = (
  192. this: PluginContext,
  193. source: string,
  194. importer: string | undefined
  195. ) => Promise<ResolveIdResult> | ResolveIdResult;
  196. export type IsExternal = (
  197. source: string,
  198. importer: string,
  199. isResolved: boolean
  200. ) => boolean | null | undefined;
  201. export type IsPureModule = (id: string) => boolean | null | undefined;
  202. export type HasModuleSideEffects = (id: string, external: boolean) => boolean;
  203. type LoadResult = SourceDescription | string | null | undefined;
  204. export type LoadHook = (this: PluginContext, id: string) => Promise<LoadResult> | LoadResult;
  205. export type TransformResult = string | null | undefined | TransformSourceDescription;
  206. export type TransformHook = (
  207. this: PluginContext,
  208. code: string,
  209. id: string
  210. ) => Promise<TransformResult> | TransformResult;
  211. export type TransformChunkHook = (
  212. this: PluginContext,
  213. code: string,
  214. options: OutputOptions
  215. ) =>
  216. | Promise<{ code: string; map?: SourceMapInput } | null | undefined>
  217. | { code: string; map?: SourceMapInput }
  218. | null
  219. | undefined;
  220. export type RenderChunkHook = (
  221. this: PluginContext,
  222. code: string,
  223. chunk: RenderedChunk,
  224. options: OutputOptions
  225. ) =>
  226. | Promise<{ code: string; map?: SourceMapInput } | null>
  227. | { code: string; map?: SourceMapInput }
  228. | string
  229. | null;
  230. export type ResolveDynamicImportHook = (
  231. this: PluginContext,
  232. specifier: string | ESTree.Node,
  233. importer: string
  234. ) => Promise<ResolveIdResult> | ResolveIdResult;
  235. export type ResolveImportMetaHook = (
  236. this: PluginContext,
  237. prop: string | null,
  238. options: { chunkId: string; format: string; moduleId: string }
  239. ) => string | null | undefined;
  240. export type ResolveAssetUrlHook = (
  241. this: PluginContext,
  242. options: {
  243. assetFileName: string;
  244. chunkId: string;
  245. format: string;
  246. moduleId: string;
  247. relativeAssetPath: string;
  248. }
  249. ) => string | null | undefined;
  250. export type ResolveFileUrlHook = (
  251. this: PluginContext,
  252. options: {
  253. assetReferenceId: string | null;
  254. chunkId: string;
  255. chunkReferenceId: string | null;
  256. fileName: string;
  257. format: string;
  258. moduleId: string;
  259. referenceId: string;
  260. relativePath: string;
  261. }
  262. ) => string | null | undefined;
  263. export type AddonHook = string | ((this: PluginContext) => string | Promise<string>);
  264. /**
  265. * use this type for plugin annotation
  266. * @example
  267. * ```ts
  268. * interface Options {
  269. * ...
  270. * }
  271. * const myPlugin: PluginImpl<Options> = (options = {}) => { ... }
  272. * ```
  273. */
  274. export type PluginImpl<O extends object = object> = (options?: O) => Plugin;
  275. export interface OutputBundle {
  276. [fileName: string]: OutputAsset | OutputChunk;
  277. }
  278. export interface FilePlaceholder {
  279. type: 'placeholder';
  280. }
  281. export interface OutputBundleWithPlaceholders {
  282. [fileName: string]: OutputAsset | OutputChunk | FilePlaceholder;
  283. }
  284. interface OnGenerateOptions extends OutputOptions {
  285. bundle: OutputChunk;
  286. }
  287. interface OnWriteOptions extends OutputOptions {
  288. bundle: RollupBuild;
  289. }
  290. interface OutputPluginHooks {
  291. augmentChunkHash: (this: PluginContext, chunk: PreRenderedChunk) => string | void;
  292. generateBundle: (
  293. this: PluginContext,
  294. options: OutputOptions,
  295. bundle: OutputBundle,
  296. isWrite: boolean
  297. ) => void | Promise<void>;
  298. /** @deprecated Use `generateBundle` instead */
  299. ongenerate: (
  300. this: PluginContext,
  301. options: OnGenerateOptions,
  302. chunk: OutputChunk
  303. ) => void | Promise<void>;
  304. /** @deprecated Use `writeBundle` instead */
  305. onwrite: (
  306. this: PluginContext,
  307. options: OnWriteOptions,
  308. chunk: OutputChunk
  309. ) => void | Promise<void>;
  310. outputOptions: (this: PluginContext, options: OutputOptions) => OutputOptions | null | undefined;
  311. renderChunk: RenderChunkHook;
  312. renderError: (this: PluginContext, err?: Error) => Promise<void> | void;
  313. renderStart: (
  314. this: PluginContext,
  315. outputOptions: OutputOptions,
  316. inputOptions: InputOptions
  317. ) => Promise<void> | void;
  318. /** @deprecated Use `resolveFileUrl` instead */
  319. resolveAssetUrl: ResolveAssetUrlHook;
  320. resolveDynamicImport: ResolveDynamicImportHook;
  321. resolveFileUrl: ResolveFileUrlHook;
  322. /** @deprecated Use `renderChunk` instead */
  323. transformBundle: TransformChunkHook;
  324. /** @deprecated Use `renderChunk` instead */
  325. transformChunk: TransformChunkHook;
  326. writeBundle: (this: PluginContext, bundle: OutputBundle) => void | Promise<void>;
  327. }
  328. export interface PluginHooks extends OutputPluginHooks {
  329. buildEnd: (this: PluginContext, err?: Error) => Promise<void> | void;
  330. buildStart: (this: PluginContext, options: InputOptions) => Promise<void> | void;
  331. load: LoadHook;
  332. options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | null | undefined;
  333. resolveId: ResolveIdHook;
  334. resolveImportMeta: ResolveImportMetaHook;
  335. transform: TransformHook;
  336. watchChange: (id: string) => void;
  337. }
  338. interface OutputPluginValueHooks {
  339. banner: AddonHook;
  340. cacheKey: string;
  341. footer: AddonHook;
  342. intro: AddonHook;
  343. outro: AddonHook;
  344. }
  345. export interface Plugin extends Partial<PluginHooks>, Partial<OutputPluginValueHooks> {
  346. name: string;
  347. }
  348. export interface OutputPlugin extends Partial<OutputPluginHooks>, Partial<OutputPluginValueHooks> {
  349. name: string;
  350. }
  351. export interface TreeshakingOptions {
  352. annotations?: boolean;
  353. moduleSideEffects?: ModuleSideEffectsOption;
  354. propertyReadSideEffects?: boolean;
  355. /** @deprecated Use `moduleSideEffects` instead */
  356. pureExternalModules?: PureModulesOption;
  357. tryCatchDeoptimization?: boolean;
  358. unknownGlobalSideEffects?: boolean;
  359. }
  360. export type GetManualChunk = (id: string) => string | null | undefined;
  361. export type ExternalOption = string[] | IsExternal;
  362. export type PureModulesOption = boolean | string[] | IsPureModule;
  363. export type GlobalsOption = { [name: string]: string } | ((name: string) => string);
  364. export type InputOption = string | string[] | { [entryAlias: string]: string };
  365. export type ManualChunksOption = { [chunkAlias: string]: string[] } | GetManualChunk;
  366. export type ModuleSideEffectsOption = boolean | 'no-external' | string[] | HasModuleSideEffects;
  367. export interface InputOptions {
  368. acorn?: any;
  369. acornInjectPlugins?: Function[];
  370. cache?: false | RollupCache;
  371. chunkGroupingSize?: number;
  372. context?: string;
  373. experimentalCacheExpiry?: number;
  374. experimentalOptimizeChunks?: boolean;
  375. external?: ExternalOption;
  376. inlineDynamicImports?: boolean;
  377. input?: InputOption;
  378. manualChunks?: ManualChunksOption;
  379. moduleContext?: ((id: string) => string) | { [id: string]: string };
  380. onwarn?: WarningHandlerWithDefault;
  381. perf?: boolean;
  382. plugins?: Plugin[];
  383. preserveModules?: boolean;
  384. preserveSymlinks?: boolean;
  385. shimMissingExports?: boolean;
  386. strictDeprecations?: boolean;
  387. treeshake?: boolean | TreeshakingOptions;
  388. watch?: WatcherOptions;
  389. }
  390. export type ModuleFormat =
  391. | 'amd'
  392. | 'cjs'
  393. | 'commonjs'
  394. | 'es'
  395. | 'esm'
  396. | 'iife'
  397. | 'module'
  398. | 'system'
  399. | 'umd';
  400. export type OptionsPaths = Record<string, string> | ((id: string) => string);
  401. export interface OutputOptions {
  402. amd?: {
  403. define?: string;
  404. id?: string;
  405. };
  406. assetFileNames?: string;
  407. banner?: string | (() => string | Promise<string>);
  408. chunkFileNames?: string;
  409. compact?: boolean;
  410. // only required for bundle.write
  411. dir?: string;
  412. dynamicImportFunction?: string;
  413. entryFileNames?: string;
  414. esModule?: boolean;
  415. exports?: 'default' | 'named' | 'none' | 'auto';
  416. extend?: boolean;
  417. externalLiveBindings?: boolean;
  418. // only required for bundle.write
  419. file?: string;
  420. footer?: string | (() => string | Promise<string>);
  421. // this is optional at the base-level of RollupWatchOptions,
  422. // which extends from this interface through config merge
  423. format?: ModuleFormat;
  424. freeze?: boolean;
  425. globals?: GlobalsOption;
  426. hoistTransitiveImports?: boolean;
  427. indent?: boolean;
  428. interop?: boolean;
  429. intro?: string | (() => string | Promise<string>);
  430. name?: string;
  431. namespaceToStringTag?: boolean;
  432. noConflict?: boolean;
  433. outro?: string | (() => string | Promise<string>);
  434. paths?: OptionsPaths;
  435. plugins?: OutputPlugin[];
  436. preferConst?: boolean;
  437. sourcemap?: boolean | 'inline' | 'hidden';
  438. sourcemapExcludeSources?: boolean;
  439. sourcemapFile?: string;
  440. sourcemapPathTransform?: (sourcePath: string) => string;
  441. strict?: boolean;
  442. }
  443. export type WarningHandlerWithDefault = (
  444. warning: RollupWarning,
  445. defaultHandler: WarningHandler
  446. ) => void;
  447. export type WarningHandler = (warning: RollupWarning) => void;
  448. export interface SerializedTimings {
  449. [label: string]: [number, number, number];
  450. }
  451. export interface OutputAsset {
  452. fileName: string;
  453. /** @deprecated Accessing "isAsset" on files in the bundle is deprecated, please use "type === \'asset\'" instead */
  454. isAsset: true;
  455. source: string | Buffer;
  456. type: 'asset';
  457. }
  458. export interface RenderedModule {
  459. originalLength: number;
  460. removedExports: string[];
  461. renderedExports: string[];
  462. renderedLength: number;
  463. }
  464. export interface PreRenderedChunk {
  465. dynamicImports: string[];
  466. exports: string[];
  467. facadeModuleId: string | null;
  468. imports: string[];
  469. isDynamicEntry: boolean;
  470. isEntry: boolean;
  471. modules: {
  472. [id: string]: RenderedModule;
  473. };
  474. name: string;
  475. }
  476. export interface RenderedChunk extends PreRenderedChunk {
  477. fileName: string;
  478. }
  479. export interface OutputChunk extends RenderedChunk {
  480. code: string;
  481. map?: SourceMap;
  482. type: 'chunk';
  483. }
  484. export interface SerializablePluginCache {
  485. [key: string]: [number, any];
  486. }
  487. export interface RollupCache {
  488. modules: ModuleJSON[];
  489. plugins?: Record<string, SerializablePluginCache>;
  490. }
  491. export interface RollupOutput {
  492. output: [OutputChunk, ...(OutputChunk | OutputAsset)[]];
  493. }
  494. export interface RollupBuild {
  495. cache: RollupCache;
  496. generate: (outputOptions: OutputOptions) => Promise<RollupOutput>;
  497. getTimings?: () => SerializedTimings;
  498. watchFiles: string[];
  499. write: (options: OutputOptions) => Promise<RollupOutput>;
  500. }
  501. export interface RollupOptions extends InputOptions {
  502. // This is included for compatibility with config files but ignored by rollup.rollup
  503. output?: OutputOptions | OutputOptions[];
  504. }
  505. export function rollup(options: RollupOptions): Promise<RollupBuild>;
  506. // chokidar watch options
  507. export interface WatchOptions {
  508. alwaysStat?: boolean;
  509. atomic?: boolean | number;
  510. awaitWriteFinish?:
  511. | {
  512. pollInterval?: number;
  513. stabilityThreshold?: number;
  514. }
  515. | boolean;
  516. binaryInterval?: number;
  517. cwd?: string;
  518. depth?: number;
  519. disableGlobbing?: boolean;
  520. followSymlinks?: boolean;
  521. ignored?: any;
  522. ignoreInitial?: boolean;
  523. ignorePermissionErrors?: boolean;
  524. interval?: number;
  525. persistent?: boolean;
  526. useFsEvents?: boolean;
  527. usePolling?: boolean;
  528. }
  529. export interface WatcherOptions {
  530. chokidar?: boolean | WatchOptions;
  531. clearScreen?: boolean;
  532. exclude?: string[];
  533. include?: string[];
  534. }
  535. export interface RollupWatchOptions extends InputOptions {
  536. output?: OutputOptions | OutputOptions[];
  537. watch?: WatcherOptions;
  538. }
  539. interface TypedEventEmitter<T> {
  540. addListener<K extends keyof T>(event: K, listener: T[K]): this;
  541. emit<K extends keyof T>(event: K, ...args: any[]): boolean;
  542. eventNames(): Array<keyof T>;
  543. getMaxListeners(): number;
  544. listenerCount(type: keyof T): number;
  545. listeners<K extends keyof T>(event: K): Array<T[K]>;
  546. off<K extends keyof T>(event: K, listener: T[K]): this;
  547. on<K extends keyof T>(event: K, listener: T[K]): this;
  548. once<K extends keyof T>(event: K, listener: T[K]): this;
  549. prependListener<K extends keyof T>(event: K, listener: T[K]): this;
  550. prependOnceListener<K extends keyof T>(event: K, listener: T[K]): this;
  551. rawListeners<K extends keyof T>(event: K): Array<T[K]>;
  552. removeAllListeners<K extends keyof T>(event?: K): this;
  553. removeListener<K extends keyof T>(event: K, listener: T[K]): this;
  554. setMaxListeners(n: number): this;
  555. }
  556. export type RollupWatcherEvent =
  557. | { code: 'START' }
  558. | { code: 'BUNDLE_START'; input: InputOption; output: readonly string[] }
  559. | {
  560. code: 'BUNDLE_END';
  561. duration: number;
  562. input: InputOption;
  563. output: readonly string[];
  564. result: RollupBuild;
  565. }
  566. | { code: 'END' }
  567. | { code: 'ERROR'; error: RollupError };
  568. export interface RollupWatcher
  569. extends TypedEventEmitter<{
  570. change: (id: string) => void;
  571. event: (event: RollupWatcherEvent) => void;
  572. restart: () => void;
  573. }> {
  574. close(): void;
  575. }
  576. export function watch(configs: RollupWatchOptions[]): RollupWatcher;