Socket
Socket
Sign inDemoInstall

@babel/core

Package Overview
Dependencies
76
Maintainers
5
Versions
181
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @babel/core

Babel compiler core.


Version published
Weekly downloads
50M
decreased by-0.12%
Maintainers
5
Install size
4.17 MB
Created
Weekly downloads
 

Package description

What is @babel/core?

@babel/core is the core library of the Babel JavaScript compiler. It is primarily used for transforming ES6+ code into backwards compatible JavaScript that can be run by older JavaScript engines. Babel is widely used for its ability to transpile new JavaScript syntax, JSX, TypeScript, and for its plugin system that allows developers to use experimental features and customize the build process.

What are @babel/core's main functionalities?

Syntax Transformation

Transforms modern JavaScript syntax into a backwards compatible version. For example, it can convert ES6 arrow functions into regular ES5 functions.

require('@babel/core').transform('code', { presets: ['@babel/preset-env'] });

Plugin/Preset Application

Applies Babel plugins or presets to the code to enable specific transformations or sets of transformations.

require('@babel/core').transform('code', { plugins: ['@babel/plugin-transform-arrow-functions'] });

Source Map Generation

Generates source maps which help in debugging the original source code after it has been transformed by Babel.

require('@babel/core').transform('code', { sourceMaps: true });

Code Generation

Generates code from an Abstract Syntax Tree (AST). This is useful when you want to transform the AST directly or manipulate it before generating code.

require('@babel/core').transformFromAst(ast, 'code', { presets: ['@babel/preset-env'] });

Other packages similar to @babel/core

Readme

Source

@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 options to disable it.

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.

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

Example

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.

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

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

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

Example

var result = babel.transformSync("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, callback: Function)

Given an AST, transform it.

const sourceCode = "if (true) return;";
const parsedAst = babylon.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.

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

Given an AST, transform it.

const sourceCode = "if (true) return;";
const parsedAst = babylon.parse(sourceCode, { allowReturnOutsideFunction: true });
const { code, map, ast } = babel.transformFromAstSync(parsedAst, sourceCode, 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-babelrc instead
envNameenv varsDefaults to environment variable BABEL_ENV if set, or else NODE_ENV if set, or else it defaults to "development"
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 true on 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 envName is production
extendsnullA path to a .babelrc file 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 only option. ignore is disregarded if only is 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 new when 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 common modules)
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 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
parserOpts{}An object containing the options to be passed down to the babel parser, babylon
plugins[]List of plugins to 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)
shouldPrintCommentnullAn optional callback that controls whether a comment should be output or not. Called as shouldPrintComment(commentContents). NOTE: This overrides the comment option when used
sourceFileName(filenameRelative)Set sources[0] on returned source map
sourceMapsfalseIf 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
sourceMapTarget(filenameRelative)Set file on 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 one of "script", "module", or "unambiguous". "unambiguous" will make Babel attempt to guess, based on the presence of ES6 import or export statements. Files with ES6 imports and exports are considered "module" and are otherwise "script".
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).

Keywords

FAQs

Last updated on 08 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc