4.4.0 • Published 4 years ago

rollup-plugin-babel v4.4.0

Weekly downloads
1,438,350
License
MIT
Repository
github
Last release
4 years ago

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 6.x

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

babel 7.x - beta

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

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

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.

Configuring Babel

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 babel-preset-env babel-plugin-external-helpers
// .babelrc
{
  "presets": [
    [
      "env",
      {
        "modules": false
      }
    ]
  ],
  "plugins": [
    "external-helpers"
  ]
}

Modules

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.

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. It's therefore recommended that you use the external-helpers plugin, which is automatically included in the es2015-rollup preset. Rollup will combine the helpers in a single block at the top of your bundle.

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

License

MIT

aka-cli@monodrom/monodrom@fbeyza/trial_libverdaccio-okta-authnexvel-component-library@admin-bro/coremarigold-build@bunchee/buncheecf-storybookgoulashifybbt-editortsdx-extrabappo-scriptsplamensvelteit@huozhi/burritobalerqinshixixing-rollupdiode-inspr-testdiode-testttdiode-inspr-test-test-testdiode-inspr-testtdiode-testinspr-test-diode-testdiode-test-inspradmin-bro-jznotiontheory-basic-buildfuze-teafinlab-components@naetverkjs/naetverk-cliadmiral_storybookaccess-tools@deity/falcon-scriptscloud-archive-s3lcs-axiosbt-dsbridgemy-react-library-gccvue3-relaxplus@accurate-util/filter-util@workrails/wr-catalog-basereact-mxgraph-editorvideo-clip-ljstivity-scheduler-devextremerikaard1mk-beter-clinpmlib-hayk-davreact-artsy-modalprototype-1tq-mui@shiluodexiaobaitu/zcutilskingsun-ui-expandmb-librairie-reactlightlib-uibussr-admin-brovue-waterfall-jy@lp0124/a@codewitchbella/microbundletakla-buildertakla-builder-rollup@schoonlabs/blocks@jackerwang/xzguidothttp-swagger2hardockersurface-threejs@thebespokepixel/cordialmdrokz-react-librarycross-selectoralex-frontend-sdk-stagingnbsdkfather-build-mbptest@nirvarnia/pkg@ez-fe/oopsform-manager-reactchangzhn-jsonpitauros-material-libjcinvccmock-vue-componentmock-global-fngulp-pipelinestorybook-tutorial-bhjyq-test-dumi@surmon-china/abc-factoryblazedocmor-ui-react-library@coverdash/quotes-stagingcomponents-seguroscomponents-sitio-publicoguficonpickersuz-claim-componentstest-test-sp-rickazure-functions-bundlexania-clipipiap-testpipiapasimplereacttest@thequack/sharednext-auth-updated@hui_wu/testautomatic-interactionsoul-buttontaro-marquees
5.0.0-alpha.2

4 years ago

4.4.0

4 years ago

5.0.0-alpha.1

4 years ago

4.3.3

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

5.0.0-alpha.0

5 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.1.0-1

5 years ago

4.1.0-0

6 years ago

4.0.3

6 years ago

4.0.3-0

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

4.0.0-beta.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

4.0.0-beta.7

6 years ago

4.0.0-beta.6

6 years ago

3.0.5

6 years ago

4.0.0-beta.5

6 years ago

3.0.4

6 years ago

4.0.0-beta.4

6 years ago

4.0.0-beta.3

6 years ago

4.0.0-beta.2

6 years ago

4.0.0-beta.1

6 years ago

3.0.3

6 years ago

4.0.0-beta.0

6 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

3.0.0-alpha.17

7 years ago

3.0.0-alpha.15

7 years ago

3.0.0-alpha.12

7 years ago

2.7.1

7 years ago

2.7.0

7 years ago

2.6.1

8 years ago

2.6.0

8 years ago

2.5.1

8 years ago

2.5.0

8 years ago

2.4.0

8 years ago

2.3.9

8 years ago

2.3.8

8 years ago

2.3.7

8 years ago

2.3.6

8 years ago

2.3.5

8 years ago

2.3.4

8 years ago

2.3.3

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago