@babel/core

    Transforms the passed in . Calling a callback with an object with the generated code,source map, and AST.

    1. babel.transform(code, options, function(err, result) {
    2. result; // => { code, map, ast }
    3. });

    Example

    1. babel.transform("code();", options, function(err, result) {
    2. result.map;
    3. result.ast;
    4. });

    Compat Note:

    In Babel 6, this method was synchronous and transformSync did not exist. For backward-compatibility, this function will behave synchronously if no callback is given. If you're starting with Babel 7 and need synchronous behavior, please use transformSync since this backward-compat may be dropped in future major versions of Babel.

    transformSync

    babel.transformSync(code: string, : Object)

    Transforms the passed in code. Returning an object with the generated code,source map, and AST.

    1. babel.transformSync(code, options) // => { code, map, ast }

    Example

    1. var result = babel.transformSync("code();", options);
    2. result.code;
    3. result.map;
    4. result.ast;

    transformAsync

    babel.transformAsync(code: string, : Object)

    Transforms the passed in code. Returning an promise for an object with thegenerated code, source map, and AST.

    Example

    1. babel.transformAsync("code();", options).then(result => {
    2. result.code;
    3. result.map;
    4. result.ast;
    5. });

    transformFile

    babel.transformFile(filename: string, : Object, callback: Function)

    Asynchronously transforms the entire contents of a file.

    1. babel.transformFile(filename, options, callback)

    Example

    1. babel.transformFile("filename.js", options, function (err, result) {
    2. result; // => { code, map, ast }
    3. });

    transformFileSync

    babel.transformFileSync(filename: string, : Object)

    1. babel.transformFileSync(filename, options) // => { code, map, ast }

    Example

    Promise version of babel.transformFile. Returns a promise for the transformedcontents of the filename.

    1. babel.transformFileAsync(filename, options) // => Promise<{ code, map, ast }>

    Example

    1. babel.transformFileAsync("filename.js", options).then(result => {
    2. result.code;
    3. });

    transformFromAst

    babel.transformFromAst(ast: Object, code?: string, : Object, callback: Function): FileNode | null

    Given an AST, transform it.

    1. const sourceCode = "if (true) return;";
    2. babel.transformFromAst(parsedAst, sourceCode, options, function(err, result) {
    3. const { code, map, ast } = result;
    4. });

    Compat Note:

    In Babel 6, this method was synchronous and transformFromAstSync did not exist. For backward-compatibility, this function will behave synchronously if no callback is given. If you're starting with Babel 7 and need synchronous behavior, please use transformFromAstSync since this backward-compat may be dropped in future major versions of Babel.

    transformFromAstSync

    babel.transformFromAstSync(ast: Object, code?: string, options?: Object)

    Given an , transform it.

    1. const sourceCode = "if (true) return;";
    2. const parsedAst = babel.parse(sourceCode, { allowReturnOutsideFunction: true });
    3. const { code, map, ast } = babel.transformFromAstSync(parsedAst, sourceCode, options);

    transformFromAstAsync

    babel.transformFromAstAsync(ast: Object, code?: string, : Object)

    Given an AST, transform it.

    parse

    babel.parse(code: string, options?: Object, callback: Function)

    Given some code, parse it using Babel's standard behavior. Referenced presets andplugins will be loaded such that optional syntax plugins are automaticallyenabled.

    babel.parseSync(code: string, : Object)

    Returns an AST.

    Given some code, parse it using Babel's standard behavior. Referenced presets andplugins will be loaded such that optional syntax plugins are automaticallyenabled.

    parseAsync

    babel.parseAsync(code: string, : Object)

    Returns a promise for an AST.

    Given some code, parse it using Babel's standard behavior. Referenced presets andplugins will be loaded such that optional syntax plugins are automaticallyenabled.

    Advanced APIs

    Many systems that wrap Babel like to automatically inject plugins and presets,or override options. To accomplish this goal, Babel exposes several functionsthat aid in loading the configuration part-way without transforming.

    babel.loadOptions(: Object)

    Resolve Babel's options fully, resulting in an options object where:

    • opts.plugins is a full list of Plugin instances.
    • opts.presets is empty and all presets are flattened into opts.
    • It can be safely passed back to Babel. Fields like "babelrc" have been set tofalse so that later calls to Babel will not make a second attempt to loadconfig files. instances aren't meant to be manipulated directly, but oftencallers will serialize this opts to JSON to use it as a cache key representingthe options Babel has received. Caching on this isn't 100% guaranteed toinvalidate properly, but it is the best we have at the moment.

    babel.loadPartialConfig(: Object): PartialConfig

    To allow systems to easily manipulate and validate a user's config, this functionresolves the plugins and presets and proceeds no further. The expectation isthat callers will take the config's .options, manipulate it as they see fitand pass it back to Babel again.

    • babelrc: string | void - The path of the file-relative configuration file, if there was one.
    • babelignore: string | void - The path of the .babelignore file, if there was one.
    • config: string | void - The path of the file, if there was one.
    • options: ValidatedOptions - The partially resolved options, which can be manipulated and passed back to Babel again.
      • plugins: Array<ConfigItem> - See below.
      • presets: Array<ConfigItem> - See below.
      • It can be safely passed back to Babel. Options like "babelrc" have been setto false so that later calls to Babel will not make a second attempt toload config files.
    • hasFilesystemConfig(): boolean - Check if the resolved config loaded any settings from the filesystem. instances expose properties to introspect the values, but eachitem should be treated as immutable. If changes are desired, the item should beremoved from the list and replaced with either a normal Babel config value, orwith a replacement item created by babel.createConfigItem. See thatfunction for information about ConfigItem fields.

    babel.createConfigItem(value: string | {} | Function | [string | {} | Function, {} | void], { dirname?: string, type?: "preset" | "plugin" }): ConfigItem

    Allows build tooling to create and cache config items up front. If this functionis called multiple times for a given plugin, Babel will call the plugin's function itselfmultiple times. If you have a clear set of expected plugins and presets toinject, pre-constructing the config items would be recommended.

    Each ConfigItem exposes all of the information Babel knows. The fields are:

    • value: {} | Function - The resolved value of the plugin.
    • options: {} | void - The options object passed to the plugin.
    • dirname: string - The path that the options are relative to.
    • name: string | void - The name that the user gave the plugin instance, e.g. plugins: [ ['env', {}, 'my-env'] ]
    • file: Object | void - Information about the plugin's file, if Babel knows it.
      • request: string - The file that the user requested, e.g. "@babel/env"

    DEFAULT_EXTENSIONS

    Options

    See the full option list here.