Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
rollup-plugin-babel
Advanced tools
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.
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/**'
})
]
};
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.
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.
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.
Seamless integration between Rollup and Babel.
If you're using Babel to transpile your ES6/7 code and Rollup to generate a standalone bundle, you have a couple of options:
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.
babel 7.x
npm install --save-dev rollup-plugin-babel@latest
babel 6.x
npm install --save-dev rollup-plugin-babel@3
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, plus the following:
options.externalHelpers
: a boolean value indicating whether to bundle in the Babel helpersoptions.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)options.externalHelpersWhitelist
: an array which gives explicit control over which babelHelper functions are allowed in the bundle (by default, every helper is allowed)options.extensions
: an array of file extensions that Babel should transpile (by default the Babel defaults of .js, .jsx, .es6, .es, .mjs. are used)Babel will respect .babelrc
files – this is generally the best place to put your configuration.
Ideally, you should only be transforming your source code, rather than running all of your external dependencies through Babel – hence the exclude: 'node_modules/**'
in the example above. If you have a dependency that exposes untranspiled ES6 source code that doesn't run in your target environment, then you may need to break this rule, but it often causes problems with unusual .babelrc
files or mismatched versions of Babel.
We encourage library authors not to distribute code that uses untranspiled ES6 features (other than modules) for this reason. Consumers of your library should not have to transpile your ES6 code, any more than they should have to transpile your CoffeeScript, ClojureScript or TypeScript.
Use babelrc: false
to prevent Babel from using local (i.e. to your external dependencies) .babelrc
files, relying instead on the configuration you pass in.
In some cases Babel uses helpers to avoid repeating chunks of code – for example, if you use the class
keyword, it will use a classCallCheck
function to ensure that the class is instantiated correctly.
By default, those helpers will be inserted at the top of the file being transformed, which can lead to duplication. This rollup plugin automatically deduplicates those helpers, keeping only one copy of each one used in the output bundle. Rollup will combine the helpers in a single block at the top of your bundle. To achieve the same in Babel 6 you must use the external-helpers
plugin.
Alternatively, if you know what you're doing, you can use the transform-runtime
plugin. If you do this, use runtimeHelpers: true
:
rollup.rollup({
...,
plugins: [
babel({ runtimeHelpers: true })
]
}).then(...)
By default externalHelpers
option is set to false
so babel helpers will be included in your bundle.
If you do not wish the babel helpers to be included in your bundle at all (but instead reference the global babelHelpers
object), you may set the externalHelpers
option to true
:
rollup.rollup({
...,
plugins: [
babel({
plugins: ['external-helpers'],
externalHelpers: true
})
]
}).then(...)
This is not needed for Babel 7 - it knows automatically that Rollup understands ES modules & that it shouldn't use any module transform with it. The section below describes what needs to be done for Babel 6.
The env
preset includes the transform-es2015-modules-commonjs plugin, which converts ES6 modules to CommonJS – preventing Rollup from working. Since Babel 6.3 it's possible to deactivate module transformation with "modules": false
. So there is no need to use the old workaround with babel-preset-es2015-rollup
, that will work for Babel <6.13. Rollup will throw an error if this is incorrectly configured.
However, setting modules: false
in your .babelrc
may conflict if you are using babel-register
. To work around this, specify babelrc: false
in your rollup config. This allows Rollup to bypass your .babelrc
file. In order to use the env
preset, you will also need to specify it with modules: false
option:
plugins: [
babel({
babelrc: false,
presets: [['env', { modules: false }]],
}),
];
The following applies to Babel 6 only. If you're using Babel 5, do npm i -D rollup-plugin-babel@1
, as version 2 and above no longer supports Babel 5
npm install --save-dev rollup-plugin-babel@3 babel-preset-env babel-plugin-external-helpers
// .babelrc
{
"presets": [
[
"env",
{
"modules": false
}
]
],
"plugins": [
"external-helpers"
]
}
rollup-plugin-babel
exposes a plugin-builder utility that allows users to add custom handling of Babel's configuration for each file that it processes.
.custom
accepts a callback that will be called with the loader's instance of babel
so that tooling can ensure that it using exactly the same @babel/core
instance as the loader itself.
It's main purpose is to allow other tools for configuration of transpilation without forcing people to add extra configuration but still allow for using their own babelrc / babel config files.
import babel from 'rollup-plugin-babel';
export default babel.custom(babelCore => {
function myPlugin() {
return {
visitor: {},
};
}
return {
// Passed the plugin options.
options({ opt1, opt2, ...pluginOptions }) {
return {
// Pull out any custom options that the plugin might have.
customOptions: { opt1, opt2 },
// Pass the options back with the two custom options removed.
pluginOptions,
};
},
config(cfg /* Passed Babel's 'PartialConfig' object. */, { code, customOptions }) {
if (cfg.hasFilesystemConfig()) {
// Use the normal config
return cfg.options;
}
return {
...cfg.options,
plugins: [
...(cfg.options.plugins || []),
// Include a custom plugin in the options.
myPlugin,
],
};
},
result(result, { code, customOptions, config, transformOptions }) {
return {
...result,
code: result.code + '\n// Generated by some custom plugin',
};
},
};
});
MIT
FAQs
Seamless integration between Rollup and Babel.
We found that rollup-plugin-babel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers collaborating on the project.
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.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.