UNPKG

17.3 kBTypeScriptView Raw
1import { BuildOptions, Metafile, Plugin as Plugin$1, Loader } from 'esbuild';
2import { SourceMap as SourceMap$1, TreeshakingOptions, TreeshakingPreset, InputOption } from 'rollup';
3import { RawSourceMap } from 'source-map';
4
5type GeneratedColumn = number;
6type SourcesIndex = number;
7type SourceLine = number;
8type SourceColumn = number;
9type NamesIndex = number;
10type SourceMapSegment = [GeneratedColumn] | [GeneratedColumn, SourcesIndex, SourceLine, SourceColumn] | [GeneratedColumn, SourcesIndex, SourceLine, SourceColumn, NamesIndex];
11
12declare class TraceMap implements SourceMap {
13 version: SourceMapV3['version'];
14 file: SourceMapV3['file'];
15 names: SourceMapV3['names'];
16 sourceRoot: SourceMapV3['sourceRoot'];
17 sources: SourceMapV3['sources'];
18 sourcesContent: SourceMapV3['sourcesContent'];
19 ignoreList: SourceMapV3['ignoreList'];
20 resolvedSources: string[];
21 private _encoded;
22 private _decoded;
23 private _decodedMemo;
24 private _bySources;
25 private _bySourceMemos;
26 constructor(map: SourceMapInput, mapUrl?: string | null);
27}
28
29interface SourceMapV3 {
30 file?: string | null;
31 names: string[];
32 sourceRoot?: string;
33 sources: (string | null)[];
34 sourcesContent?: (string | null)[];
35 version: 3;
36 ignoreList?: number[];
37}
38interface EncodedSourceMap extends SourceMapV3 {
39 mappings: string;
40}
41interface DecodedSourceMap extends SourceMapV3 {
42 mappings: SourceMapSegment[][];
43}
44interface Section {
45 offset: {
46 line: number;
47 column: number;
48 };
49 map: EncodedSourceMap | DecodedSourceMap | SectionedSourceMap;
50}
51interface SectionedSourceMap {
52 file?: string | null;
53 sections: Section[];
54 version: 3;
55}
56type XInput = {
57 x_google_ignoreList?: SourceMapV3['ignoreList'];
58};
59type EncodedSourceMapXInput = EncodedSourceMap & XInput;
60type DecodedSourceMapXInput = DecodedSourceMap & XInput;
61type SectionedSourceMapXInput = Omit<SectionedSourceMap, 'sections'> & {
62 sections: SectionXInput[];
63};
64type SectionXInput = Omit<Section, 'map'> & {
65 map: SectionedSourceMapInput;
66};
67type SourceMapInput = string | EncodedSourceMapXInput | DecodedSourceMapXInput | TraceMap;
68type SectionedSourceMapInput = SourceMapInput | SectionedSourceMapXInput;
69declare abstract class SourceMap {
70 version: SourceMapV3['version'];
71 file: SourceMapV3['file'];
72 names: SourceMapV3['names'];
73 sourceRoot: SourceMapV3['sourceRoot'];
74 sources: SourceMapV3['sources'];
75 sourcesContent: SourceMapV3['sourcesContent'];
76 resolvedSources: SourceMapV3['sources'];
77 ignoreList: SourceMapV3['ignoreList'];
78}
79
80/// <reference lib="es2015" />
81
82
83
84type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;
85
86type ConsoleProperty = keyof typeof console;
87type DropConsoleOption = boolean | ConsoleProperty[];
88
89interface ParseOptions {
90 bare_returns?: boolean;
91 /** @deprecated legacy option. Currently, all supported EcmaScript is valid to parse. */
92 ecma?: ECMA;
93 html5_comments?: boolean;
94 shebang?: boolean;
95}
96
97interface CompressOptions {
98 arguments?: boolean;
99 arrows?: boolean;
100 booleans_as_integers?: boolean;
101 booleans?: boolean;
102 collapse_vars?: boolean;
103 comparisons?: boolean;
104 computed_props?: boolean;
105 conditionals?: boolean;
106 dead_code?: boolean;
107 defaults?: boolean;
108 directives?: boolean;
109 drop_console?: DropConsoleOption;
110 drop_debugger?: boolean;
111 ecma?: ECMA;
112 evaluate?: boolean;
113 expression?: boolean;
114 global_defs?: object;
115 hoist_funs?: boolean;
116 hoist_props?: boolean;
117 hoist_vars?: boolean;
118 ie8?: boolean;
119 if_return?: boolean;
120 inline?: boolean | InlineFunctions;
121 join_vars?: boolean;
122 keep_classnames?: boolean | RegExp;
123 keep_fargs?: boolean;
124 keep_fnames?: boolean | RegExp;
125 keep_infinity?: boolean;
126 loops?: boolean;
127 module?: boolean;
128 negate_iife?: boolean;
129 passes?: number;
130 properties?: boolean;
131 pure_funcs?: string[];
132 pure_new?: boolean;
133 pure_getters?: boolean | 'strict';
134 reduce_funcs?: boolean;
135 reduce_vars?: boolean;
136 sequences?: boolean | number;
137 side_effects?: boolean;
138 switches?: boolean;
139 toplevel?: boolean;
140 top_retain?: null | string | string[] | RegExp;
141 typeofs?: boolean;
142 unsafe_arrows?: boolean;
143 unsafe?: boolean;
144 unsafe_comps?: boolean;
145 unsafe_Function?: boolean;
146 unsafe_math?: boolean;
147 unsafe_symbols?: boolean;
148 unsafe_methods?: boolean;
149 unsafe_proto?: boolean;
150 unsafe_regexp?: boolean;
151 unsafe_undefined?: boolean;
152 unused?: boolean;
153}
154
155declare enum InlineFunctions {
156 Disabled = 0,
157 SimpleFunctions = 1,
158 WithArguments = 2,
159 WithArgumentsAndVariables = 3
160}
161
162interface MangleOptions {
163 eval?: boolean;
164 keep_classnames?: boolean | RegExp;
165 keep_fnames?: boolean | RegExp;
166 module?: boolean;
167 nth_identifier?: SimpleIdentifierMangler | WeightedIdentifierMangler;
168 properties?: boolean | ManglePropertiesOptions;
169 reserved?: string[];
170 safari10?: boolean;
171 toplevel?: boolean;
172}
173
174/**
175 * An identifier mangler for which the output is invariant with respect to the source code.
176 */
177interface SimpleIdentifierMangler {
178 /**
179 * Obtains the nth most favored (usually shortest) identifier to rename a variable to.
180 * The mangler will increment n and retry until the return value is not in use in scope, and is not a reserved word.
181 * This function is expected to be stable; Evaluating get(n) === get(n) should always return true.
182 * @param n The ordinal of the identifier.
183 */
184 get(n: number): string;
185}
186
187/**
188 * An identifier mangler that leverages character frequency analysis to determine identifier precedence.
189 */
190interface WeightedIdentifierMangler extends SimpleIdentifierMangler {
191 /**
192 * Modifies the internal weighting of the input characters by the specified delta.
193 * Will be invoked on the entire printed AST, and then deduct mangleable identifiers.
194 * @param chars The characters to modify the weighting of.
195 * @param delta The numeric weight to add to the characters.
196 */
197 consider(chars: string, delta: number): number;
198 /**
199 * Resets character weights.
200 */
201 reset(): void;
202 /**
203 * Sorts identifiers by character frequency, in preparation for calls to get(n).
204 */
205 sort(): void;
206}
207
208interface ManglePropertiesOptions {
209 builtins?: boolean;
210 debug?: boolean;
211 keep_quoted?: boolean | 'strict';
212 nth_identifier?: SimpleIdentifierMangler | WeightedIdentifierMangler;
213 regex?: RegExp | string;
214 reserved?: string[];
215}
216
217interface FormatOptions {
218 ascii_only?: boolean;
219 /** @deprecated Not implemented anymore */
220 beautify?: boolean;
221 braces?: boolean;
222 comments?: boolean | 'all' | 'some' | RegExp | ( (node: any, comment: {
223 value: string,
224 type: 'comment1' | 'comment2' | 'comment3' | 'comment4',
225 pos: number,
226 line: number,
227 col: number,
228 }) => boolean );
229 ecma?: ECMA;
230 ie8?: boolean;
231 keep_numbers?: boolean;
232 indent_level?: number;
233 indent_start?: number;
234 inline_script?: boolean;
235 keep_quoted_props?: boolean;
236 max_line_len?: number | false;
237 preamble?: string;
238 preserve_annotations?: boolean;
239 quote_keys?: boolean;
240 quote_style?: OutputQuoteStyle;
241 safari10?: boolean;
242 semicolons?: boolean;
243 shebang?: boolean;
244 shorthand?: boolean;
245 source_map?: SourceMapOptions;
246 webkit?: boolean;
247 width?: number;
248 wrap_iife?: boolean;
249 wrap_func_args?: boolean;
250}
251
252declare enum OutputQuoteStyle {
253 PreferDouble = 0,
254 AlwaysSingle = 1,
255 AlwaysDouble = 2,
256 AlwaysOriginal = 3
257}
258
259interface MinifyOptions {
260 compress?: boolean | CompressOptions;
261 ecma?: ECMA;
262 enclose?: boolean | string;
263 ie8?: boolean;
264 keep_classnames?: boolean | RegExp;
265 keep_fnames?: boolean | RegExp;
266 mangle?: boolean | MangleOptions;
267 module?: boolean;
268 nameCache?: object;
269 format?: FormatOptions;
270 /** @deprecated */
271 output?: FormatOptions;
272 parse?: ParseOptions;
273 safari10?: boolean;
274 sourceMap?: boolean | SourceMapOptions;
275 toplevel?: boolean;
276}
277
278interface SourceMapOptions {
279 /** Source map object, 'inline' or source map file content */
280 content?: SectionedSourceMapInput | string;
281 includeSources?: boolean;
282 filename?: string;
283 root?: string;
284 asObject?: boolean;
285 url?: string | 'inline';
286}
287
288type MarkRequired<Type, Keys extends keyof Type> = Type extends Type ? Type & Required<Pick<Type, Keys>> : never;
289
290type Logger = ReturnType<typeof createLogger>;
291declare const createLogger: (name?: string) => {
292 setName(_name: string): void;
293 success(label: string, ...args: any[]): void;
294 info(label: string, ...args: any[]): void;
295 error(label: string, ...args: any[]): void;
296 warn(label: string, ...args: any[]): void;
297 log(label: string, type: "info" | "success" | "error" | "warn", ...data: unknown[]): void;
298};
299
300type ChunkInfo = {
301 type: 'chunk';
302 code: string;
303 map?: string | RawSourceMap | null;
304 path: string;
305 /**
306 * Sets the file mode
307 */
308 mode?: number;
309 entryPoint?: string;
310 exports?: string[];
311 imports?: Metafile['outputs'][string]['imports'];
312};
313type RenderChunk = (this: PluginContext, code: string, chunkInfo: ChunkInfo) => MaybePromise<{
314 code: string;
315 map?: object | string | SourceMap$1 | null;
316} | undefined | null | void>;
317type BuildStart = (this: PluginContext) => MaybePromise<void>;
318type BuildEnd = (this: PluginContext, ctx: {
319 writtenFiles: WrittenFile[];
320}) => MaybePromise<void>;
321type ModifyEsbuildOptions = (this: PluginContext, options: BuildOptions) => void;
322type Plugin = {
323 name: string;
324 esbuildOptions?: ModifyEsbuildOptions;
325 buildStart?: BuildStart;
326 renderChunk?: RenderChunk;
327 buildEnd?: BuildEnd;
328};
329type PluginContext = {
330 format: Format;
331 splitting?: boolean;
332 options: NormalizedOptions;
333 logger: Logger;
334};
335type WrittenFile = {
336 readonly name: string;
337 readonly size: number;
338};
339
340type TreeshakingStrategy = boolean | TreeshakingOptions | TreeshakingPreset;
341
342type KILL_SIGNAL = 'SIGKILL' | 'SIGTERM';
343type Format = 'cjs' | 'esm' | 'iife';
344type ContextForOutPathGeneration = {
345 options: NormalizedOptions;
346 format: Format;
347 /** "type" field in project's package.json */
348 pkgType?: string;
349};
350type OutExtensionObject = {
351 js?: string;
352 dts?: string;
353};
354type OutExtensionFactory = (ctx: ContextForOutPathGeneration) => OutExtensionObject;
355type DtsConfig = {
356 entry?: InputOption;
357 /** Resolve external types used in dts files from node_modules */
358 resolve?: boolean | (string | RegExp)[];
359 /** Emit declaration files only */
360 only?: boolean;
361 /** Insert at the top of each output .d.ts file */
362 banner?: string;
363 /** Insert at the bottom */
364 footer?: string;
365 /**
366 * Overrides `compilerOptions`
367 * This option takes higher priority than `compilerOptions` in tsconfig.json
368 */
369 compilerOptions?: any;
370};
371type ExperimentalDtsConfig = {
372 entry?: InputOption;
373 /**
374 * Overrides `compilerOptions`
375 * This option takes higher priority than `compilerOptions` in tsconfig.json
376 */
377 compilerOptions?: any;
378};
379type BannerOrFooter = {
380 js?: string;
381 css?: string;
382} | ((ctx: {
383 format: Format;
384}) => {
385 js?: string;
386 css?: string;
387} | undefined);
388type BrowserTarget = 'chrome' | 'deno' | 'edge' | 'firefox' | 'hermes' | 'ie' | 'ios' | 'node' | 'opera' | 'rhino' | 'safari';
389type BrowserTargetWithVersion = `${BrowserTarget}${number}` | `${BrowserTarget}${number}.${number}` | `${BrowserTarget}${number}.${number}.${number}`;
390type EsTarget = 'es3' | 'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'es2023' | 'es2024' | 'esnext';
391type Target = BrowserTarget | BrowserTargetWithVersion | EsTarget | (string & {});
392type Entry = string[] | Record<string, string>;
393/**
394 * The options available in tsup.config.ts
395 * Not all of them are available from CLI flags
396 */
397type Options = {
398 /** Optional config name to show in CLI output */
399 name?: string;
400 /**
401 * @deprecated Use `entry` instead
402 */
403 entryPoints?: Entry;
404 entry?: Entry;
405 /**
406 * Output different formats to different folder instead of using different extensions
407 */
408 legacyOutput?: boolean;
409 /**
410 * Compile target
411 *
412 * default to `node16`
413 */
414 target?: Target | Target[];
415 minify?: boolean | 'terser';
416 terserOptions?: MinifyOptions;
417 minifyWhitespace?: boolean;
418 minifyIdentifiers?: boolean;
419 minifySyntax?: boolean;
420 keepNames?: boolean;
421 watch?: boolean | string | (string | boolean)[];
422 ignoreWatch?: string[] | string;
423 onSuccess?: string | (() => Promise<void | undefined | (() => void | Promise<void>)>);
424 jsxFactory?: string;
425 jsxFragment?: string;
426 outDir?: string;
427 outExtension?: OutExtensionFactory;
428 format?: Format[] | Format;
429 globalName?: string;
430 env?: {
431 [k: string]: string;
432 };
433 define?: {
434 [k: string]: string;
435 };
436 dts?: boolean | string | DtsConfig;
437 experimentalDts?: boolean | string | ExperimentalDtsConfig;
438 sourcemap?: boolean | 'inline';
439 /** Always bundle modules matching given patterns */
440 noExternal?: (string | RegExp)[];
441 /** Don't bundle these modules */
442 external?: (string | RegExp)[];
443 /**
444 * Replace `process.env.NODE_ENV` with `production` or `development`
445 * `production` when the bundled is minified, `development` otherwise
446 */
447 replaceNodeEnv?: boolean;
448 /**
449 * Code splitting
450 * Default to `true` for ESM, `false` for CJS.
451 *
452 * You can set it to `true` explicitly, and may want to disable code splitting sometimes: [`#255`](https://github.com/egoist/tsup/issues/255)
453 */
454 splitting?: boolean;
455 /**
456 * Clean output directory before each build
457 */
458 clean?: boolean | string[];
459 esbuildPlugins?: Plugin$1[];
460 esbuildOptions?: (options: BuildOptions, context: {
461 format: Format;
462 }) => void;
463 /**
464 * Suppress non-error logs (excluding "onSuccess" process output)
465 */
466 silent?: boolean;
467 /**
468 * Skip node_modules bundling
469 * Will still bundle modules matching the `noExternal` option
470 */
471 skipNodeModulesBundle?: boolean;
472 /**
473 * @see https://esbuild.github.io/api/#pure
474 */
475 pure?: string | string[];
476 /**
477 * Disable bundling, default to true
478 */
479 bundle?: boolean;
480 /**
481 * This option allows you to automatically replace a global variable with an import from another file.
482 * @see https://esbuild.github.io/api/#inject
483 */
484 inject?: string[];
485 /**
486 * Emit esbuild metafile
487 * @see https://esbuild.github.io/api/#metafile
488 */
489 metafile?: boolean;
490 footer?: BannerOrFooter;
491 banner?: BannerOrFooter;
492 /**
493 * Target platform
494 * @default `node`
495 */
496 platform?: 'node' | 'browser' | 'neutral';
497 /**
498 * Esbuild loader option
499 */
500 loader?: Record<string, Loader>;
501 /**
502 * Disable config file with `false`
503 * Or pass a custom config filename
504 */
505 config?: boolean | string;
506 /**
507 * Use a custom tsconfig
508 */
509 tsconfig?: string;
510 /**
511 * Inject CSS as style tags to document head
512 * @default {false}
513 */
514 injectStyle?: boolean | ((css: string, fileId: string) => string | Promise<string>);
515 /**
516 * Inject cjs and esm shims if needed
517 * @default false
518 */
519 shims?: boolean;
520 /**
521 * TSUP plugins
522 * @experimental
523 * @alpha
524 */
525 plugins?: Plugin[];
526 /**
527 * By default esbuild already does treeshaking
528 *
529 * But this option allow you to perform additional treeshaking with Rollup
530 *
531 * This can result in smaller bundle size
532 */
533 treeshake?: TreeshakingStrategy;
534 /**
535 * Copy the files inside `publicDir` to output directory
536 */
537 publicDir?: string | boolean;
538 killSignal?: KILL_SIGNAL;
539 /**
540 * Interop default within `module.exports` in cjs
541 * @default false
542 */
543 cjsInterop?: boolean;
544 /**
545 * Remove `node:` protocol from imports
546 *
547 * The default value will be flipped to `false` in the next major release
548 * @default true
549 */
550 removeNodeProtocol?: boolean;
551};
552interface NormalizedExperimentalDtsConfig {
553 entry: {
554 [entryAlias: string]: string;
555 };
556 compilerOptions?: any;
557}
558type NormalizedOptions = Omit<MarkRequired<Options, 'entry' | 'outDir'>, 'dts' | 'experimentalDts' | 'format'> & {
559 dts?: DtsConfig;
560 experimentalDts?: NormalizedExperimentalDtsConfig;
561 tsconfigResolvePaths: Record<string, string[]>;
562 tsconfigDecoratorMetadata?: boolean;
563 format: Format[];
564};
565
566type MaybePromise<T> = T | Promise<T>;
567
568declare const defineConfig: (options: Options | Options[] | ((
569/** The options derived from CLI flags */
570overrideOptions: Options) => MaybePromise<Options | Options[]>)) => Options | Options[] | ((overrideOptions: Options) => MaybePromise<Options | Options[]>);
571declare function build(_options: Options): Promise<void>;
572
573export { type Format, type NormalizedOptions, type Options, build, defineConfig };
574
\No newline at end of file