Babel compiler core providing programmatic JavaScript code transformation API with plugin support and source maps
npx @tessl/cli install tessl/npm-babel-core@6.26.0Babel Core is the programmatic API for the Babel JavaScript transpiler, providing comprehensive code transformation capabilities. It offers synchronous and asynchronous methods for transforming JavaScript code strings, files, and ASTs, with full plugin system integration, source map generation, and extensive configuration options.
npm install babel-coreconst babel = require("babel-core");
const {
transform, transformFile, transformFileSync, transformFromAst,
analyse, File, Pipeline, OptionManager, buildExternalHelpers,
template, traverse, types, messages, util, options,
resolvePlugin, resolvePreset, version
} = babel;ES6 imports:
import {
transform, transformFile, transformFileSync, transformFromAst,
analyse, File, Pipeline, OptionManager, buildExternalHelpers,
template, traverse, types, messages, util, options,
resolvePlugin, resolvePreset, version
} from 'babel-core';
import * as babel from 'babel-core';const babel = require("babel-core");
// Transform code string
const result = babel.transform("const greeting = () => 'Hello World';", {
presets: ["es2015"]
});
console.log(result.code); // transformed code
console.log(result.map); // source map
console.log(result.ast); // AST
// Transform file
babel.transformFile("input.js", { presets: ["es2015"] }, (err, result) => {
if (err) throw err;
console.log(result.code);
});
// Transform file synchronously
const fileResult = babel.transformFileSync("input.js", { presets: ["es2015"] });
console.log(fileResult.code);Babel Core is built around several key components:
Pipeline classFile class managing code parsing, transformation, and generationOptionManager for processing and normalizing configurationCore synchronous and asynchronous transformation functions for JavaScript code strings and files. Essential for build tools, CLI applications, and development servers.
function transform(code, opts);
function transformFile(filename, opts, callback);
function transformFileSync(filename, opts);Advanced AST-based transformations and analysis operations for direct AST manipulation and programmatic code analysis.
function transformFromAst(ast, code, opts);
function analyse(code, opts, visitor);Browser-specific API for client-side JavaScript transformation, script loading, and automatic transpilation of script tags.
function run(code, opts);
function load(url, callback, opts, hold);Comprehensive plugin loading, configuration management, and preset system for extensible transformations.
class OptionManager {
constructor(log, pipeline);
init(opts);
}
const options; // Configuration schema object
function resolvePlugin(name);
function resolvePreset(name);Helper functions for file type checking, option processing, and build tool integration.
const util = {
canCompile(filename, altExts),
list(val),
regexify(val),
arrayify(val, mapFn),
booleanify(val),
shouldIgnore(filename, ignore, only)
};
function buildExternalHelpers(whitelist, outputType);Babel Core accepts extensive configuration options:
ast, code, comments, compact, env, filenameplugins, presets, sourceMaps, sourceTypebabelrc, ignore, only, retainLinesparserOpts, generatorOpts, resolveModuleSourceFull configuration documentation available in individual capability sections.
Babel Core re-exports several essential Babel packages:
// Re-exported from external packages
const template; // from babel-template
const traverse; // from babel-traverse
const types; // from babel-types (as 't')
const messages; // from babel-messages
const version; // package version stringThese provide template parsing, AST traversal, type checking, error messaging, and version information.