当前位置: 首页 > 文档资料 > Babel 中文文档 >

@babel/core

优质
小牛编辑
137浏览
2023-12-01
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, transformSync

babel.transformSync(code: string, transformAsync

babel.transformAsync(code: string, transformFile

babel.transformFile(filename: string, transformFileSync

babel.transformFileSync(filename: string, transformFileAsync

babel.transformFileAsync(filename: string, transformFromAst

babel.transformFromAst(ast: Object, code?: string, transformFromAstSync

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

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

babel.parse(code: string, parseSync

babel.parseSync(code: string, parseAsync

babel.parseAsync(code: string, 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(loadPartialConfig

babel.loadPartialConfig(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.