1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import { GeneratorOptions } from "@babel/generator";
|
10 | import traverse, { Visitor, NodePath } from "@babel/traverse";
|
11 | import template from "@babel/template";
|
12 | import * as t from "@babel/types";
|
13 | import { BabylonOptions } from "babylon";
|
14 |
|
15 | export {
|
16 | BabylonOptions,
|
17 | GeneratorOptions,
|
18 | t as types,
|
19 | template,
|
20 | traverse,
|
21 | Visitor
|
22 | };
|
23 |
|
24 | export type Node = t.Node;
|
25 | export type ParseResult = t.File | t.Program;
|
26 | export const version: string;
|
27 |
|
28 | export interface TransformOptions {
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | ast?: boolean | null;
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | auxiliaryCommentAfter?: string | null;
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | auxiliaryCommentBefore?: string | null;
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | root?: string | null;
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | configFile?: string | false | null;
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | babelrc?: boolean | null;
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | babelrcRoots?: true | string | string[] | null;
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | envName?: string | null;
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 | code?: boolean | null;
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | comments?: boolean | null;
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 | compact?: boolean | "auto" | null;
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | cwd?: string | null;
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | env?: { [index: string]: TransformOptions | null | undefined; } | null;
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 | extends?: string | null;
|
129 |
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 | filename?: string | null;
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | filenameRelative?: string | null;
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 | generatorOpts?: GeneratorOptions | null;
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | getModuleId?: ((moduleName: string) => string | null | undefined) | null;
|
157 |
|
158 | /**
|
159 | * ANSI highlight syntax error code frames
|
160 | *
|
161 | * Default: `true`
|
162 | */
|
163 | highlightCode?: boolean | null;
|
164 |
|
165 | /**
|
166 | * Opposite to the `only` option. `ignore` is disregarded if `only` is specified
|
167 | *
|
168 | * Default: `null`
|
169 | */
|
170 | ignore?: string[] | null;
|
171 |
|
172 | /**
|
173 | * A source map object that the output source map will be based on
|
174 | *
|
175 | * Default: `null`
|
176 | */
|
177 | inputSourceMap?: object | null;
|
178 |
|
179 | /**
|
180 | * Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping `()` from `new` when safe)
|
181 | *
|
182 | * Default: `false`
|
183 | */
|
184 | minified?: boolean | null;
|
185 |
|
186 | /**
|
187 | * Specify a custom name for module ids
|
188 | *
|
189 | * Default: `null`
|
190 | */
|
191 | moduleId?: string | null;
|
192 |
|
193 | /**
|
194 | * If truthy, insert an explicit id for modules. By default, all modules are anonymous. (Not available for `common` modules)
|
195 | *
|
196 | * Default: `false`
|
197 | */
|
198 | moduleIds?: boolean | null;
|
199 |
|
200 | /**
|
201 | * Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions
|
202 | *
|
203 | * Default: `(sourceRoot)`
|
204 | */
|
205 | moduleRoot?: string | null;
|
206 |
|
207 | /**
|
208 | * 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
|
209 | * a non-matching file it's returned verbatim
|
210 | *
|
211 | * Default: `null`
|
212 | */
|
213 | only?: string | RegExp | Array<string | RegExp> | null;
|
214 |
|
215 | /**
|
216 | * An object containing the options to be passed down to the babel parser, @babel/parser
|
217 | *
|
218 | * Default: `{}`
|
219 | */
|
220 | parserOpts?: BabylonOptions | null;
|
221 |
|
222 | /**
|
223 | * List of plugins to load and use
|
224 | *
|
225 | * Default: `[]`
|
226 | */
|
227 | plugins?: PluginItem[] | null;
|
228 |
|
229 | /**
|
230 | * List of presets (a set of plugins) to load and use
|
231 | *
|
232 | * Default: `[]`
|
233 | */
|
234 | presets?: PluginItem[] | null;
|
235 |
|
236 | /**
|
237 | * 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)
|
238 | *
|
239 | * Default: `false`
|
240 | */
|
241 | retainLines?: boolean | null;
|
242 |
|
243 | /**
|
244 | * 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
|
245 | *
|
246 | * Default: `null`
|
247 | */
|
248 | shouldPrintComment?: ((commentContents: string) => boolean) | null;
|
249 |
|
250 | /**
|
251 | * Set `sources[0]` on returned source map
|
252 | *
|
253 | * Default: `(filenameRelative)`
|
254 | */
|
255 | sourceFileName?: string | null;
|
256 |
|
257 | /**
|
258 | * 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"`
|
259 | * then a `map` property is returned as well as a source map comment appended. **This does not emit sourcemap files by itself!**
|
260 | *
|
261 | * Default: `false`
|
262 | */
|
263 | sourceMaps?: boolean | "inline" | "both" | null;
|
264 |
|
265 | /**
|
266 | * The root from which all sources are relative
|
267 | *
|
268 | * Default: `(moduleRoot)`
|
269 | */
|
270 | sourceRoot?: string | null;
|
271 |
|
272 | /**
|
273 | * 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
|
274 | * `import` or `export` statements. Files with ES6 `import`s and `export`s are considered `"module"` and are otherwise `"script"`.
|
275 | *
|
276 | * Default: `("module")`
|
277 | */
|
278 | sourceType?: "script" | "module" | "unambiguous" | null;
|
279 |
|
280 | /**
|
281 | * 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
|
282 | * `wrapPluginVisitorMethod(pluginAlias, visitorType, callback)`.
|
283 | */
|
284 | wrapPluginVisitorMethod?: ((pluginAlias: string, visitorType: "enter" | "exit", callback: (path: NodePath, state: any) => void) => (path: NodePath, state: any) => void) | null;
|
285 | }
|
286 |
|
287 | export type FileResultCallback = (err: Error | null, result: BabelFileResult | null) => any;
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | export function transform(code: string, callback: FileResultCallback): void;
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | export function transform(code: string, opts: TransformOptions | undefined, callback: FileResultCallback): void;
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | export function transform(code: string, opts?: TransformOptions): BabelFileResult | null;
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | export function transformSync(code: string, opts?: TransformOptions): BabelFileResult | null;
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | export function transformAsync(code: string, opts?: TransformOptions): Promise<BabelFileResult | null>;
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | export function transformFile(filename: string, callback: FileResultCallback): void;
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | export function transformFile(filename: string, opts: TransformOptions | undefined, callback: FileResultCallback): void;
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | export function transformFileSync(filename: string, opts?: TransformOptions): BabelFileResult | null;
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | export function transformFileAsync(filename: string, opts?: TransformOptions): Promise<BabelFileResult | null>;
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | export function transformFromAst(ast: Node, code: string | undefined, callback: FileResultCallback): void;
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | export function transformFromAst(ast: Node, code: string | undefined, opts: TransformOptions | undefined, callback: FileResultCallback): void;
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | export function transformFromAstSync(ast: Node, code?: string, opts?: TransformOptions): BabelFileResult | null;
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | export function transformFromAstSync(ast: Node, code?: string, opts?: TransformOptions): Promise<BabelFileResult | null>;
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | export interface PluginObj<S = {}> {
|
359 | name?: string;
|
360 | manipulateOptions?(opts: any, parserOpts: any): void;
|
361 | pre?(this: S, state: any): void;
|
362 | visitor: Visitor<S>;
|
363 | post?(this: S, state: any): void;
|
364 | inherits?: any;
|
365 | }
|
366 |
|
367 | export interface BabelFileResult {
|
368 | ast?: Node | null;
|
369 | code?: string | null;
|
370 | ignored?: boolean;
|
371 | map?: {
|
372 | version: number;
|
373 | sources: string[];
|
374 | names: string[];
|
375 | sourceRoot?: string;
|
376 | sourcesContent?: string[];
|
377 | mappings: string;
|
378 | file: string;
|
379 | } | null;
|
380 | metadata?: BabelFileMetadata;
|
381 | }
|
382 |
|
383 | export interface BabelFileMetadata {
|
384 | usedHelpers: string[];
|
385 | marked: Array<{
|
386 | type: string;
|
387 | message: string;
|
388 | loc: object;
|
389 | }>;
|
390 | modules: BabelFileModulesMetadata;
|
391 | }
|
392 |
|
393 | export interface BabelFileModulesMetadata {
|
394 | imports: object[];
|
395 | exports: {
|
396 | exported: object[];
|
397 | specifiers: object[];
|
398 | };
|
399 | }
|
400 |
|
401 | export type FileParseCallback = (err: Error | null, result: ParseResult | null) => any;
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 | export function parse(code: string, callback: FileParseCallback): void;
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | export function parse(code: string, options: TransformOptions | undefined, callback: FileParseCallback): void;
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | export function parse(code: string, options?: TransformOptions): ParseResult | null;
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | export function parseSync(code: string, options?: TransformOptions): ParseResult | null;
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | export function parseAsync(code: string, options?: TransformOptions): Promise<ParseResult | null>;
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | export function loadOptions(options?: TransformOptions): object | null;
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | export function loadPartialConfig(options?: TransformOptions): Readonly<PartialConfig> | null;
|
467 |
|
468 | export interface PartialConfig {
|
469 | options: TransformOptions;
|
470 | babelrc?: string;
|
471 | babelignore?: string;
|
472 | config?: string;
|
473 | }
|
474 |
|
475 | export interface ConfigItem {
|
476 | |
477 |
|
478 |
|
479 | name?: string;
|
480 |
|
481 | |
482 |
|
483 |
|
484 | value: object | ((...args: any[]) => any);
|
485 |
|
486 | /**
|
487 | * The options object passed to the plugin.
|
488 | */
|
489 | options?: object | false;
|
490 |
|
491 | /**
|
492 | * The path that the options are relative to.
|
493 | */
|
494 | dirname: string;
|
495 |
|
496 | /**
|
497 | * Information about the plugin's file, if Babel knows it.
|
498 | * *
|
499 | */
|
500 | file?: {
|
501 | /**
|
502 | * The file that the user requested, e.g. `"@babel/env"`
|
503 | */
|
504 | request: string;
|
505 |
|
506 | /**
|
507 | * The full path of the resolved file, e.g. `"/tmp/node_modules/@babel/preset-env/lib/index.js"`
|
508 | */
|
509 | resolved: string;
|
510 | } | null;
|
511 | }
|
512 |
|
513 | export type PluginOptions = object | undefined | false;
|
514 |
|
515 | export type PluginTarget = string | object | ((...args: any[]) => any);
|
516 |
|
517 | export type PluginItem = ConfigItem | PluginObj<any> | PluginTarget | [PluginTarget, PluginOptions] | [PluginTarget, PluginOptions, string | undefined];
|
518 |
|
519 | export interface CreateConfigItemOptions {
|
520 | dirname?: string;
|
521 | type?: "preset" | "plugin";
|
522 | }
|
523 |
|
524 | /**
|
525 | * Allows build tooling to create and cache config items up front. If this function is called multiple times for a
|
526 | * given plugin, Babel will call the plugin's function itself multiple times. If you have a clear set of expected
|
527 | * plugins and presets to inject, pre-constructing the config items would be recommended.
|
528 | */
|
529 | export function createConfigItem(value: PluginTarget | [PluginTarget, PluginOptions] | [PluginTarget, PluginOptions, string | undefined], options?: CreateConfigItemOptions): ConfigItem;
|
530 |
|
531 | export as namespace babel;
|
532 |
|
\ | No newline at end of file |