UNPKG

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