main.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. export type Platform = 'browser' | 'node';
  2. export type Format = 'iife' | 'cjs' | 'esm';
  3. export type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'css' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'binary';
  4. export type LogLevel = 'info' | 'warning' | 'error' | 'silent';
  5. export type Strict = 'nullish-coalescing' | 'optional-chaining' | 'class-fields';
  6. export type Charset = 'ascii' | 'utf8';
  7. interface CommonOptions {
  8. sourcemap?: boolean | 'inline' | 'external';
  9. format?: Format;
  10. globalName?: string;
  11. target?: string | string[];
  12. strict?: boolean | Strict[];
  13. minify?: boolean;
  14. minifyWhitespace?: boolean;
  15. minifyIdentifiers?: boolean;
  16. minifySyntax?: boolean;
  17. charset?: Charset;
  18. jsxFactory?: string;
  19. jsxFragment?: string;
  20. define?: { [key: string]: string };
  21. pure?: string[];
  22. avoidTDZ?: boolean;
  23. color?: boolean;
  24. logLevel?: LogLevel;
  25. errorLimit?: number;
  26. }
  27. export interface BuildOptions extends CommonOptions {
  28. bundle?: boolean;
  29. splitting?: boolean;
  30. outfile?: string;
  31. metafile?: string;
  32. outdir?: string;
  33. platform?: Platform;
  34. color?: boolean;
  35. external?: string[];
  36. loader?: { [ext: string]: Loader };
  37. resolveExtensions?: string[];
  38. mainFields?: string[];
  39. write?: boolean;
  40. tsconfig?: string;
  41. outExtension?: { [ext: string]: string };
  42. publicPath?: string;
  43. inject?: string[];
  44. entryPoints?: string[];
  45. stdin?: StdinOptions;
  46. }
  47. export interface StdinOptions {
  48. contents: string;
  49. resolveDir?: string;
  50. sourcefile?: string;
  51. loader?: Loader;
  52. }
  53. export interface Message {
  54. text: string;
  55. location: Location | null;
  56. }
  57. export interface Location {
  58. file: string;
  59. line: number; // 1-based
  60. column: number; // 0-based, in bytes
  61. length: number; // in bytes
  62. lineText: string;
  63. }
  64. export interface OutputFile {
  65. path: string;
  66. contents: Uint8Array;
  67. }
  68. export interface BuildResult {
  69. warnings: Message[];
  70. outputFiles?: OutputFile[]; // Only when "write: false"
  71. }
  72. export interface BuildFailure extends Error {
  73. errors: Message[];
  74. warnings: Message[];
  75. }
  76. export interface TransformOptions extends CommonOptions {
  77. tsconfigRaw?: string | {
  78. compilerOptions?: {
  79. jsxFactory?: string,
  80. jsxFragmentFactory?: string,
  81. useDefineForClassFields?: boolean,
  82. importsNotUsedAsValues?: 'remove' | 'preserve' | 'error',
  83. },
  84. };
  85. sourcefile?: string;
  86. loader?: Loader;
  87. }
  88. export interface TransformResult {
  89. js: string;
  90. jsSourceMap: string;
  91. warnings: Message[];
  92. }
  93. export interface TransformFailure extends Error {
  94. errors: Message[];
  95. warnings: Message[];
  96. }
  97. // This is the type information for the "metafile" JSON format
  98. export interface Metadata {
  99. inputs: {
  100. [path: string]: {
  101. bytes: number
  102. imports: {
  103. path: string
  104. }[]
  105. }
  106. }
  107. outputs: {
  108. [path: string]: {
  109. bytes: number
  110. inputs: {
  111. [path: string]: {
  112. bytesInOutput: number
  113. }
  114. }
  115. imports: {
  116. path: string
  117. }[]
  118. }
  119. }
  120. }
  121. export interface Service {
  122. build(options: BuildOptions): Promise<BuildResult>;
  123. transform(input: string, options?: TransformOptions): Promise<TransformResult>;
  124. // This stops the service, which kills the long-lived child process. Any
  125. // pending requests will be aborted.
  126. stop(): void;
  127. }
  128. // This function invokes the "esbuild" command-line tool for you. It returns a
  129. // promise that either resolves with a "BuildResult" object or rejects with a
  130. // "BuildFailure" object.
  131. //
  132. // Works in node: yes
  133. // Works in browser: no
  134. export declare function build(options: BuildOptions): Promise<BuildResult>;
  135. // This function transforms a single JavaScript file. It can be used to minify
  136. // JavaScript, convert TypeScript/JSX to JavaScript, or convert newer JavaScript
  137. // to older JavaScript. It returns a promise that is either resolved with a
  138. // "TransformResult" object or rejected with a "TransformFailure" object.
  139. //
  140. // Works in node: yes
  141. // Works in browser: no
  142. export declare function transform(input: string, options?: TransformOptions): Promise<TransformResult>;
  143. // A synchronous version of "build".
  144. //
  145. // Works in node: yes
  146. // Works in browser: no
  147. export declare function buildSync(options: BuildOptions): BuildResult;
  148. // A synchronous version of "transform".
  149. //
  150. // Works in node: yes
  151. // Works in browser: no
  152. export declare function transformSync(input: string, options?: TransformOptions): TransformResult;
  153. // This starts "esbuild" as a long-lived child process that is then reused, so
  154. // you can call methods on the service many times without the overhead of
  155. // starting up a new child process each time.
  156. //
  157. // Works in node: yes
  158. // Works in browser: yes ("options" is required)
  159. export declare function startService(options?: ServiceOptions): Promise<Service>;
  160. export interface ServiceOptions {
  161. // The URL of the "esbuild.wasm" file. This must be provided when running
  162. // esbuild in the browser.
  163. wasmURL?: string
  164. // By default esbuild runs the WebAssembly-based browser API in a web worker
  165. // to avoid blocking the UI thread. This can be disabled by setting "worker"
  166. // to false.
  167. worker?: boolean
  168. }
  169. export let version: string;