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