main.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 TreeShaking = true | 'ignore-annotations';
  7. interface CommonOptions {
  8. sourcemap?: boolean | 'inline' | 'external' | 'both';
  9. legalComments?: 'none' | 'inline' | 'eof' | 'linked' | 'external';
  10. sourceRoot?: string;
  11. sourcesContent?: boolean;
  12. format?: Format;
  13. globalName?: string;
  14. target?: string | string[];
  15. minify?: boolean;
  16. minifyWhitespace?: boolean;
  17. minifyIdentifiers?: boolean;
  18. minifySyntax?: boolean;
  19. charset?: Charset;
  20. treeShaking?: TreeShaking;
  21. jsx?: 'transform' | 'preserve';
  22. jsxFactory?: string;
  23. jsxFragment?: string;
  24. define?: { [key: string]: string };
  25. pure?: string[];
  26. keepNames?: boolean;
  27. color?: boolean;
  28. logLevel?: LogLevel;
  29. logLimit?: number;
  30. }
  31. export interface BuildOptions extends CommonOptions {
  32. bundle?: boolean;
  33. splitting?: boolean;
  34. preserveSymlinks?: boolean;
  35. outfile?: string;
  36. metafile?: boolean;
  37. outdir?: string;
  38. outbase?: string;
  39. platform?: Platform;
  40. external?: string[];
  41. loader?: { [ext: string]: Loader };
  42. resolveExtensions?: string[];
  43. mainFields?: string[];
  44. conditions?: string[];
  45. write?: boolean;
  46. allowOverwrite?: boolean;
  47. tsconfig?: string;
  48. outExtension?: { [ext: string]: string };
  49. publicPath?: string;
  50. entryNames?: string;
  51. chunkNames?: string;
  52. assetNames?: string;
  53. inject?: string[];
  54. banner?: { [type: string]: string };
  55. footer?: { [type: string]: string };
  56. incremental?: boolean;
  57. entryPoints?: string[] | Record<string, string>;
  58. stdin?: StdinOptions;
  59. plugins?: Plugin[];
  60. absWorkingDir?: string;
  61. nodePaths?: string[]; // The "NODE_PATH" variable from Node.js
  62. watch?: boolean | WatchMode;
  63. }
  64. export interface WatchMode {
  65. onRebuild?: (error: BuildFailure | null, result: BuildResult | null) => void;
  66. }
  67. export interface StdinOptions {
  68. contents: string;
  69. resolveDir?: string;
  70. sourcefile?: string;
  71. loader?: Loader;
  72. }
  73. export interface Message {
  74. pluginName: string;
  75. text: string;
  76. location: Location | null;
  77. notes: Note[];
  78. // Optional user-specified data that is passed through unmodified. You can
  79. // use this to stash the original error, for example.
  80. detail: any;
  81. }
  82. export interface Note {
  83. text: string;
  84. location: Location | null;
  85. }
  86. export interface Location {
  87. file: string;
  88. namespace: string;
  89. line: number; // 1-based
  90. column: number; // 0-based, in bytes
  91. length: number; // in bytes
  92. lineText: string;
  93. suggestion: string;
  94. }
  95. export interface OutputFile {
  96. path: string;
  97. contents: Uint8Array; // "text" as bytes
  98. text: string; // "contents" as text
  99. }
  100. export interface BuildInvalidate {
  101. (): Promise<BuildIncremental>;
  102. dispose(): void;
  103. }
  104. export interface BuildIncremental extends BuildResult {
  105. rebuild: BuildInvalidate;
  106. }
  107. export interface BuildResult {
  108. errors: Message[];
  109. warnings: Message[];
  110. outputFiles?: OutputFile[]; // Only when "write: false"
  111. rebuild?: BuildInvalidate; // Only when "incremental: true"
  112. stop?: () => void; // Only when "watch: true"
  113. metafile?: Metafile; // Only when "metafile: true"
  114. }
  115. export interface BuildFailure extends Error {
  116. errors: Message[];
  117. warnings: Message[];
  118. }
  119. export interface ServeOptions {
  120. port?: number;
  121. host?: string;
  122. servedir?: string;
  123. onRequest?: (args: ServeOnRequestArgs) => void;
  124. }
  125. export interface ServeOnRequestArgs {
  126. remoteAddress: string;
  127. method: string;
  128. path: string;
  129. status: number;
  130. timeInMS: number; // The time to generate the response, not to send it
  131. }
  132. export interface ServeResult {
  133. port: number;
  134. host: string;
  135. wait: Promise<void>;
  136. stop: () => void;
  137. }
  138. export interface TransformOptions extends CommonOptions {
  139. tsconfigRaw?: string | {
  140. compilerOptions?: {
  141. jsxFactory?: string,
  142. jsxFragmentFactory?: string,
  143. useDefineForClassFields?: boolean,
  144. importsNotUsedAsValues?: 'remove' | 'preserve' | 'error',
  145. },
  146. };
  147. sourcefile?: string;
  148. loader?: Loader;
  149. banner?: string;
  150. footer?: string;
  151. }
  152. export interface TransformResult {
  153. code: string;
  154. map: string;
  155. warnings: Message[];
  156. }
  157. export interface TransformFailure extends Error {
  158. errors: Message[];
  159. warnings: Message[];
  160. }
  161. export interface Plugin {
  162. name: string;
  163. setup: (build: PluginBuild) => (void | Promise<void>);
  164. }
  165. export interface PluginBuild {
  166. initialOptions: BuildOptions;
  167. onStart(callback: () =>
  168. (OnStartResult | null | void | Promise<OnStartResult | null | void>)): void;
  169. onEnd(callback: (result: BuildResult) =>
  170. (void | Promise<void>)): void;
  171. onResolve(options: OnResolveOptions, callback: (args: OnResolveArgs) =>
  172. (OnResolveResult | null | undefined | Promise<OnResolveResult | null | undefined>)): void;
  173. onLoad(options: OnLoadOptions, callback: (args: OnLoadArgs) =>
  174. (OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>)): void;
  175. }
  176. export interface OnStartResult {
  177. errors?: PartialMessage[];
  178. warnings?: PartialMessage[];
  179. }
  180. export interface OnResolveOptions {
  181. filter: RegExp;
  182. namespace?: string;
  183. }
  184. export interface OnResolveArgs {
  185. path: string;
  186. importer: string;
  187. namespace: string;
  188. resolveDir: string;
  189. kind: ImportKind;
  190. pluginData: any;
  191. }
  192. export type ImportKind =
  193. | 'entry-point'
  194. // JS
  195. | 'import-statement'
  196. | 'require-call'
  197. | 'dynamic-import'
  198. | 'require-resolve'
  199. // CSS
  200. | 'import-rule'
  201. | 'url-token'
  202. export interface OnResolveResult {
  203. pluginName?: string;
  204. errors?: PartialMessage[];
  205. warnings?: PartialMessage[];
  206. path?: string;
  207. external?: boolean;
  208. sideEffects?: boolean;
  209. namespace?: string;
  210. pluginData?: any;
  211. watchFiles?: string[];
  212. watchDirs?: string[];
  213. }
  214. export interface OnLoadOptions {
  215. filter: RegExp;
  216. namespace?: string;
  217. }
  218. export interface OnLoadArgs {
  219. path: string;
  220. namespace: string;
  221. pluginData: any;
  222. }
  223. export interface OnLoadResult {
  224. pluginName?: string;
  225. errors?: PartialMessage[];
  226. warnings?: PartialMessage[];
  227. contents?: string | Uint8Array;
  228. resolveDir?: string;
  229. loader?: Loader;
  230. pluginData?: any;
  231. watchFiles?: string[];
  232. watchDirs?: string[];
  233. }
  234. export interface PartialMessage {
  235. pluginName?: string;
  236. text?: string;
  237. location?: Partial<Location> | null;
  238. notes?: PartialNote[];
  239. detail?: any;
  240. }
  241. export interface PartialNote {
  242. text?: string;
  243. location?: Partial<Location> | null;
  244. }
  245. export interface Metafile {
  246. inputs: {
  247. [path: string]: {
  248. bytes: number
  249. imports: {
  250. path: string
  251. kind: ImportKind
  252. }[]
  253. }
  254. }
  255. outputs: {
  256. [path: string]: {
  257. bytes: number
  258. inputs: {
  259. [path: string]: {
  260. bytesInOutput: number
  261. }
  262. }
  263. imports: {
  264. path: string
  265. kind: ImportKind
  266. }[]
  267. exports: string[]
  268. entryPoint?: string
  269. }
  270. }
  271. }
  272. export interface FormatMessagesOptions {
  273. kind: 'error' | 'warning';
  274. color?: boolean;
  275. terminalWidth?: number;
  276. }
  277. // This function invokes the "esbuild" command-line tool for you. It returns a
  278. // promise that either resolves with a "BuildResult" object or rejects with a
  279. // "BuildFailure" object.
  280. //
  281. // Works in node: yes
  282. // Works in browser: yes
  283. export declare function build(options: BuildOptions & { write: false }): Promise<BuildResult & { outputFiles: OutputFile[] }>;
  284. export declare function build(options: BuildOptions & { incremental: true }): Promise<BuildIncremental>;
  285. export declare function build(options: BuildOptions): Promise<BuildResult>;
  286. // This function is similar to "build" but it serves the resulting files over
  287. // HTTP on a localhost address with the specified port.
  288. //
  289. // Works in node: yes
  290. // Works in browser: no
  291. export declare function serve(serveOptions: ServeOptions, buildOptions: BuildOptions): Promise<ServeResult>;
  292. // This function transforms a single JavaScript file. It can be used to minify
  293. // JavaScript, convert TypeScript/JSX to JavaScript, or convert newer JavaScript
  294. // to older JavaScript. It returns a promise that is either resolved with a
  295. // "TransformResult" object or rejected with a "TransformFailure" object.
  296. //
  297. // Works in node: yes
  298. // Works in browser: yes
  299. export declare function transform(input: string, options?: TransformOptions): Promise<TransformResult>;
  300. // Converts log messages to formatted message strings suitable for printing in
  301. // the terminal. This allows you to reuse the built-in behavior of esbuild's
  302. // log message formatter. This is a batch-oriented API for efficiency.
  303. //
  304. // Works in node: yes
  305. // Works in browser: yes
  306. export declare function formatMessages(messages: PartialMessage[], options: FormatMessagesOptions): Promise<string[]>;
  307. // A synchronous version of "build".
  308. //
  309. // Works in node: yes
  310. // Works in browser: no
  311. export declare function buildSync(options: BuildOptions & { write: false }): BuildResult & { outputFiles: OutputFile[] };
  312. export declare function buildSync(options: BuildOptions): BuildResult;
  313. // A synchronous version of "transform".
  314. //
  315. // Works in node: yes
  316. // Works in browser: no
  317. export declare function transformSync(input: string, options?: TransformOptions): TransformResult;
  318. // A synchronous version of "formatMessages".
  319. //
  320. // Works in node: yes
  321. // Works in browser: no
  322. export declare function formatMessagesSync(messages: PartialMessage[], options: FormatMessagesOptions): string[];
  323. // This configures the browser-based version of esbuild. It is necessary to
  324. // call this first and wait for the returned promise to be resolved before
  325. // making other API calls when using esbuild in the browser.
  326. //
  327. // Works in node: yes
  328. // Works in browser: yes ("options" is required)
  329. export declare function initialize(options: InitializeOptions): Promise<void>;
  330. export interface InitializeOptions {
  331. // The URL of the "esbuild.wasm" file. This must be provided when running
  332. // esbuild in the browser.
  333. wasmURL?: string
  334. // By default esbuild runs the WebAssembly-based browser API in a web worker
  335. // to avoid blocking the UI thread. This can be disabled by setting "worker"
  336. // to false.
  337. worker?: boolean
  338. }
  339. export let version: string;