main.d.ts 18 KB

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