main.d.ts 18 KB

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