@andywer/babel-core-dctypes

Babel compiler core.

babel-core

Babel compiler core.

var babel = require("babel-core");
import { transform } from 'babel-core';
import * as babel from 'babel-core';

All transformations will use your local configuration files (.babelrc or in package.json). See optionsto disable it.

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

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

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

Example

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

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

Asynchronously transforms the entire contents of a file.

babel.transformFile(filename, options, callback)

Example

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

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

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

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

Example

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

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

Given, an AST, transform it.

const code = "if (true) return;";
const ast = babylon.parse(code, { allowReturnOutsideFunction: true });
const { code, map, ast } = babel.transformFromAst(ast, code, options);

Options

Babel CLI

You can pass these options from the Babel CLI like so:

babel --name=value

Following is a table of the options you can use:

OptionDefaultDescription
asttrueInclude the AST in the returned object
auxiliaryCommentAfternullAttach a comment after all non-user injected code.
auxiliaryCommentBeforenullAttach a comment before all non-user injected code.
babelrctrueSpecify whether or not to use .babelrc and .babelignore files. Not available when using the CLI, use --no-babelrcinstead.
codetrueEnable code generation
commentstrueOutput comments in generated output.
compact"auto"Do not include superfluous whitespace characters and line terminators. When set to "auto"compact is set to trueon input sizes of >500KB.
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_ENVis set to "production". If BABEL_ENVisn't set then NODE_ENVwill be used, if it's not set then it defaults to "development"
extendsnullA path to an .babelrcfile to extend
filename"unknown"Filename for use in errors etc.
filenameRelative(filename)Filename relative to sourceRoot.
generatorOpts{}An object containing the options to be passed down to the babel code generator, babel-generator
getModuleIdnullSpecify 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.
highlightCodetrueANSI highlight syntax error code frames
ignorenullOpposite to the onlyoption. ignoreis disregarded if onlyis specified.
inputSourceMapnullA source map object that the output source map will be based on.
minifiedfalseShould the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping ()from newwhen safe)
moduleIdnullSpecify a custom name for module ids.
moduleIdsfalseIf truthy, insert an explicit id for modules. By default, all modules are anonymous. (Not available for commonmodules)
moduleRoot(sourceRoot)Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions.
onlynullA glob, regex, or mixed array of both, matching paths to onlycompile. Can also be an array of arrays containing paths to explicitly match. When attempting to compile a non-matching file it's returned verbatim.
parserOpts{}An object containing the options to be passed down to the babel parser, babylon
plugins[]List of pluginsto load and use.
presets[]List of presets(a set of plugins) to load and use.
retainLinesfalseRetain 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)
resolveModuleSourcenullResolve a module source ie. import "SOURCE";to a custom value. Called as resolveModuleSource(source, filename).
shouldPrintCommentnullAn optional callback that controls whether a comment should be output or not. Called as shouldPrintComment(commentContents). NOTE:This overrides the commentoption when used.
sourceFileName(filenameRelative)Set sources[0]on returned source map.
sourceMapsfalseIf truthy, adds a mapproperty 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 mapproperty 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-mapsoption.
sourceMapTarget(filenameRelative)Set fileon returned source map.
sourceRoot(moduleRoot)The root from which all sources are relative.
sourceType"module"Indicate the mode the code should be parsed in. Can be either "script" or "module".
wrapPluginVisitorMethodnullAn 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).

HomePage

https://babeljs.io/

Repository

https://github.com/babel/babel/tree/master/packages/babel-core


上一篇:@glimmer/build
下一篇:babel-template

相关推荐

官方社区

扫码加入 JavaScript 社区