UNPKG

7.53 kBTypeScriptView Raw
1import * as t from "babel-types";
2export { t as types };
3export type Node = t.Node;
4export import template = require("babel-template");
5export const version: string;
6import traverse, { NodePath, Visitor } from "babel-traverse";
7export { traverse, Visitor };
8import { BabylonOptions } from "babylon";
9export { BabylonOptions };
10import { GeneratorOptions } from "babel-generator";
11export { GeneratorOptions };
12
13// A babel plugin is a simple function which must return an object matching
14// the following interface. Babel will throw if it finds unknown properties.
15// The list of allowed plugin keys is here:
16// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-core/src/config/option-manager.js#L71
17export interface PluginObj<S = {}> {
18 name?: string | undefined;
19 manipulateOptions?(opts: any, parserOpts: any): void;
20 pre?(this: S, state: any): void;
21 visitor: Visitor<S>;
22 post?(this: S, state: any): void;
23 inherits?: any;
24}
25
26/** Transforms the passed in `code`. Returning an object with the generated code, source map, and AST. */
27export function transform(code: string, opts?: TransformOptions): BabelFileResult;
28
29/** Asynchronously transforms the entire contents of a file. */
30export function transformFile(
31 filename: string,
32 opts: TransformOptions,
33 callback: (err: any, result: BabelFileResult) => void,
34): void;
35
36/** Synchronous version of `babel.transformFile`. Returns the transformed contents of the `filename`. */
37export function transformFileSync(filename: string, opts?: TransformOptions): BabelFileResult;
38
39export function transformFromAst(ast: Node, code?: string, opts?: TransformOptions): BabelFileResult;
40
41export interface TransformOptions {
42 /** Include the AST in the returned object. Default: `true`. */
43 ast?: boolean | undefined;
44
45 /** Attach a comment after all non-user injected code. */
46 auxiliaryCommentAfter?: string | undefined;
47
48 /** Attach a comment before all non-user injected code. */
49 auxiliaryCommentBefore?: string | undefined;
50
51 /** Specify whether or not to use `.babelrc` and `.babelignore` files. Default: `true`. */
52 babelrc?: boolean | undefined;
53
54 /** Enable code generation. Default: `true`. */
55 code?: boolean | undefined;
56
57 /** write comments to generated output. Default: `true`. */
58 comments?: boolean | undefined;
59
60 /**
61 * Do not include superfluous whitespace characters and line terminators. When set to `"auto"`, `compact` is set to
62 * `true` on input sizes of >100KB.
63 */
64 compact?: boolean | "auto" | undefined;
65
66 /**
67 * This is an object of keys that represent different environments. For example, you may have:
68 * `{ env: { production: { / * specific options * / } } }`
69 * which will use those options when the enviroment variable `BABEL_ENV` is set to `"production"`.
70 * If `BABEL_ENV` isn't set then `NODE_ENV` will be used, if it's not set then it defaults to `"development"`.
71 */
72 env?: object | undefined;
73
74 /** A path to an .babelrc file to extend. */
75 extends?: string | undefined;
76
77 /** Filename to use when reading from stdin - this will be used in source-maps, errors etc. Default: "unknown". */
78 filename?: string | undefined;
79
80 /** Filename relative to `sourceRoot`. */
81 filenameRelative?: string | undefined;
82
83 /** An object containing the options to be passed down to the babel code generator, babel-generator. Default: `{}` */
84 generatorOpts?: GeneratorOptions | undefined;
85
86 /**
87 * Specify a custom callback to generate a module id with. Called as `getModuleId(moduleName)`.
88 * If falsy value is returned then the generated module id is used.
89 */
90 getModuleId?(moduleName: string): string;
91
92 /** Enable/disable ANSI syntax highlighting of code frames. Default: `true`. */
93 highlightCode?: boolean | undefined;
94
95 /** list of glob paths to **not** compile. Opposite to the `only` option. */
96 ignore?: string[] | undefined;
97
98 /** A source map object that the output source map will be based on. */
99 inputSourceMap?: object | undefined;
100
101 /** Should the output be minified. Default: `false` */
102 minified?: boolean | undefined;
103
104 /** Specify a custom name for module ids. */
105 moduleId?: string | undefined;
106
107 /**
108 * If truthy, insert an explicit id for modules. By default, all modules are anonymous.
109 * (Not available for `common` modules).
110 */
111 moduleIds?: boolean | undefined;
112
113 /** Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions. */
114 moduleRoot?: string | undefined;
115
116 /**
117 * A glob, regex, or mixed array of both, matching paths to only compile. Can also be an array of arrays containing
118 * paths to explicitly match. When attempting to compile a non-matching file it's returned verbatim.
119 */
120 only?: string | RegExp | Array<string | RegExp> | undefined;
121
122 /** Babylon parser options. */
123 parserOpts?: BabylonOptions | undefined;
124
125 /** List of plugins to load and use. */
126 plugins?: any[] | undefined;
127
128 /** List of presets (a set of plugins) to load and use. */
129 presets?: any[] | undefined;
130
131 /** Retain line numbers - will result in really ugly code. Default: `false` */
132 retainLines?: boolean | undefined;
133
134 /** Resolve a module source ie. import "SOURCE"; to a custom value. */
135 resolveModuleSource?(source: string, filename: string): string;
136
137 /**
138 * An optional callback that controls whether a comment should be output or not. Called as
139 * `shouldPrintComment(commentContents)`. **NOTE**: This overrides the `comments` option when used.
140 */
141 shouldPrintComment?(comment: string): boolean;
142
143 /** Set `sources[0]` on returned source map. */
144 sourceFileName?: string | undefined;
145
146 /**
147 * If truthy, adds a `map` property to returned output. If set to `"inline"`, a comment with a `sourceMappingURL`
148 * directive is added to the bottom of the returned code. If set to `"both"` then a map property is returned as well
149 * as a source map comment appended.
150 */
151 sourceMaps?: boolean | "inline" | "both" | undefined;
152
153 /** Set `file` on returned source map. */
154 sourceMapTarget?: string | undefined;
155
156 /** The root from which all sources are relative. */
157 sourceRoot?: string | undefined;
158
159 /** Indicate the mode the code should be parsed in. Can be either “script” or “module”. Default: "module" */
160 sourceType?: "script" | "module" | undefined;
161
162 /**
163 * An optional callback that can be used to wrap visitor methods.
164 * NOTE: This is useful for things like introspection, and not really needed for implementing anything.
165 */
166 wrapPluginVisitorMethod?(
167 pluginAlias: string,
168 visitorType: "enter" | "exit",
169 callback: (path: NodePath, state: any) => void,
170 ): (path: NodePath, state: any) => void;
171}
172
173export interface BabelFileModulesMetadata {
174 imports: object[];
175 exports: {
176 exported: object[];
177 specifiers: object[];
178 };
179}
180
181export interface BabelFileMetadata {
182 usedHelpers: string[];
183 marked: Array<{
184 type: string;
185 message: string;
186 loc: object;
187 }>;
188 modules: BabelFileModulesMetadata;
189}
190
191export interface BabelFileResult {
192 ast?: Node | undefined;
193 code?: string | undefined;
194 ignored?: boolean | undefined;
195 map?: object | undefined;
196 metadata?: BabelFileMetadata | undefined;
197}
198
199export as namespace babel;