Babel ES2015 Rollup Preset
This package is a Babel preset specifically designed for use with Rollup, building upon `babel-preset-es2015`. Its primary function was to modify the standard ES2015 preset by disabling the transformation of ES module syntax to CommonJS (as Rollup handles ES modules natively) and enabling the `babel-plugin-external-helpers` to prevent helper code duplication. The latest version is 3.0.0, published nearly a decade ago. It is no longer actively maintained, and its base `babel-preset-es2015` has been deprecated since Babel v6 in favor of `@babel/preset-env`. Developers should migrate to modern Babel setups using `@babel/preset-env` with `@rollup/plugin-babel` for contemporary build workflows.
Common errors
-
Cannot find module 'babel-preset-es2015-rollup'
cause The package or one of its peer dependencies (like `babel-core` or `babel-preset-es2015`) is not installed or not resolvable by Babel.fixEnsure `babel-preset-es2015-rollup`, `babel-preset-es2015`, `babel-plugin-external-helpers`, and a compatible version of `babel-core` are installed as `devDependencies` in your project (`npm install --save-dev babel-preset-es2015-rollup babel-preset-es2015 babel-plugin-external-helpers babel-core`). -
The 'typeof' Babel helper is used more than once in your code. It's strongly recommended that you use the 'external-helpers' plugin or the 'es2015-rollup' preset.
cause Babel is inlining helper functions multiple times, indicating that `babel-plugin-external-helpers` (which is part of this preset) is either not configured correctly or is being overridden.fixEnsure `babel-preset-es2015-rollup` is correctly applied in your Babel configuration. If you're using `rollup-plugin-babel`, verify that your `.babelrc` is being picked up or pass the preset explicitly. For newer Babel versions, consider using `@babel/plugin-transform-runtime` with `@babel/runtime` and the `babelHelpers: 'runtime'` option in `@rollup/plugin-babel`. -
SyntaxError: Unexpected token import/export
cause Babel is not transforming ES module syntax, and the environment (e.g., Node.js without `--experimental-modules` or an old browser) doesn't natively support it, or a testing framework expects CommonJS. This preset intentionally disables module transformation, which can be an issue outside of Rollup's bundling process.fixIf this error occurs in a non-Rollup context (like Jest), ensure your Babel configuration for that environment *does* transform modules to CommonJS. If it's a Rollup output issue, verify your Rollup output format is correct for your target environment (e.g., `format: 'cjs'` for Node.js or `format: 'umd'` for browsers). The preset itself prevents module transformation, which is only suitable for Rollup's ES module input.
Warnings
- breaking The underlying `babel-preset-es2015` and all other yearly presets (es2016, es2017) are officially deprecated by Babel. Users are strongly encouraged to migrate to `@babel/preset-env` for future-proof and configurable Babel transpilation.
- breaking This preset is incompatible with Babel 7 and later versions due to significant changes in Babel's architecture, package naming (e.g., `@babel/*` scoped packages), and configuration options. Attempting to use it will lead to 'module not found' or configuration errors.
- gotcha This preset explicitly disables ES module transformation (`modules-commonjs`) from the base `es2015` preset, relying on Rollup to handle ES modules. If you use it in environments (like Jest tests) that expect CommonJS modules, or if other Babel plugins inadvertently re-enable module transformation, it will lead to import/require errors.
- gotcha The preset includes `babel-plugin-external-helpers`. If you are using `@rollup/plugin-babel` with `babelHelpers: 'bundled'` or `babelHelpers: 'runtime'`, this could lead to redundant helper inclusions or conflicts.
Install
-
npm install babel-preset-es2015-rollup -
yarn add babel-preset-es2015-rollup -
pnpm add babel-preset-es2015-rollup
Imports
- Preset in .babelrc
import es2015Rollup from 'babel-preset-es2015-rollup';
{ "presets": ["es2015-rollup"] } - Preset in Rollup config (via rollup-plugin-babel)
import es2015Rollup from 'babel-preset-es2015-rollup'; // ... babel({ presets: [es2015Rollup] })import babel from 'rollup-plugin-babel'; // ... in plugins array babel({ presets: ['es2015-rollup'] })
Quickstart
/* package.json */
{
"name": "my-rollup-project",
"version": "1.0.0",
"devDependencies": {
"rollup": "^0.60.0",
"rollup-plugin-babel": "^2.7.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-es2015": "^6.24.1",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-es2015-rollup": "^3.0.0"
}
}
/* .babelrc */
{
"presets": ["es2015-rollup"]
}
/* rollup.config.js */
import babel from 'rollup-plugin-babel';
export default {
input: 'src/main.js',
output: {
file: 'dist/bundle.js',
format: 'es'
},
plugins: [
babel({
exclude: 'node_modules/**',
// This tells rollup-plugin-babel to use the .babelrc configuration
// For older versions, babelrc: true might be needed explicitly
// For modern setups, prefer babelHelpers: 'bundled' or 'runtime' and @babel/preset-env
})
]
};
/* src/main.js */
export const greet = (name) => `Hello, ${name}!`;
export const add = (a, b) => a + b;
console.log(greet('World'));
console.log(`The sum is ${add(5, 3)}`);