What is rollup-plugin-babel?
The rollup-plugin-babel package is a Rollup plugin that allows you to use Babel to transpile your JavaScript code. This is particularly useful for converting ES6+ code into a format that is compatible with older browsers or environments. The plugin integrates seamlessly with Rollup, making it easy to include Babel in your build process.
What are rollup-plugin-babel's main functionalities?
Transpile ES6+ Code
This feature allows you to transpile ES6+ code to ES5 using Babel. The code sample demonstrates how to set up Rollup with the Babel plugin to transpile code from the 'src/index.js' file and output it to 'dist/bundle.js'.
const babel = require('rollup-plugin-babel');
module.exports = {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
babel({
exclude: 'node_modules/**'
})
]
};
Custom Babel Configuration
This feature allows you to use a custom Babel configuration. The code sample shows how to specify Babel presets and plugins directly within the Rollup configuration.
const babel = require('rollup-plugin-babel');
module.exports = {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime']
})
]
};
Exclude Specific Files or Directories
This feature allows you to exclude specific files or directories from being transpiled by Babel. The code sample demonstrates how to exclude all files in the 'node_modules' directory.
const babel = require('rollup-plugin-babel');
module.exports = {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
babel({
exclude: 'node_modules/**'
})
]
};
Other packages similar to rollup-plugin-babel
rollup-plugin-typescript2
The rollup-plugin-typescript2 package is a Rollup plugin that allows you to use TypeScript in your Rollup build process. It offers similar functionality to rollup-plugin-babel but is specifically designed for TypeScript. It also provides better caching and incremental compilation compared to other TypeScript Rollup plugins.
rollup-plugin-sucrase
The rollup-plugin-sucrase package is a Rollup plugin that uses Sucrase to transform your code. Sucrase is a faster alternative to Babel for transforming modern JavaScript syntax. While it doesn't support as many transformations as Babel, it can be a good choice for projects that need faster build times.
rollup-plugin-esbuild
The rollup-plugin-esbuild package is a Rollup plugin that uses esbuild to transpile JavaScript and TypeScript code. Esbuild is known for its speed and efficiency, making it a good alternative to Babel for projects that prioritize build performance.
rollup-plugin-babel
Seamless integration between Rollup and Babel.
Why?
If you're using Babel to transpile your ES6/7 code and Rollup to generate a standalone bundle, you have a couple of options:
- run the code through Babel first, being careful to exclude the module transformer, or
- run the code through Rollup first, and then pass it to Babel.
Both approaches have disadvantages – in the first case, on top of the additional configuration complexity, you may end up with Babel's helpers (like classCallCheck
) repeated throughout your code (once for each module where the helpers are used). In the second case, transpiling is likely to be slower, because transpiling a large bundle is much more work for Babel than transpiling a set of small files.
Either way, you have to worry about a place to put the intermediate files, and getting sourcemaps to behave becomes a royal pain.
Using Rollup with rollup-plugin-babel makes the process far easier.
Installation
npm install --save-dev rollup-plugin-babel
Usage
import { rollup } from 'rollup';
import babel from 'rollup-plugin-babel';
rollup({
entry: 'main.js',
plugins: [
babel({
exclude: 'node_modules/**'
})
]
}).then(...)
All options are as per the Babel documentation, except options.include
and options.exclude
(each a minimatch pattern, or array of minimatch patterns), which determine which files are transpiled by Babel (by default, all files are transpiled).
Babel will respect .babelrc
files – this is generally the best place to put your configuration.
Babel 5
The latest version of rollup-plugin-babel is designed to work with Babel 6. To use rollup-plugin-babel with Babel 5, install a 1.x version:
npm install --save-dev rollup-plugin-babel@1
Babel 6
With Babel 5, rollup-plugin-babel overrides the configuration to ensure that module syntax is left alone and that external helpers are collected for inclusion at the top of the bundle.
Babel 6 works differently – there's no blacklist
or externalHelpers
options. Instead of using the es2015
preset, install and use babel-preset-es2015-rollup:
{
"presets": [ "es2015-rollup" ]
}
If you're not using the preset, be sure to include the external helpers plugin:
{
"plugins": [ "external-helpers-2" ]
}
License
MIT