UNPKG

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