core


 javascript
var babel = require("@babel/core");
import { transform } from "@babel/core";
import * as babel from "@babel/core";

All transformations will use your local configuration files.

transform

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

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

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

Js中文网 – 前端进阶资源教程 www.javascriptC.com ,Babel中文文档
一个致力于帮助开发者用代码改变世界为使命的平台

Example

 js
babel.transform("code();", options, function(err, result) {
  result.code;
  result.map;
  result.ast;
});

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, options?: Object)

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

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

Example

 js
var result = babel.transformSync("code();", options);
result.code;
result.map;
result.ast;

transformAsync

babel.transformAsync(code: string, options?: Object)

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

 js
babel.transformAsync(code, options) // => Promise<{ code, map, ast }>

Example

 js
babel.transformAsync("code();", options).then(result => {
  result.code;
  result.map;
  result.ast;
});

transformFile

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

Asynchronously transforms the entire contents of a file.

Js中文网 – 前端进阶资源教程 www.javascriptC.com ,Babel中文文档
一个致力于帮助开发者用代码改变世界为使命的平台

 js
babel.transformFile(filename, options, callback)

Example

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

transformFileSync

babel.transformFileSync(filename: string, options?: Object)

Synchronous version of babel.transformFile. Returns the transformed contents of the filename.

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

Example

 js
babel.transformFileSync("filename.js", options).code;

transformFileAsync

babel.transformFileAsync(filename: string, options?: Object)

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

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

Example

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

transformFromAst

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

Given an AST, transform it.

 js
const sourceCode = "if (true) return;";
const parsedAst = babel.parse(sourceCode, { allowReturnOutsideFunction: true });
babel.transformFromAst(parsedAst, sourceCode, options, function(err, result) {
  const { code, map, ast } = result;
});

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 AST, transform it.

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

transformFromAstAsync

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

Given an AST, transform it.

 js
const sourceCode = "if (true) return;";
babel.parseAsync(sourceCode, { allowReturnOutsideFunction: true })
  .then(parsedAst => {
    return babel.transformFromAstSync(parsedAst, sourceCode, options);
  })
  .then(({ code, map, ast }) => {
    // ...
  });

Js中文网 – 前端进阶资源教程 www.javascriptC.com ,Babel中文文档
一个致力于帮助开发者用代码改变世界为使命的平台

parse

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

Given some code, parse it using Babel’s standard behavior. Referenced presets and plugins will be loaded such that optional syntax plugins are automatically enabled.

Compat Note:

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

parseSync

babel.parseSync(code: string, options?: Object)

Returns an AST.

Given some code, parse it using Babel’s standard behavior. Referenced presets and plugins will be loaded such that optional syntax plugins are automatically enabled.

parseAsync

babel.parseAsync(code: string, options?: Object)

Returns a promise for an AST.

Given some code, parse it using Babel’s standard behavior. Referenced presets and plugins will be loaded such that optional syntax plugins are automatically enabled.

Advanced APIs

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

loadOptions

babel.loadOptions(options?: 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 to false so that later calls to Babel will not make a second attempt to load config files.

Plugin instances aren’t meant to be manipulated directly, but often callers will serialize this opts to JSON to use it as a cache key representing the options Babel has received. Caching on this isn’t 100% guaranteed to invalidate properly, but it is the best we have at the moment.

loadPartialConfig

babel.loadPartialConfig(options?: Object): PartialConfig

To allow systems to easily manipulate and validate a user’s config, this function resolves the plugins and presets and proceeds no further. The expectation is that callers will take the config’s .options, manipulate it as they see fit and 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 project-wide config file 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 set to false so that later calls to Babel will not make a second attempt to load config files.
  • hasFilesystemConfig(): boolean – Check if the resolved config loaded any settings from the filesystem.

ConfigItem instances expose properties to introspect the values, but each item should be treated as immutable. If changes are desired, the item should be removed from the list and replaced with either a normal Babel config value, or with a replacement item created by babel.createConfigItem. See that function for information about ConfigItem fields.

createConfigItem

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 function is called multiple times for a given plugin, Babel will call the plugin’s function itself multiple times. If you have a clear set of expected plugins and presets to inject, pre-constructing the config items would be recommended.

ConfigItem type

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"
    • resolved: string – The full path of the resolved file, e.g. "/tmp/node_modules/@babel/preset-env/lib/index.js"

Options

See the full option list here.

← parsergenerator →

来源:Js中文网 – 前端进阶资源教程
链接:https://www.javascriptc.com/docs/babel-manual/babel-core

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「IT平头哥联盟」,公众号后台回复「资源」 免费领取我精心整理的前端进阶资源教程

JS中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。欢迎热爱技术的你一起加入交流与学习,JS中文网的使命是帮助开发者用代码改变世界