main.d.ts 17 KB

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