main.d.ts 9.3 KB

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