main.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. export type Platform = 'browser' | 'node' | 'neutral';
  2. export type Format = 'iife' | 'cjs' | 'esm';
  3. export type Loader = 'base64' | 'binary' | 'copy' | 'css' | 'dataurl' | 'default' | 'empty' | 'file' | 'js' | 'json' | 'jsx' | 'text' | 'ts' | 'tsx';
  4. export type LogLevel = 'verbose' | 'debug' | 'info' | 'warning' | 'error' | 'silent';
  5. export type Charset = 'ascii' | 'utf8';
  6. export type Drop = 'console' | 'debugger';
  7. interface CommonOptions {
  8. /** Documentation: https://esbuild.github.io/api/#sourcemap */
  9. sourcemap?: boolean | 'linked' | 'inline' | 'external' | 'both';
  10. /** Documentation: https://esbuild.github.io/api/#legal-comments */
  11. legalComments?: 'none' | 'inline' | 'eof' | 'linked' | 'external';
  12. /** Documentation: https://esbuild.github.io/api/#source-root */
  13. sourceRoot?: string;
  14. /** Documentation: https://esbuild.github.io/api/#sources-content */
  15. sourcesContent?: boolean;
  16. /** Documentation: https://esbuild.github.io/api/#format */
  17. format?: Format;
  18. /** Documentation: https://esbuild.github.io/api/#global-name */
  19. globalName?: string;
  20. /** Documentation: https://esbuild.github.io/api/#target */
  21. target?: string | string[];
  22. /** Documentation: https://esbuild.github.io/api/#supported */
  23. supported?: Record<string, boolean>;
  24. /** Documentation: https://esbuild.github.io/api/#platform */
  25. platform?: Platform;
  26. /** Documentation: https://esbuild.github.io/api/#mangle-props */
  27. mangleProps?: RegExp;
  28. /** Documentation: https://esbuild.github.io/api/#mangle-props */
  29. reserveProps?: RegExp;
  30. /** Documentation: https://esbuild.github.io/api/#mangle-props */
  31. mangleQuoted?: boolean;
  32. /** Documentation: https://esbuild.github.io/api/#mangle-props */
  33. mangleCache?: Record<string, string | false>;
  34. /** Documentation: https://esbuild.github.io/api/#drop */
  35. drop?: Drop[];
  36. /** Documentation: https://esbuild.github.io/api/#minify */
  37. minify?: boolean;
  38. /** Documentation: https://esbuild.github.io/api/#minify */
  39. minifyWhitespace?: boolean;
  40. /** Documentation: https://esbuild.github.io/api/#minify */
  41. minifyIdentifiers?: boolean;
  42. /** Documentation: https://esbuild.github.io/api/#minify */
  43. minifySyntax?: boolean;
  44. /** Documentation: https://esbuild.github.io/api/#charset */
  45. charset?: Charset;
  46. /** Documentation: https://esbuild.github.io/api/#tree-shaking */
  47. treeShaking?: boolean;
  48. /** Documentation: https://esbuild.github.io/api/#ignore-annotations */
  49. ignoreAnnotations?: boolean;
  50. /** Documentation: https://esbuild.github.io/api/#jsx */
  51. jsx?: 'transform' | 'preserve' | 'automatic';
  52. /** Documentation: https://esbuild.github.io/api/#jsx-factory */
  53. jsxFactory?: string;
  54. /** Documentation: https://esbuild.github.io/api/#jsx-fragment */
  55. jsxFragment?: string;
  56. /** Documentation: https://esbuild.github.io/api/#jsx-import-source */
  57. jsxImportSource?: string;
  58. /** Documentation: https://esbuild.github.io/api/#jsx-development */
  59. jsxDev?: boolean;
  60. /** Documentation: https://esbuild.github.io/api/#jsx-side-effects */
  61. jsxSideEffects?: boolean;
  62. /** Documentation: https://esbuild.github.io/api/#define */
  63. define?: { [key: string]: string };
  64. /** Documentation: https://esbuild.github.io/api/#pure */
  65. pure?: string[];
  66. /** Documentation: https://esbuild.github.io/api/#keep-names */
  67. keepNames?: boolean;
  68. /** Documentation: https://esbuild.github.io/api/#color */
  69. color?: boolean;
  70. /** Documentation: https://esbuild.github.io/api/#log-level */
  71. logLevel?: LogLevel;
  72. /** Documentation: https://esbuild.github.io/api/#log-limit */
  73. logLimit?: number;
  74. /** Documentation: https://esbuild.github.io/api/#log-override */
  75. logOverride?: Record<string, LogLevel>;
  76. }
  77. export interface BuildOptions extends CommonOptions {
  78. /** Documentation: https://esbuild.github.io/api/#bundle */
  79. bundle?: boolean;
  80. /** Documentation: https://esbuild.github.io/api/#splitting */
  81. splitting?: boolean;
  82. /** Documentation: https://esbuild.github.io/api/#preserve-symlinks */
  83. preserveSymlinks?: boolean;
  84. /** Documentation: https://esbuild.github.io/api/#outfile */
  85. outfile?: string;
  86. /** Documentation: https://esbuild.github.io/api/#metafile */
  87. metafile?: boolean;
  88. /** Documentation: https://esbuild.github.io/api/#outdir */
  89. outdir?: string;
  90. /** Documentation: https://esbuild.github.io/api/#outbase */
  91. outbase?: string;
  92. /** Documentation: https://esbuild.github.io/api/#external */
  93. external?: string[];
  94. /** Documentation: https://esbuild.github.io/api/#packages */
  95. packages?: 'external';
  96. /** Documentation: https://esbuild.github.io/api/#alias */
  97. alias?: Record<string, string>;
  98. /** Documentation: https://esbuild.github.io/api/#loader */
  99. loader?: { [ext: string]: Loader };
  100. /** Documentation: https://esbuild.github.io/api/#resolve-extensions */
  101. resolveExtensions?: string[];
  102. /** Documentation: https://esbuild.github.io/api/#main-fields */
  103. mainFields?: string[];
  104. /** Documentation: https://esbuild.github.io/api/#conditions */
  105. conditions?: string[];
  106. /** Documentation: https://esbuild.github.io/api/#write */
  107. write?: boolean;
  108. /** Documentation: https://esbuild.github.io/api/#allow-overwrite */
  109. allowOverwrite?: boolean;
  110. /** Documentation: https://esbuild.github.io/api/#tsconfig */
  111. tsconfig?: string;
  112. /** Documentation: https://esbuild.github.io/api/#out-extension */
  113. outExtension?: { [ext: string]: string };
  114. /** Documentation: https://esbuild.github.io/api/#public-path */
  115. publicPath?: string;
  116. /** Documentation: https://esbuild.github.io/api/#entry-names */
  117. entryNames?: string;
  118. /** Documentation: https://esbuild.github.io/api/#chunk-names */
  119. chunkNames?: string;
  120. /** Documentation: https://esbuild.github.io/api/#asset-names */
  121. assetNames?: string;
  122. /** Documentation: https://esbuild.github.io/api/#inject */
  123. inject?: string[];
  124. /** Documentation: https://esbuild.github.io/api/#banner */
  125. banner?: { [type: string]: string };
  126. /** Documentation: https://esbuild.github.io/api/#footer */
  127. footer?: { [type: string]: string };
  128. /** Documentation: https://esbuild.github.io/api/#incremental */
  129. incremental?: boolean;
  130. /** Documentation: https://esbuild.github.io/api/#entry-points */
  131. entryPoints?: string[] | Record<string, string>;
  132. /** Documentation: https://esbuild.github.io/api/#stdin */
  133. stdin?: StdinOptions;
  134. /** Documentation: https://esbuild.github.io/plugins/ */
  135. plugins?: Plugin[];
  136. /** Documentation: https://esbuild.github.io/api/#working-directory */
  137. absWorkingDir?: string;
  138. /** Documentation: https://esbuild.github.io/api/#node-paths */
  139. nodePaths?: string[]; // The "NODE_PATH" variable from Node.js
  140. /** Documentation: https://esbuild.github.io/api/#watch */
  141. watch?: boolean | WatchMode;
  142. }
  143. export interface WatchMode {
  144. onRebuild?: (error: BuildFailure | null, result: BuildResult | null) => void;
  145. }
  146. export interface StdinOptions {
  147. contents: string | Uint8Array;
  148. resolveDir?: string;
  149. sourcefile?: string;
  150. loader?: Loader;
  151. }
  152. export interface Message {
  153. id: string;
  154. pluginName: string;
  155. text: string;
  156. location: Location | null;
  157. notes: Note[];
  158. /**
  159. * Optional user-specified data that is passed through unmodified. You can
  160. * use this to stash the original error, for example.
  161. */
  162. detail: any;
  163. }
  164. export interface Note {
  165. text: string;
  166. location: Location | null;
  167. }
  168. export interface Location {
  169. file: string;
  170. namespace: string;
  171. /** 1-based */
  172. line: number;
  173. /** 0-based, in bytes */
  174. column: number;
  175. /** in bytes */
  176. length: number;
  177. lineText: string;
  178. suggestion: string;
  179. }
  180. export interface OutputFile {
  181. path: string;
  182. /** "text" as bytes */
  183. contents: Uint8Array;
  184. /** "contents" as text (changes automatically with "contents") */
  185. readonly text: string;
  186. }
  187. export interface BuildInvalidate {
  188. (): Promise<BuildIncremental>;
  189. dispose(): void;
  190. }
  191. export interface BuildIncremental extends BuildResult {
  192. rebuild: BuildInvalidate;
  193. }
  194. export interface BuildResult {
  195. errors: Message[];
  196. warnings: Message[];
  197. /** Only when "write: false" */
  198. outputFiles?: OutputFile[];
  199. /** Only when "incremental: true" */
  200. rebuild?: BuildInvalidate;
  201. /** Only when "watch: true" */
  202. stop?: () => void;
  203. /** Only when "metafile: true" */
  204. metafile?: Metafile;
  205. /** Only when "mangleCache" is present */
  206. mangleCache?: Record<string, string | false>;
  207. }
  208. export interface BuildFailure extends Error {
  209. errors: Message[];
  210. warnings: Message[];
  211. }
  212. /** Documentation: https://esbuild.github.io/api/#serve-arguments */
  213. export interface ServeOptions {
  214. port?: number;
  215. host?: string;
  216. servedir?: string;
  217. onRequest?: (args: ServeOnRequestArgs) => void;
  218. }
  219. export interface ServeOnRequestArgs {
  220. remoteAddress: string;
  221. method: string;
  222. path: string;
  223. status: number;
  224. /** The time to generate the response, not to send it */
  225. timeInMS: number;
  226. }
  227. /** Documentation: https://esbuild.github.io/api/#serve-return-values */
  228. export interface ServeResult {
  229. port: number;
  230. host: string;
  231. wait: Promise<void>;
  232. stop: () => void;
  233. }
  234. export interface TransformOptions extends CommonOptions {
  235. tsconfigRaw?: string | {
  236. compilerOptions?: {
  237. alwaysStrict?: boolean,
  238. importsNotUsedAsValues?: 'remove' | 'preserve' | 'error',
  239. jsx?: 'react' | 'react-jsx' | 'react-jsxdev' | 'preserve',
  240. jsxFactory?: string,
  241. jsxFragmentFactory?: string,
  242. jsxImportSource?: string,
  243. preserveValueImports?: boolean,
  244. target?: string,
  245. useDefineForClassFields?: boolean,
  246. },
  247. };
  248. sourcefile?: string;
  249. loader?: Loader;
  250. banner?: string;
  251. footer?: string;
  252. }
  253. export interface TransformResult {
  254. code: string;
  255. map: string;
  256. warnings: Message[];
  257. /** Only when "mangleCache" is present */
  258. mangleCache?: Record<string, string | false>;
  259. /** Only when "legalComments" is "external" */
  260. legalComments?: string;
  261. }
  262. export interface TransformFailure extends Error {
  263. errors: Message[];
  264. warnings: Message[];
  265. }
  266. export interface Plugin {
  267. name: string;
  268. setup: (build: PluginBuild) => (void | Promise<void>);
  269. }
  270. export interface PluginBuild {
  271. initialOptions: BuildOptions;
  272. resolve(path: string, options?: ResolveOptions): Promise<ResolveResult>;
  273. onStart(callback: () =>
  274. (OnStartResult | null | void | Promise<OnStartResult | null | void>)): void;
  275. onEnd(callback: (result: BuildResult) =>
  276. (void | Promise<void>)): void;
  277. onResolve(options: OnResolveOptions, callback: (args: OnResolveArgs) =>
  278. (OnResolveResult | null | undefined | Promise<OnResolveResult | null | undefined>)): void;
  279. onLoad(options: OnLoadOptions, callback: (args: OnLoadArgs) =>
  280. (OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>)): void;
  281. // This is a full copy of the esbuild library in case you need it
  282. esbuild: {
  283. serve: typeof serve,
  284. build: typeof build,
  285. buildSync: typeof buildSync,
  286. transform: typeof transform,
  287. transformSync: typeof transformSync,
  288. formatMessages: typeof formatMessages,
  289. formatMessagesSync: typeof formatMessagesSync,
  290. analyzeMetafile: typeof analyzeMetafile,
  291. analyzeMetafileSync: typeof analyzeMetafileSync,
  292. initialize: typeof initialize,
  293. version: typeof version,
  294. };
  295. }
  296. export interface ResolveOptions {
  297. pluginName?: string;
  298. importer?: string;
  299. namespace?: string;
  300. resolveDir?: string;
  301. kind?: ImportKind;
  302. pluginData?: any;
  303. }
  304. export interface ResolveResult {
  305. errors: Message[];
  306. warnings: Message[];
  307. path: string;
  308. external: boolean;
  309. sideEffects: boolean;
  310. namespace: string;
  311. suffix: string;
  312. pluginData: any;
  313. }
  314. export interface OnStartResult {
  315. errors?: PartialMessage[];
  316. warnings?: PartialMessage[];
  317. }
  318. export interface OnResolveOptions {
  319. filter: RegExp;
  320. namespace?: string;
  321. }
  322. export interface OnResolveArgs {
  323. path: string;
  324. importer: string;
  325. namespace: string;
  326. resolveDir: string;
  327. kind: ImportKind;
  328. pluginData: any;
  329. }
  330. export type ImportKind =
  331. | 'entry-point'
  332. // JS
  333. | 'import-statement'
  334. | 'require-call'
  335. | 'dynamic-import'
  336. | 'require-resolve'
  337. // CSS
  338. | 'import-rule'
  339. | 'url-token'
  340. export interface OnResolveResult {
  341. pluginName?: string;
  342. errors?: PartialMessage[];
  343. warnings?: PartialMessage[];
  344. path?: string;
  345. external?: boolean;
  346. sideEffects?: boolean;
  347. namespace?: string;
  348. suffix?: string;
  349. pluginData?: any;
  350. watchFiles?: string[];
  351. watchDirs?: string[];
  352. }
  353. export interface OnLoadOptions {
  354. filter: RegExp;
  355. namespace?: string;
  356. }
  357. export interface OnLoadArgs {
  358. path: string;
  359. namespace: string;
  360. suffix: string;
  361. pluginData: any;
  362. }
  363. export interface OnLoadResult {
  364. pluginName?: string;
  365. errors?: PartialMessage[];
  366. warnings?: PartialMessage[];
  367. contents?: string | Uint8Array;
  368. resolveDir?: string;
  369. loader?: Loader;
  370. pluginData?: any;
  371. watchFiles?: string[];
  372. watchDirs?: string[];
  373. }
  374. export interface PartialMessage {
  375. id?: string;
  376. pluginName?: string;
  377. text?: string;
  378. location?: Partial<Location> | null;
  379. notes?: PartialNote[];
  380. detail?: any;
  381. }
  382. export interface PartialNote {
  383. text?: string;
  384. location?: Partial<Location> | null;
  385. }
  386. export interface Metafile {
  387. inputs: {
  388. [path: string]: {
  389. bytes: number
  390. imports: {
  391. path: string
  392. kind: ImportKind
  393. external?: boolean
  394. original?: string
  395. }[]
  396. format?: 'cjs' | 'esm'
  397. }
  398. }
  399. outputs: {
  400. [path: string]: {
  401. bytes: number
  402. inputs: {
  403. [path: string]: {
  404. bytesInOutput: number
  405. }
  406. }
  407. imports: {
  408. path: string
  409. kind: ImportKind | 'file-loader'
  410. external?: boolean
  411. }[]
  412. exports: string[]
  413. entryPoint?: string
  414. cssBundle?: string
  415. }
  416. }
  417. }
  418. export interface FormatMessagesOptions {
  419. kind: 'error' | 'warning';
  420. color?: boolean;
  421. terminalWidth?: number;
  422. }
  423. export interface AnalyzeMetafileOptions {
  424. color?: boolean;
  425. verbose?: boolean;
  426. }
  427. /**
  428. * This function invokes the "esbuild" command-line tool for you. It returns a
  429. * promise that either resolves with a "BuildResult" object or rejects with a
  430. * "BuildFailure" object.
  431. *
  432. * - Works in node: yes
  433. * - Works in browser: yes
  434. *
  435. * Documentation: https://esbuild.github.io/api/#build-api
  436. */
  437. export declare function build(options: BuildOptions & { write: false }): Promise<BuildResult & { outputFiles: OutputFile[] }>;
  438. export declare function build(options: BuildOptions & { incremental: true, metafile: true }): Promise<BuildIncremental & { metafile: Metafile }>;
  439. export declare function build(options: BuildOptions & { incremental: true }): Promise<BuildIncremental>;
  440. export declare function build(options: BuildOptions & { metafile: true }): Promise<BuildResult & { metafile: Metafile }>;
  441. export declare function build(options: BuildOptions): Promise<BuildResult>;
  442. /**
  443. * This function is similar to "build" but it serves the resulting files over
  444. * HTTP on a localhost address with the specified port.
  445. *
  446. * - Works in node: yes
  447. * - Works in browser: no
  448. *
  449. * Documentation: https://esbuild.github.io/api/#serve
  450. */
  451. export declare function serve(serveOptions: ServeOptions, buildOptions: BuildOptions): Promise<ServeResult>;
  452. /**
  453. * This function transforms a single JavaScript file. It can be used to minify
  454. * JavaScript, convert TypeScript/JSX to JavaScript, or convert newer JavaScript
  455. * to older JavaScript. It returns a promise that is either resolved with a
  456. * "TransformResult" object or rejected with a "TransformFailure" object.
  457. *
  458. * - Works in node: yes
  459. * - Works in browser: yes
  460. *
  461. * Documentation: https://esbuild.github.io/api/#transform-api
  462. */
  463. export declare function transform(input: string | Uint8Array, options?: TransformOptions): Promise<TransformResult>;
  464. /**
  465. * Converts log messages to formatted message strings suitable for printing in
  466. * the terminal. This allows you to reuse the built-in behavior of esbuild's
  467. * log message formatter. This is a batch-oriented API for efficiency.
  468. *
  469. * - Works in node: yes
  470. * - Works in browser: yes
  471. */
  472. export declare function formatMessages(messages: PartialMessage[], options: FormatMessagesOptions): Promise<string[]>;
  473. /**
  474. * Pretty-prints an analysis of the metafile JSON to a string. This is just for
  475. * convenience to be able to match esbuild's pretty-printing exactly. If you want
  476. * to customize it, you can just inspect the data in the metafile yourself.
  477. *
  478. * - Works in node: yes
  479. * - Works in browser: yes
  480. *
  481. * Documentation: https://esbuild.github.io/api/#analyze
  482. */
  483. export declare function analyzeMetafile(metafile: Metafile | string, options?: AnalyzeMetafileOptions): Promise<string>;
  484. /**
  485. * A synchronous version of "build".
  486. *
  487. * - Works in node: yes
  488. * - Works in browser: no
  489. *
  490. * Documentation: https://esbuild.github.io/api/#build-api
  491. */
  492. export declare function buildSync(options: BuildOptions & { write: false }): BuildResult & { outputFiles: OutputFile[] };
  493. export declare function buildSync(options: BuildOptions): BuildResult;
  494. /**
  495. * A synchronous version of "transform".
  496. *
  497. * - Works in node: yes
  498. * - Works in browser: no
  499. *
  500. * Documentation: https://esbuild.github.io/api/#transform-api
  501. */
  502. export declare function transformSync(input: string, options?: TransformOptions): TransformResult;
  503. /**
  504. * A synchronous version of "formatMessages".
  505. *
  506. * - Works in node: yes
  507. * - Works in browser: no
  508. */
  509. export declare function formatMessagesSync(messages: PartialMessage[], options: FormatMessagesOptions): string[];
  510. /**
  511. * A synchronous version of "analyzeMetafile".
  512. *
  513. * - Works in node: yes
  514. * - Works in browser: no
  515. *
  516. * Documentation: https://esbuild.github.io/api/#analyze
  517. */
  518. export declare function analyzeMetafileSync(metafile: Metafile | string, options?: AnalyzeMetafileOptions): string;
  519. /**
  520. * This configures the browser-based version of esbuild. It is necessary to
  521. * call this first and wait for the returned promise to be resolved before
  522. * making other API calls when using esbuild in the browser.
  523. *
  524. * - Works in node: yes
  525. * - Works in browser: yes ("options" is required)
  526. *
  527. * Documentation: https://esbuild.github.io/api/#running-in-the-browser
  528. */
  529. export declare function initialize(options: InitializeOptions): Promise<void>;
  530. export interface InitializeOptions {
  531. /**
  532. * The URL of the "esbuild.wasm" file. This must be provided when running
  533. * esbuild in the browser.
  534. */
  535. wasmURL?: string | URL
  536. /**
  537. * The result of calling "new WebAssembly.Module(buffer)" where "buffer"
  538. * is a typed array or ArrayBuffer containing the binary code of the
  539. * "esbuild.wasm" file.
  540. *
  541. * You can use this as an alternative to "wasmURL" for environments where it's
  542. * not possible to download the WebAssembly module.
  543. */
  544. wasmModule?: WebAssembly.Module
  545. /**
  546. * By default esbuild runs the WebAssembly-based browser API in a web worker
  547. * to avoid blocking the UI thread. This can be disabled by setting "worker"
  548. * to false.
  549. */
  550. worker?: boolean
  551. }
  552. export let version: string;