UNPKG

7.42 kBMarkdownView Raw
1# babel-core
2
3> Babel compiler core.
4
5
6```javascript
7var babel = require("babel-core");
8import { transform } from 'babel-core';
9import * as babel from 'babel-core';
10```
11
12All transformations will use your local configuration files (.babelrc or in package.json). See [options](#options) to disable it.
13
14## babel.transform(code: string, [options?](#options): Object)
15
16Transforms the passed in `code`. Returning an object with the generated code,
17source map, and AST.
18
19```js
20babel.transform(code, options) // => { code, map, ast }
21```
22
23**Example**
24
25```js
26var result = babel.transform("code();", options);
27result.code;
28result.map;
29result.ast;
30```
31
32## babel.transformFile(filename: string, [options?](#options): Object, callback: Function)
33
34Asynchronously transforms the entire contents of a file.
35
36```js
37babel.transformFile(filename, options, callback)
38```
39
40**Example**
41
42```js
43babel.transformFile("filename.js", options, function (err, result) {
44 result; // => { code, map, ast }
45});
46```
47
48## babel.transformFileSync(filename: string, [options?](#options): Object)
49
50Synchronous version of `babel.transformFile`. Returns the transformed contents of
51the `filename`.
52
53```js
54babel.transformFileSync(filename, options) // => { code, map, ast }
55```
56
57**Example**
58
59```js
60babel.transformFileSync("filename.js", options).code;
61```
62
63## babel.transformFromAst(ast: Object, code?: string, [options?](#options): Object)
64
65Given, an [AST](https://astexplorer.net/), transform it.
66
67```js
68const code = "if (true) return;";
69const ast = babylon.parse(code, { allowReturnOutsideFunction: true });
70const { code, map, ast } = babel.transformFromAst(ast, code, options);
71```
72
73## Options
74
75> #### Babel CLI
76>
77> You can pass these options from the Babel CLI like so:
78>
79> `babel --name=value`
80
81Following is a table of the options you can use:
82
83| Option | Default | Description |
84| ------------------------ | -------------------- | ------------------------------- |
85| `ast` | `true` | Include the AST in the returned object |
86| `auxiliaryCommentAfter` | `null` | Attach a comment after all non-user injected code. |
87| `auxiliaryCommentBefore` | `null` | Attach a comment before all non-user injected code. |
88| `babelrc` | `true` | Specify whether or not to use .babelrc and .babelignore files. Not available when using the CLI, [use `--no-babelrc` instead](https://babeljs.io/docs/en/babel-cli#ignoring-babelrc). |
89| `code` | `true` | Enable code generation |
90| `comments` | `true` | Output comments in generated output. |
91| `compact` | `"auto"` | Do not include superfluous whitespace characters and line terminators. When set to `"auto"` compact is set to `true` on input sizes of >500KB. |
92| `env` | `{}` | This is an object of keys that represent different environments. For example, you may have: `{ env: { production: { /* specific options */ } } }` which will use those options when the environment variable `BABEL_ENV` is set to `"production"`. If `BABEL_ENV` isn't set then `NODE_ENV` will be used, if it's not set then it defaults to `"development"` |
93| `extends` | `null` | A path to an `.babelrc` file to extend |
94| `filename` | `"unknown"` | Filename for use in errors etc. |
95| `filenameRelative` | `(filename)` | Filename relative to `sourceRoot`. |
96| `generatorOpts` | `{}` | An object containing the options to be passed down to the babel code generator, babel-generator |
97| `getModuleId` | `null` | Specify a custom callback to generate a module id with. Called as `getModuleId(moduleName)`. If falsy value is returned then the generated module id is used. |
98| `highlightCode` | `true` | ANSI highlight syntax error code frames |
99| `ignore` | `null` | Opposite to the `only` option. `ignore` is disregarded if `only` is specified. |
100| `inputSourceMap` | `null` | A source map object that the output source map will be based on. |
101| `minified` | `false` | Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping `()` from `new` when safe) |
102| `moduleId` | `null` | Specify a custom name for module ids. |
103| `moduleIds` | `false` | If truthy, insert an explicit id for modules. By default, all modules are anonymous. (Not available for `common` modules) |
104| `moduleRoot` | `(sourceRoot)` | Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions. |
105| `only` | `null` | A [glob](https://github.com/isaacs/minimatch), 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 a non-matching file it's returned verbatim. |
106| `parserOpts` | `{}` | An object containing the options to be passed down to the babel parser, babylon |
107| `plugins` | `[]` | List of [plugins](https://babeljs.io/docs/en/plugins) to load and use. |
108| `presets` | `[]` | List of [presets](https://babeljs.io/docs/en/plugins#presets) (a set of plugins) to load and use. |
109| `retainLines` | `false` | 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) |
110| `resolveModuleSource` | `null` | Resolve a module source ie. `import "SOURCE";` to a custom value. Called as `resolveModuleSource(source, filename)`. |
111| `shouldPrintComment` | `null` | 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. |
112| `sourceFileName` | `(filenameRelative)` | Set `sources[0]` on returned source map. |
113| `sourceMaps` | `false` | 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"` then a `map` property is returned as well as a source map comment appended. **This does not emit sourcemap files by itself!** To have sourcemaps emitted using the CLI, you must pass it the `--source-maps` option. |
114| `sourceMapTarget` | `(filenameRelative)` | Set `file` on returned source map. |
115| `sourceRoot` | `(moduleRoot)` | The root from which all sources are relative. |
116| `sourceType` | `"module"` | Indicate the mode the code should be parsed in. Can be either "script" or "module". |
117| `wrapPluginVisitorMethod`| `null` | 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 `wrapPluginVisitorMethod(pluginAlias, visitorType, callback)`.