babel-preset-es2015 is a comprehensive Babel preset that provides a complete collection of transformation plugins for converting ES2015 (ES6) JavaScript features to ES5-compatible code. It includes 24 individual transformation plugins covering all ES2015 language features like arrow functions, classes, destructuring, template literals, modules, and more.
Deprecation Notice: This package has been deprecated since Babel 7.0.0 in favor of @babel/preset-env, which provides more intelligent targeting based on supported environments rather than language version.
npm install --save-dev babel-preset-es2015This preset is typically used via Babel configuration files rather than direct imports:
.babelrc
{
"presets": ["es2015"]
}babel.config.js
module.exports = {
presets: ["es2015"]
};For direct imports (advanced usage):
const es2015 = require("babel-preset-es2015");{
"presets": ["es2015"]
}{
"presets": [
["es2015", {
"loose": true,
"modules": false,
"spec": true
}]
]
}const babel = require("babel-core");
// Using preset by name (most common)
const result = babel.transform(code, {
presets: ["es2015"]
});
// Using with options
const resultWithOptions = babel.transform(code, {
presets: [["es2015", { loose: true, modules: "amd" }]]
});
// Direct preset import (advanced)
const es2015 = require("babel-preset-es2015");
const directResult = babel.transform(code, {
presets: [es2015]
});babel-preset-es2015 follows Babel's preset architecture:
buildPreset function creates customized configurationsThe main export is a preset configuration object with default ES2015 transformation settings.
// Default export - preset configuration object with buildPreset property
interface PresetConfig {
plugins: Array<PluginConfiguration>;
buildPreset?: (context: any, options?: PresetOptions) => { plugins: Array<PluginConfiguration> };
}Usage:
const es2015 = require("babel-preset-es2015");
console.log(es2015.plugins); // Array of plugin configurations
console.log(typeof es2015.buildPreset); // "function"The preset object has a buildPreset property function for creating customized configurations.
/**
* Property function on the default export for creating customized preset configurations
* @param context - Babel context object (typically null)
* @param options - Configuration options
* @returns Preset configuration object with plugins array
*/
preset.buildPreset(
context: any,
options?: PresetOptions
): { plugins: Array<PluginConfiguration> };
interface PresetOptions {
/** Enable loose transformations for faster, less spec-compliant output */
loose?: boolean;
/** Module transformation type or false to disable module transforms */
modules?: "commonjs" | "amd" | "umd" | "systemjs" | false;
/** Enable spec-compliant transformations for more accurate ES2015 behavior */
spec?: boolean;
}Usage:
const es2015 = require("babel-preset-es2015");
// Create custom preset using buildPreset property
const customPreset = es2015.buildPreset(null, {
loose: true,
modules: "amd",
spec: false
});
// Use in Babel configuration
const babel = require("babel-core");
const result = babel.transform(code, {
presets: [customPreset]
});Controls the transformation style for plugins that support loose mode.
interface LooseOption {
/** When true, enables faster but less spec-compliant transformations */
loose: boolean; // default: false
}Effect: Loose mode produces simpler, faster code but may not handle all edge cases according to ES2015 specification.
Examples:
Controls ES6 module transformation to different module systems.
interface ModulesOption {
/** Target module system or false to preserve ES6 modules */
modules: "commonjs" | "amd" | "umd" | "systemjs" | false; // default: "commonjs"
}Module Types:
"commonjs": Node.js style require()/module.exports"amd": Asynchronous Module Definition for RequireJS"umd": Universal Module Definition for browsers and Node.js"systemjs": SystemJS dynamic module loader formatfalse: Preserve ES6 import/export statementsControls whether to use spec-compliant transformations.
interface SpecOption {
/** When true, uses more spec-compliant but potentially slower transformations */
spec: boolean; // default: false
}Effect: Spec mode produces code that more closely follows ES2015 specification behavior, but may be larger and slower.
The preset validates options and throws descriptive errors for invalid configurations:
// Thrown for invalid loose option
throw new Error("Preset es2015 'loose' option must be a boolean.");
// Thrown for invalid spec option
throw new Error("Preset es2015 'spec' option must be a boolean.");
// Thrown for invalid modules option
throw new Error(
"Preset es2015 'modules' option must be 'false' to indicate no modules\n" +
"or a module type which be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'"
);The preset includes these 24 ES2015 transformation plugins in execution order:
interface PluginConfiguration {
/** Plugin function or [plugin, options] array */
0: Function | [Function, object];
}
interface PresetExport {
/** Array of Babel plugin configurations */
plugins: Array<PluginConfiguration>;
/** Property function to create custom preset configurations (non-enumerable) */
buildPreset?: (context: any, options?: PresetOptions) => { plugins: Array<PluginConfiguration> };
}