1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import { GeneratorOptions } from '@babel/generator';
|
12 | import { ParserOptions } from '@babel/parser';
|
13 | import template from '@babel/template';
|
14 | import traverse, { Hub, NodePath, Scope, Visitor } from '@babel/traverse';
|
15 | import * as t from '@babel/types';
|
16 |
|
17 | export { ParserOptions, GeneratorOptions, t as types, template, traverse, NodePath, Visitor };
|
18 |
|
19 | export type Node = t.Node;
|
20 | export type ParseResult = ReturnType<typeof import('@babel/parser').parse>;
|
21 | export const version: string;
|
22 | export const DEFAULT_EXTENSIONS: ['.js', '.jsx', '.es6', '.es', '.mjs'];
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | interface InputSourceMap {
|
30 | version: number;
|
31 | sources: string[];
|
32 | names: string[];
|
33 | sourceRoot?: string | undefined;
|
34 | sourcesContent?: string[] | undefined;
|
35 | mappings: string;
|
36 | file: string;
|
37 | }
|
38 |
|
39 | export interface TransformOptions {
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | assumptions?: { [name: string]: boolean } | null | undefined;
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 | ast?: boolean | null | undefined;
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | auxiliaryCommentAfter?: string | null | undefined;
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | auxiliaryCommentBefore?: string | null | undefined;
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | root?: string | null | undefined;
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | rootMode?: 'root' | 'upward' | 'upward-optional' | undefined;
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | configFile?: string | boolean | null | undefined;
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | babelrc?: boolean | null | undefined;
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | babelrcRoots?: boolean | MatchPattern | MatchPattern[] | null | undefined;
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | browserslistConfigFile?: boolean | null | undefined;
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | browserslistEnv?: string | null | undefined;
|
125 |
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | cloneInputAst?: boolean | null | undefined;
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 | envName?: string | undefined;
|
140 |
|
141 | |
142 |
|
143 |
|
144 | exclude?: MatchPattern | MatchPattern[] | undefined;
|
145 |
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 | code?: boolean | null | undefined;
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | comments?: boolean | null | undefined;
|
159 |
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 | compact?: boolean | 'auto' | null | undefined;
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 | cwd?: string | null | undefined;
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | caller?: TransformCaller | undefined;
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | env?: { [index: string]: TransformOptions | null | undefined } | null | undefined;
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 | extends?: string | null | undefined;
|
196 |
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 | filename?: string | null | undefined;
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 | filenameRelative?: string | null | undefined;
|
210 |
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 | generatorOpts?: GeneratorOptions | null | undefined;
|
217 |
|
218 | |
219 |
|
220 |
|
221 |
|
222 |
|
223 | getModuleId?: ((moduleName: string) => string | null | undefined) | null | undefined;
|
224 |
|
225 | /**
|
226 | * ANSI highlight syntax error code frames
|
227 | *
|
228 | * Default: `true`
|
229 | */
|
230 | highlightCode?: boolean | null | undefined;
|
231 |
|
232 | /**
|
233 | * Opposite to the `only` option. `ignore` is disregarded if `only` is specified
|
234 | *
|
235 | * Default: `null`
|
236 | */
|
237 | ignore?: MatchPattern[] | null | undefined;
|
238 |
|
239 | /**
|
240 | * This option is a synonym for "test"
|
241 | */
|
242 | include?: MatchPattern | MatchPattern[] | undefined;
|
243 |
|
244 | /**
|
245 | * A source map object that the output source map will be based on
|
246 | *
|
247 | * Default: `null`
|
248 | */
|
249 | inputSourceMap?: InputSourceMap | null | undefined;
|
250 |
|
251 | /**
|
252 | * Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping `()` from `new` when safe)
|
253 | *
|
254 | * Default: `false`
|
255 | */
|
256 | minified?: boolean | null | undefined;
|
257 |
|
258 | /**
|
259 | * Specify a custom name for module ids
|
260 | *
|
261 | * Default: `null`
|
262 | */
|
263 | moduleId?: string | null | undefined;
|
264 |
|
265 | /**
|
266 | * If truthy, insert an explicit id for modules. By default, all modules are anonymous. (Not available for `common` modules)
|
267 | *
|
268 | * Default: `false`
|
269 | */
|
270 | moduleIds?: boolean | null | undefined;
|
271 |
|
272 | /**
|
273 | * Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions
|
274 | *
|
275 | * Default: `(sourceRoot)`
|
276 | */
|
277 | moduleRoot?: string | null | undefined;
|
278 |
|
279 | /**
|
280 | * A glob, regex, or mixed array of both, matching paths to **only** compile. Can also be an array of arrays containing paths to explicitly match. When attempting to compile
|
281 | * a non-matching file it's returned verbatim
|
282 | *
|
283 | * Default: `null`
|
284 | */
|
285 | only?: MatchPattern[] | null | undefined;
|
286 |
|
287 | /**
|
288 | * Allows users to provide an array of options that will be merged into the current configuration one at a time.
|
289 | * This feature is best used alongside the "test"/"include"/"exclude" options to provide conditions for which an override should apply
|
290 | */
|
291 | overrides?: TransformOptions[] | undefined;
|
292 |
|
293 | /**
|
294 | * An object containing the options to be passed down to the babel parser, @babel/parser
|
295 | *
|
296 | * Default: `{}`
|
297 | */
|
298 | parserOpts?: ParserOptions | null | undefined;
|
299 |
|
300 | /**
|
301 | * List of plugins to load and use
|
302 | *
|
303 | * Default: `[]`
|
304 | */
|
305 | plugins?: PluginItem[] | null | undefined;
|
306 |
|
307 | /**
|
308 | * List of presets (a set of plugins) to load and use
|
309 | *
|
310 | * Default: `[]`
|
311 | */
|
312 | presets?: PluginItem[] | null | undefined;
|
313 |
|
314 | /**
|
315 | * Retain line numbers. This will lead to wacky code but is handy for scenarios where you can't use source maps. (**NOTE**: This will not retain the columns)
|
316 | *
|
317 | * Default: `false`
|
318 | */
|
319 | retainLines?: boolean | null | undefined;
|
320 |
|
321 | /**
|
322 | * An optional callback that controls whether a comment should be output or not. Called as `shouldPrintComment(commentContents)`. **NOTE**: This overrides the `comment` option when used
|
323 | *
|
324 | * Default: `null`
|
325 | */
|
326 | shouldPrintComment?: ((commentContents: string) => boolean) | null | undefined;
|
327 |
|
328 | /**
|
329 | * Set `sources[0]` on returned source map
|
330 | *
|
331 | * Default: `(filenameRelative)`
|
332 | */
|
333 | sourceFileName?: string | null | undefined;
|
334 |
|
335 | /**
|
336 | * If truthy, adds a `map` property to returned output. If set to `"inline"`, a comment with a sourceMappingURL directive is added to the bottom of the returned code. If set to `"both"`
|
337 | * then a `map` property is returned as well as a source map comment appended. **This does not emit sourcemap files by itself!**
|
338 | *
|
339 | * Default: `false`
|
340 | */
|
341 | sourceMaps?: boolean | 'inline' | 'both' | null | undefined;
|
342 |
|
343 | /**
|
344 | * The root from which all sources are relative
|
345 | *
|
346 | * Default: `(moduleRoot)`
|
347 | */
|
348 | sourceRoot?: string | null | undefined;
|
349 |
|
350 | /**
|
351 | * Indicate the mode the code should be parsed in. Can be one of "script", "module", or "unambiguous". `"unambiguous"` will make Babel attempt to guess, based on the presence of ES6
|
352 | * `import` or `export` statements. Files with ES6 `import`s and `export`s are considered `"module"` and are otherwise `"script"`.
|
353 | *
|
354 | * Default: `("module")`
|
355 | */
|
356 | sourceType?: 'script' | 'module' | 'unambiguous' | null | undefined;
|
357 |
|
358 | /**
|
359 | * If all patterns fail to match, the current configuration object is considered inactive and is ignored during config processing.
|
360 | */
|
361 | test?: MatchPattern | MatchPattern[] | undefined;
|
362 |
|
363 | /**
|
364 | * An optional callback that can be used to wrap visitor methods. **NOTE**: This is useful for things like introspection, and not really needed for implementing anything. Called as
|
365 | * `wrapPluginVisitorMethod(pluginAlias, visitorType, callback)`.
|
366 | */
|
367 | wrapPluginVisitorMethod?:
|
368 | | ((
|
369 | pluginAlias: string,
|
370 | visitorType: 'enter' | 'exit',
|
371 | callback: (path: NodePath, state: any) => void,
|
372 | ) => (path: NodePath, state: any) => void)
|
373 | | null | undefined;
|
374 | }
|
375 |
|
376 | export interface TransformCaller {
|
377 | // the only required property
|
378 | name: string;
|
379 | // e.g. set to true by `babel-loader` and false by `babel-jest`
|
380 | supportsStaticESM?: boolean | undefined;
|
381 | supportsDynamicImport?: boolean | undefined;
|
382 | supportsExportNamespaceFrom?: boolean | undefined;
|
383 | supportsTopLevelAwait?: boolean | undefined;
|
384 | // augment this with a "declare module '@babel/core' { ... }" if you need more keys
|
385 | }
|
386 |
|
387 | export type FileResultCallback = (err: Error | null, result: BabelFileResult | null) => any;
|
388 |
|
389 | export interface MatchPatternContext {
|
390 | envName: string;
|
391 | dirname: string;
|
392 | caller: TransformCaller | undefined;
|
393 | }
|
394 | export type MatchPattern = string | RegExp | ((filename: string | undefined, context: MatchPatternContext) => boolean);
|
395 |
|
396 | /**
|
397 | * Transforms the passed in code. Calling a callback with an object with the generated code, source map, and AST.
|
398 | */
|
399 | export function transform(code: string, callback: FileResultCallback): void;
|
400 |
|
401 | /**
|
402 | * Transforms the passed in code. Calling a callback with an object with the generated code, source map, and AST.
|
403 | */
|
404 | export function transform(code: string, opts: TransformOptions | undefined, callback: FileResultCallback): void;
|
405 |
|
406 | /**
|
407 | * Here for backward-compatibility. Ideally use `transformSync` if you want a synchronous API.
|
408 | */
|
409 | export function transform(code: string, opts?: TransformOptions): BabelFileResult | null;
|
410 |
|
411 | /**
|
412 | * Transforms the passed in code. Returning an object with the generated code, source map, and AST.
|
413 | */
|
414 | export function transformSync(code: string, opts?: TransformOptions): BabelFileResult | null;
|
415 |
|
416 | /**
|
417 | * Transforms the passed in code. Calling a callback with an object with the generated code, source map, and AST.
|
418 | */
|
419 | export function transformAsync(code: string, opts?: TransformOptions): Promise<BabelFileResult | null>;
|
420 |
|
421 | /**
|
422 | * Asynchronously transforms the entire contents of a file.
|
423 | */
|
424 | export function transformFile(filename: string, callback: FileResultCallback): void;
|
425 |
|
426 | /**
|
427 | * Asynchronously transforms the entire contents of a file.
|
428 | */
|
429 | export function transformFile(filename: string, opts: TransformOptions | undefined, callback: FileResultCallback): void;
|
430 |
|
431 | /**
|
432 | * Synchronous version of `babel.transformFile`. Returns the transformed contents of the `filename`.
|
433 | */
|
434 | export function transformFileSync(filename: string, opts?: TransformOptions): BabelFileResult | null;
|
435 |
|
436 | /**
|
437 | * Asynchronously transforms the entire contents of a file.
|
438 | */
|
439 | export function transformFileAsync(filename: string, opts?: TransformOptions): Promise<BabelFileResult | null>;
|
440 |
|
441 | /**
|
442 | * Given an AST, transform it.
|
443 | */
|
444 | export function transformFromAst(ast: Node, code: string | undefined, callback: FileResultCallback): void;
|
445 |
|
446 | /**
|
447 | * Given an AST, transform it.
|
448 | */
|
449 | export function transformFromAst(
|
450 | ast: Node,
|
451 | code: string | undefined,
|
452 | opts: TransformOptions | undefined,
|
453 | callback: FileResultCallback,
|
454 | ): void;
|
455 |
|
456 | /**
|
457 | * Here for backward-compatibility. Ideally use ".transformSync" if you want a synchronous API.
|
458 | */
|
459 | export function transformFromAstSync(ast: Node, code?: string, opts?: TransformOptions): BabelFileResult | null;
|
460 |
|
461 | /**
|
462 | * Given an AST, transform it.
|
463 | */
|
464 | export function transformFromAstAsync(
|
465 | ast: Node,
|
466 | code?: string,
|
467 | opts?: TransformOptions,
|
468 | ): Promise<BabelFileResult | null>;
|
469 |
|
470 | // A babel plugin is a simple function which must return an object matching
|
471 | // the following interface. Babel will throw if it finds unknown properties.
|
472 | // The list of allowed plugin keys is here:
|
473 | // https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-core/src/config/option-manager.js#L71
|
474 | export interface PluginObj<S = PluginPass> {
|
475 | name?: string | undefined;
|
476 | manipulateOptions?(opts: any, parserOpts: any): void;
|
477 | pre?(this: S, file: BabelFile): void;
|
478 | visitor: Visitor<S>;
|
479 | post?(this: S, file: BabelFile): void;
|
480 | inherits?: any;
|
481 | }
|
482 |
|
483 | export interface BabelFile {
|
484 | ast: t.File;
|
485 | opts: TransformOptions;
|
486 | hub: Hub;
|
487 | metadata: object;
|
488 | path: NodePath<t.Program>;
|
489 | scope: Scope;
|
490 | inputMap: object | null;
|
491 | code: string;
|
492 | }
|
493 |
|
494 | export interface PluginPass {
|
495 | file: BabelFile;
|
496 | key: string;
|
497 | opts: object;
|
498 | cwd: string;
|
499 | filename: string | undefined;
|
500 | get(key: unknown): any;
|
501 | set(key: unknown, value: unknown): void;
|
502 | [key: string]: unknown;
|
503 | }
|
504 |
|
505 | export interface BabelFileResult {
|
506 | ast?: t.File | null | undefined;
|
507 | code?: string | null | undefined;
|
508 | ignored?: boolean | undefined;
|
509 | map?: {
|
510 | version: number;
|
511 | sources: string[];
|
512 | names: string[];
|
513 | sourceRoot?: string | undefined;
|
514 | sourcesContent?: string[] | undefined;
|
515 | mappings: string;
|
516 | file: string;
|
517 | } | null | undefined;
|
518 | metadata?: BabelFileMetadata | undefined;
|
519 | }
|
520 |
|
521 | export interface BabelFileMetadata {
|
522 | usedHelpers: string[];
|
523 | marked: Array<{
|
524 | type: string;
|
525 | message: string;
|
526 | loc: object;
|
527 | }>;
|
528 | modules: BabelFileModulesMetadata;
|
529 | }
|
530 |
|
531 | export interface BabelFileModulesMetadata {
|
532 | imports: object[];
|
533 | exports: {
|
534 | exported: object[];
|
535 | specifiers: object[];
|
536 | };
|
537 | }
|
538 |
|
539 | export type FileParseCallback = (err: Error | null, result: ParseResult | null) => any;
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 | export function parse(code: string, callback: FileParseCallback): void;
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | export function parse(code: string, options: TransformOptions | undefined, callback: FileParseCallback): void;
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 | export function parse(code: string, options?: TransformOptions): ParseResult | null;
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | export function parseSync(code: string, options?: TransformOptions): ParseResult | null;
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 | export function parseAsync(code: string, options?: TransformOptions): Promise<ParseResult | null>;
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | export function loadOptions(options?: TransformOptions): object | null;
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | export function loadPartialConfig(options?: TransformOptions): Readonly<PartialConfig> | null;
|
605 | export function loadPartialConfigAsync(options?: TransformOptions): Promise<Readonly<PartialConfig> | null>;
|
606 |
|
607 | export interface PartialConfig {
|
608 | options: TransformOptions;
|
609 | babelrc?: string | undefined;
|
610 | babelignore?: string | undefined;
|
611 | config?: string | undefined;
|
612 | hasFilesystemConfig: () => boolean;
|
613 | }
|
614 |
|
615 | export interface ConfigItem {
|
616 | |
617 |
|
618 |
|
619 | name?: string | undefined;
|
620 |
|
621 | |
622 |
|
623 |
|
624 | value: object | ((...args: any[]) => any);
|
625 |
|
626 | /**
|
627 | * The options object passed to the plugin.
|
628 | */
|
629 | options?: object | false | undefined;
|
630 |
|
631 | /**
|
632 | * The path that the options are relative to.
|
633 | */
|
634 | dirname: string;
|
635 |
|
636 | /**
|
637 | * Information about the plugin's file, if Babel knows it.
|
638 | * *
|
639 | */
|
640 | file?: {
|
641 | /**
|
642 | * The file that the user requested, e.g. `"@babel/env"`
|
643 | */
|
644 | request: string;
|
645 |
|
646 | /**
|
647 | * The full path of the resolved file, e.g. `"/tmp/node_modules/@babel/preset-env/lib/index.js"`
|
648 | */
|
649 | resolved: string;
|
650 | } | null | undefined;
|
651 | }
|
652 |
|
653 | export type PluginOptions = object | undefined | false;
|
654 |
|
655 | export type PluginTarget = string | object | ((...args: any[]) => any);
|
656 |
|
657 | export type PluginItem =
|
658 | | ConfigItem
|
659 | | PluginObj<any>
|
660 | | PluginTarget
|
661 | | [PluginTarget, PluginOptions]
|
662 | | [PluginTarget, PluginOptions, string | undefined];
|
663 |
|
664 | export function resolvePlugin(name: string, dirname: string): string | null;
|
665 | export function resolvePreset(name: string, dirname: string): string | null;
|
666 |
|
667 | export interface CreateConfigItemOptions {
|
668 | dirname?: string | undefined;
|
669 | type?: 'preset' | 'plugin' | undefined;
|
670 | }
|
671 |
|
672 | /**
|
673 | * Allows build tooling to create and cache config items up front. If this function is called multiple times for a
|
674 | * given plugin, Babel will call the plugin's function itself multiple times. If you have a clear set of expected
|
675 | * plugins and presets to inject, pre-constructing the config items would be recommended.
|
676 | */
|
677 | export function createConfigItem(
|
678 | value: PluginTarget | [PluginTarget, PluginOptions] | [PluginTarget, PluginOptions, string | undefined],
|
679 | options?: CreateConfigItemOptions,
|
680 | ): ConfigItem;
|
681 |
|
682 | // NOTE: the documentation says the ConfigAPI also exposes @babel/core's exports, but it actually doesn't
|
683 | /**
|
684 | * @see https://babeljs.io/docs/en/next/config-files#config-function-api
|
685 | */
|
686 | export interface ConfigAPI {
|
687 | /**
|
688 | * The version string for the Babel version that is loading the config file.
|
689 | *
|
690 | * @see https://babeljs.io/docs/en/next/config-files#apiversion
|
691 | */
|
692 | version: string;
|
693 | /**
|
694 | * @see https://babeljs.io/docs/en/next/config-files#apicache
|
695 | */
|
696 | cache: SimpleCacheConfigurator;
|
697 | /**
|
698 | * @see https://babeljs.io/docs/en/next/config-files#apienv
|
699 | */
|
700 | env: EnvFunction;
|
701 | // undocumented; currently hardcoded to return 'false'
|
702 | // async(): boolean
|
703 | /**
|
704 | * This API is used as a way to access the `caller` data that has been passed to Babel.
|
705 | * Since many instances of Babel may be running in the same process with different `caller` values,
|
706 | * this API is designed to automatically configure `api.cache`, the same way `api.env()` does.
|
707 | *
|
708 | * The `caller` value is available as the first parameter of the callback function.
|
709 | * It is best used with something like this to toggle configuration behavior
|
710 | * based on a specific environment:
|
711 | *
|
712 | * @example
|
713 | * function isBabelRegister(caller?: { name: string }) {
|
714 | * return !!(caller && caller.name === "@babel/register")
|
715 | * }
|
716 | * api.caller(isBabelRegister)
|
717 | *
|
718 | * @see https://babeljs.io/docs/en/next/config-files#apicallercb
|
719 | */
|
720 | caller<T extends SimpleCacheKey>(callerCallback: (caller: TransformOptions['caller']) => T): T;
|
721 | /**
|
722 | * While `api.version` can be useful in general, it's sometimes nice to just declare your version.
|
723 | * This API exposes a simple way to do that with:
|
724 | *
|
725 | * @example
|
726 | * api.assertVersion(7) // major version only
|
727 | * api.assertVersion("^7.2")
|
728 | *
|
729 | * @see https://babeljs.io/docs/en/next/config-files#apiassertversionrange
|
730 | */
|
731 | assertVersion(versionRange: number | string): boolean;
|
732 | // NOTE: this is an undocumented reexport from "@babel/parser" but it's missing from its types
|
733 | // tokTypes: typeof tokTypes
|
734 | }
|
735 |
|
736 | /**
|
737 | * JS configs are great because they can compute a config on the fly,
|
738 | * but the downside there is that it makes caching harder.
|
739 | * Babel wants to avoid re-executing the config function every time a file is compiled,
|
740 | * because then it would also need to re-execute any plugin and preset functions
|
741 | * referenced in that config.
|
742 | *
|
743 | * To avoid this, Babel expects users of config functions to tell it how to manage caching
|
744 | * within a config file.
|
745 | *
|
746 | * @see https://babeljs.io/docs/en/next/config-files#apicache
|
747 | */
|
748 | export interface SimpleCacheConfigurator {
|
749 | // there is an undocumented call signature that is a shorthand for forever()/never()/using().
|
750 | // (ever: boolean): void
|
751 | // <T extends SimpleCacheKey>(callback: CacheCallback<T>): T
|
752 | /**
|
753 | * Permacache the computed config and never call the function again.
|
754 | */
|
755 | forever(): void;
|
756 | /**
|
757 | * Do not cache this config, and re-execute the function every time.
|
758 | */
|
759 | never(): void;
|
760 | /**
|
761 | * Any time the using callback returns a value other than the one that was expected,
|
762 | * the overall config function will be called again and a new entry will be added to the cache.
|
763 | *
|
764 | * @example
|
765 | * api.cache.using(() => process.env.NODE_ENV)
|
766 | */
|
767 | using<T extends SimpleCacheKey>(callback: SimpleCacheCallback<T>): T;
|
768 | /**
|
769 | * Any time the using callback returns a value other than the one that was expected,
|
770 | * the overall config function will be called again and all entries in the cache will
|
771 | * be replaced with the result.
|
772 | *
|
773 | * @example
|
774 | * api.cache.invalidate(() => process.env.NODE_ENV)
|
775 | */
|
776 | invalidate<T extends SimpleCacheKey>(callback: SimpleCacheCallback<T>): T;
|
777 | }
|
778 |
|
779 | // https://github.com/babel/babel/blob/v7.3.3/packages/babel-core/src/config/caching.js#L231
|
780 | export type SimpleCacheKey = string | boolean | number | null | undefined;
|
781 | export type SimpleCacheCallback<T extends SimpleCacheKey> = () => T;
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 | export interface EnvFunction {
|
791 | |
792 |
|
793 |
|
794 | (): string;
|
795 | |
796 |
|
797 |
|
798 | (envName: string | ReadonlyArray<string>): boolean;
|
799 |
|
800 |
|
801 |
|
802 | <T extends SimpleCacheKey>(envCallback: (envName: NonNullable<TransformOptions['envName']>) => T): T;
|
803 | }
|
804 |
|
805 | export type ConfigFunction = (api: ConfigAPI) => TransformOptions;
|
806 |
|
807 | export as namespace babel;
|