• This repository has been archived on 05/Aug/2021
  • Stars
    star
    702
  • Rank 64,499 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This package has moved and is now available at @rollup/plugin-babel / https://github.com/rollup/plugins/tree/master/packages/babel

Moved

This package has moved and is now available at @rollup/plugin-babel. Please update your dependencies. This repository is no longer maintained.

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

babel 7.x

npm install --save-dev rollup-plugin-babel@latest

babel 6.x

npm install --save-dev rollup-plugin-babel@3

Usage

Command Line (rollup)

Configuration

rollup.config.js (docs):

import babel from 'rollup-plugin-babel';
import pkg from './package.json';

const config = {
	input: 'src/index.js',
	output: [
		{
			file: pkg.module,
			format: 'esm',
		},
	],
	plugins: [babel()],
};

export default config;

Programmatic

import { rollup } from 'rollup';
import babel from 'rollup-plugin-babel';

rollup({
  input: '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 helpers
  • 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)
  • 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.

External dependencies

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.

Helpers

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(...)

Modules

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 }]],
	}),
];

Configuring Babel 6

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"
  ]
}

Custom plugin builder

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.

Example

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',
			};
		},
	};
});

License

MIT

More Repositories

1

rollup

Next-generation ES module bundler
JavaScript
25,314
star
2

plugins

🍣 The one-stop shop for official Rollup plugins
JavaScript
3,626
star
3

awesome

⚡️ Delightful Rollup Plugins, Packages, and Resources
2,549
star
4

rollup-starter-lib

Bare-bones example of how to create a library using Rollup
JavaScript
966
star
5

rollup-plugin-commonjs

This module has moved and is now available at @rollup/plugin-commonjs / https://github.com/rollup/plugins/blob/master/packages/commonjs
JavaScript
501
star
6

rollup-plugin-node-resolve

This module has moved and is now available at @rollup/plugin-node-resolve / https://github.com/rollup/plugins/blob/master/packages/node-resolve
JavaScript
469
star
7

rollup-starter-app

Bare-bones example of how to create an application using Rollup
JavaScript
424
star
8

rollup-starter-project

Sample project for packages built using rollup.
JavaScript
325
star
9

rollup-plugin-typescript

This module has moved and is now available at @rollup/plugin-typescript / https://github.com/rollup/plugins/blob/master/packages/typescript
JavaScript
324
star
10

rollup-starter-code-splitting

Starter project with code-splitting and dynamic imports, for modern and legacy browsers
JavaScript
248
star
11

rollup-plugin-alias

This module has moved and is now available at @rollup/plugin-alias / https://github.com/rollup/plugins/tree/master/packages/alias
JavaScript
172
star
12

rollup-plugin-multi-entry

This module has moved and is now available at @rollup/plugin-multi-entry / https://github.com/rollup/plugins/blob/master/packages/multi-entry
JavaScript
171
star
13

rollup-plugin-replace

This module has moved and is now available at @rollup/plugin-replace / https://github.com/rollup/plugins/blob/master/packages/replace
JavaScript
162
star
14

rollup-plugin-json

This module has moved and is now available at @rollup/plugin-json / https://github.com/rollup/plugins/blob/master/packages/json
JavaScript
126
star
15

rollup-watch

Fast incremental rebuilds with Rollup CLI
JavaScript
91
star
16

three-jsnext

three.js, but futuristic
JavaScript
85
star
17

rollupjs.org

Rollup demo website
Svelte
77
star
18

rollup-plugin-inject

This module has moved and is now available at @rollup/plugin-inject / https://github.com/rollup/plugins/blob/master/packages/inject
JavaScript
77
star
19

rollup-plugin-wasm

This module has moved and is now available at @rollup/plugin-wasm / https://github.com/rollup/plugins/blob/master/packages/wasm
WebAssembly
75
star
20

rollup-plugin-url

This module has moved and is now available at @rollup/plugin-url / https://github.com/rollup/plugins/blob/master/packages/url
JavaScript
75
star
21

rollup-docs-cn

Rollup.js 中文文档 - Built with Vitepress
JavaScript
74
star
22

rollup-plugin-run

This module has moved and is now available at @rollup/plugin-run / https://github.com/rollup/plugins/blob/master/packages/run
JavaScript
64
star
23

rollup-plugin-strip

This module has moved and is now available at @rollup/plugin-strip / https://github.com/rollup/plugins/blob/master/packages/strip
JavaScript
50
star
24

babel-preset-es2015-rollup

babel-preset-es2015, minus modules, plus helpers
JavaScript
48
star
25

rollup-pluginutils

This package has moved and is now available at @rollup/pluginutils / https://github.com/rollup/plugins
TypeScript
45
star
26

rollup-plugin-buble

This module has moved and is now available at @rollup/plugin-buble / https://github.com/rollup/plugins/blob/master/packages/buble
JavaScript
43
star
27

rollup-plugin-image

This module has moved and is now available at @rollup/plugin-image / https://github.com/rollup/plugins/blob/master/packages/image
JavaScript
41
star
28

plugin-auto-install

This module has moved and is now available at @rollup/plugin-auto-install / https://github.com/rollup/plugins/blob/master/packages/auto-install
JavaScript
41
star
29

rollup-plugin-virtual

This module has moved and is now available at @rollup/plugin-virtual / https://github.com/rollup/plugins/blob/master/packages/virtual
JavaScript
41
star
30

d3-jsnext

d3, but futuristic
JavaScript
33
star
31

rollup-babel

[DEPRECATED] Experimental rollup/babel integration
JavaScript
28
star
32

rollup-plugin-sucrase

This package has moved and is now available at @rollup/plugin-sucrase / https://github.com/rollup/plugins/blob/master/packages/sucrase
JavaScript
23
star
33

stream

🍣 Stream Rollup build results
TypeScript
22
star
34

rollup-plugin-butternut

This module is no longer maintained. Please use https://www.npmjs.com/package/rollup-plugin-terser
JavaScript
20
star
35

rollup-plugin-ractive

Precompile Ractive components
JavaScript
11
star
36

rollup-plugin-yaml

This module has moved and is now available at @rollup/plugin-yaml / https://github.com/rollup/plugins/blob/master/packages/yaml
JavaScript
11
star
37

rollup-plugin-legacy

Add export statements to plain scripts. Moved to https://github.com/rollup/plugins/blob/master/packages/legacy
JavaScript
10
star
38

eslint-config-rollup

A shareable ESLint configuration for Rollup projects
JavaScript
9
star
39

rollup-init

Initialise Rollup configuration
JavaScript
7
star
40

rollup-plugin-dsv

This module has moved and is now available at @rollup/plugin-dsv / https://github.com/rollup/plugins/blob/master/packages/dsv
JavaScript
5
star
41

rollup-starter-plugin

Starter code for creating a Rollup plugin
JavaScript
5
star
42

containers

📤
Dockerfile
4
star
43

log

🌳
TypeScript
3
star
44

hull

🛳
JavaScript
2
star
45

org

Organizational issues and tasks for Rollup
1
star
46

rollup-docs-az

Rollup docs in Azerbaijani
JavaScript
1
star