@rollup/plugin-babel 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +174 -0
- package/LICENSE +21 -0
- package/README.md +352 -0
- package/dist/index.es.js +437 -0
- package/dist/index.js +444 -0
- package/package.json +77 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# @rollup/plugin-babel changelog
|
|
2
|
+
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
_2020-04-27_
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- Added `getBabelOutputPlugin` and `createBabelOutputPluginFactory` exports which can be used to transform generated code
|
|
10
|
+
- Added `skipPreflightCheck` option. The plugin performs some extra checks to see if the passed configuration is correct and matching its expectations. This comes with some runtime overhead and can slow down builds. If you know what you are doing and you are confident that you have configured things correctly you can disable those checks with this option.
|
|
11
|
+
- Published as `@rollup/plugin-babel`
|
|
12
|
+
|
|
13
|
+
### Updates
|
|
14
|
+
|
|
15
|
+
- Default export exported as `getBabelInputPlugin` for symmetry with `getBabelOutputPlugin`
|
|
16
|
+
|
|
17
|
+
### Breaking Changes
|
|
18
|
+
|
|
19
|
+
- Minimum compatible Rollup version is 1.2.0
|
|
20
|
+
- Minimum supported Node version is 10.0.0
|
|
21
|
+
- `.custom` factory is now available as separate `createBabelInputPluginFactory` export
|
|
22
|
+
- Removed `externalHelpers` & `externalHelpers` options. There is now a single `babelHelpers` option which can take one of `'bundled'`, `'inline'`, `'runtime'` and `'external'` as a value. The default is `'bundled'` which matches 4.x behavior, but it is recommended to configure this option explicitly.
|
|
23
|
+
|
|
24
|
+
## 4.3.2
|
|
25
|
+
|
|
26
|
+
- Fixed usage with `externalHelpers: true` option
|
|
27
|
+
|
|
28
|
+
## 4.3.1
|
|
29
|
+
|
|
30
|
+
- Add `.js` extension to the virtual babel helpers file (only matters when using `preserveModules` option in rollup)
|
|
31
|
+
|
|
32
|
+
## 4.3.0
|
|
33
|
+
|
|
34
|
+
- Added `.custom` builder.
|
|
35
|
+
- Fail build when a plugin tries to add non existent babel helper
|
|
36
|
+
|
|
37
|
+
## 4.2.0
|
|
38
|
+
|
|
39
|
+
Allow `rollup@1` as peer dependency.
|
|
40
|
+
|
|
41
|
+
## 4.1.0
|
|
42
|
+
|
|
43
|
+
- Fixed "preflight check" for ignored files.
|
|
44
|
+
- Return `null` when no transformation has been done (fixing source maps for this case)
|
|
45
|
+
|
|
46
|
+
## 4.0.3
|
|
47
|
+
|
|
48
|
+
Fixed fallback class transform in "preflight check".
|
|
49
|
+
|
|
50
|
+
## 4.0.2
|
|
51
|
+
|
|
52
|
+
Fixed `rollup` peer dependency.
|
|
53
|
+
|
|
54
|
+
## 4.0.0
|
|
55
|
+
|
|
56
|
+
Babel 7 compatible! (dropped Babel 6 compatibility though).
|
|
57
|
+
|
|
58
|
+
Additionally:
|
|
59
|
+
|
|
60
|
+
- Internal preflight checks are created now per plugin instance, so using 2 instances of rollup-plugin-babel (i.e. targeting 2 different set of files with include/exclude options) shouldn't conflict with each other
|
|
61
|
+
- Transpiling by default only what Babel transpiles - files with those extensions: .js, .jsx, .es6, .es, .mjs. You can customize this with new `extensions` option. This also fixes long standing issue with rollup-plugin-babel trying to transform JSON files.
|
|
62
|
+
|
|
63
|
+
## 3.0.3
|
|
64
|
+
|
|
65
|
+
- Drop babel7 support. Use 4.0.0-beta if you use babel 7
|
|
66
|
+
- Use "module" in addition to "jsnext:main" ([#150](https://github.com/rollup/rollup-plugin-babel/issues/150))
|
|
67
|
+
- Remove unused babel helpers namespace declaration & expression ([#164](https://github.com/rollup/rollup-plugin-babel/issues/164))
|
|
68
|
+
|
|
69
|
+
## 3.0.2
|
|
70
|
+
|
|
71
|
+
- Fix regression with Babel 6 ([#158](https://github.com/rollup/rollup-plugin-babel/issues/158))
|
|
72
|
+
|
|
73
|
+
## 3.0.1
|
|
74
|
+
|
|
75
|
+
- Wasn't working, fix bug with transform (not using es2015-classes for preflight check)
|
|
76
|
+
|
|
77
|
+
## 3.0.0
|
|
78
|
+
|
|
79
|
+
- Drop Node 0.10/0.12 (Use native `Object.assign`)
|
|
80
|
+
- Change `babel-core` to be a peerDependency
|
|
81
|
+
- Support `babel-core` v7 as well as a peerDep (no changes necessary)
|
|
82
|
+
|
|
83
|
+
## 2.7.1
|
|
84
|
+
|
|
85
|
+
- Prevent erroneous warnings about duplicated runtime helpers ([#105](https://github.com/rollup/rollup-plugin-babel/issues/105))
|
|
86
|
+
- Ignore `ignore` option in preflight check ([#102](https://github.com/rollup/rollup-plugin-babel/issues/102))
|
|
87
|
+
- Allow custom `moduleName` with `runtime-helpers` ([#95](https://github.com/rollup/rollup-plugin-babel/issues/95))
|
|
88
|
+
|
|
89
|
+
## 2.7.0
|
|
90
|
+
|
|
91
|
+
- Add `externalHelpersWhitelist` option ([#92](https://github.com/rollup/rollup-plugin-babel/pull/92))
|
|
92
|
+
- Ignore `only` option during preflight checks ([#98](https://github.com/rollup/rollup-plugin-babel/issues/98))
|
|
93
|
+
- Use `options.onwarn` if available ([#84](https://github.com/rollup/rollup-plugin-babel/issues/84))
|
|
94
|
+
- Update documentation and dependencies
|
|
95
|
+
|
|
96
|
+
## 2.6.1
|
|
97
|
+
|
|
98
|
+
- Return a `name`
|
|
99
|
+
|
|
100
|
+
## 2.6.0
|
|
101
|
+
|
|
102
|
+
- Use `\0` convention for helper module ID ([#64](https://github.com/rollup/rollup-plugin-babel/issues/64))
|
|
103
|
+
|
|
104
|
+
## 2.5.1
|
|
105
|
+
|
|
106
|
+
- Don't mutate `options.plugins` ([#47](https://github.com/rollup/rollup-plugin-babel/issues/47))
|
|
107
|
+
|
|
108
|
+
## 2.5.0
|
|
109
|
+
|
|
110
|
+
- Import `babelHelpers` rather than injecting them – allows `transform` function to be pure ([#rollup/658](https://github.com/rollup/rollup/pull/658#issuecomment-223876824))
|
|
111
|
+
|
|
112
|
+
## 2.4.0
|
|
113
|
+
|
|
114
|
+
- Add `externalHelpers` option ([#41](https://github.com/rollup/rollup-plugin-babel/pull/41))
|
|
115
|
+
|
|
116
|
+
## 2.3.9
|
|
117
|
+
|
|
118
|
+
- Do not rename Babel helpers ([#34](https://github.com/rollup/rollup-plugin-babel/pull/34))
|
|
119
|
+
|
|
120
|
+
## 2.3.8
|
|
121
|
+
|
|
122
|
+
- Create new version to (hopefully) solve bizarre CI issue
|
|
123
|
+
|
|
124
|
+
## 2.3.7
|
|
125
|
+
|
|
126
|
+
- Be less clever about renaming Babel helpers ([#19](https://github.com/rollup/rollup-plugin-babel/issues/19))
|
|
127
|
+
|
|
128
|
+
## 2.3.6
|
|
129
|
+
|
|
130
|
+
- Fix cache misses in preflight check ([#29](https://github.com/rollup/rollup-plugin-babel/pull/29))
|
|
131
|
+
|
|
132
|
+
## 2.3.5
|
|
133
|
+
|
|
134
|
+
- Use class transformer local to plugin, not project being built
|
|
135
|
+
|
|
136
|
+
## 2.3.4
|
|
137
|
+
|
|
138
|
+
- Ensure class transformer is present for preflight check, and only run check once per directory ([#23](https://github.com/rollup/rollup-plugin-babel/issues/23))
|
|
139
|
+
|
|
140
|
+
## 2.3.3
|
|
141
|
+
|
|
142
|
+
- Fix helper renaming ([#22](https://github.com/rollup/rollup-plugin-babel/issues/22))
|
|
143
|
+
|
|
144
|
+
## 2.3.1-2
|
|
145
|
+
|
|
146
|
+
- Include correct files in npm package
|
|
147
|
+
|
|
148
|
+
## 2.3.0
|
|
149
|
+
|
|
150
|
+
- Allow `transform-runtime` Babel plugin, if combined with `runtimeHelpers: true` option ([#17](https://github.com/rollup/rollup-plugin-babel/issues/17))
|
|
151
|
+
- More permissive handling of helpers – only warn if inline helpers are duplicated
|
|
152
|
+
- Handle plugins that change export patterns ([#18](https://github.com/rollup/rollup-plugin-babel/issues/18))
|
|
153
|
+
|
|
154
|
+
## 2.2.0
|
|
155
|
+
|
|
156
|
+
- Preflight checks are run per-file, to avoid configuration snafus ([#16](https://github.com/rollup/rollup-plugin-babel/issues/16))
|
|
157
|
+
|
|
158
|
+
## 2.1.0
|
|
159
|
+
|
|
160
|
+
- Generate sourcemaps by default
|
|
161
|
+
|
|
162
|
+
## 2.0.1
|
|
163
|
+
|
|
164
|
+
- Use object-assign ponyfill
|
|
165
|
+
- Add travis support
|
|
166
|
+
- Fix test
|
|
167
|
+
|
|
168
|
+
## 2.0.0
|
|
169
|
+
|
|
170
|
+
- Babel 6 compatible
|
|
171
|
+
|
|
172
|
+
## 1.0.0
|
|
173
|
+
|
|
174
|
+
- First release
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
[npm]: https://img.shields.io/npm/v/@rollup/plugin-babel
|
|
2
|
+
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-babel
|
|
3
|
+
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-babel
|
|
4
|
+
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-babel
|
|
5
|
+
|
|
6
|
+
[![npm][npm]][npm-url]
|
|
7
|
+
[![size][size]][size-url]
|
|
8
|
+
[](https://liberamanifesto.com)
|
|
9
|
+
|
|
10
|
+
# @rollup/plugin-babel
|
|
11
|
+
|
|
12
|
+
🍣 A Rollup plugin for seamless integration between Rollup and Babel.
|
|
13
|
+
|
|
14
|
+
## Why?
|
|
15
|
+
|
|
16
|
+
If you're using Babel to transpile your ES6/7 code and Rollup to generate a standalone bundle, you have a couple of options:
|
|
17
|
+
|
|
18
|
+
- run the code through Babel first, being careful to exclude the module transformer, or
|
|
19
|
+
- run the code through Rollup first, and _then_ pass it to Babel.
|
|
20
|
+
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
Either way, you have to worry about a place to put the intermediate files, and getting sourcemaps to behave becomes a royal pain.
|
|
24
|
+
|
|
25
|
+
Using Rollup with `@rollup/plugin-babel` makes the process far easier.
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @rollup/plugin-babel --save-dev
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import babel from '@rollup/plugin-babel';
|
|
43
|
+
import pkg from './package.json';
|
|
44
|
+
|
|
45
|
+
const config = {
|
|
46
|
+
input: 'src/index.js',
|
|
47
|
+
output: {
|
|
48
|
+
dir: 'output',
|
|
49
|
+
format: 'esm'
|
|
50
|
+
},
|
|
51
|
+
plugins: [babel({ babelHelpers: 'bundled' })]
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export default config;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
|
|
58
|
+
|
|
59
|
+
## Options
|
|
60
|
+
|
|
61
|
+
This plugin respects Babel [configuration files](https://babeljs.io/docs/en/configuration) by default and they are generally the best place to put your configuration.
|
|
62
|
+
|
|
63
|
+
You can also run Babel on the generated chunks instead of the input files. Even though this is slower, it is the only way to transpile Rollup's auto-generated wrapper code to lower compatibility targets than ES5 (or most likely ES6 when Rollup 2 will be released), see [Running Babel on the generated code](#running-babel-on-the-generated-code) for details.
|
|
64
|
+
|
|
65
|
+
All options are as per the [Babel documentation](https://babeljs.io/docs/en/options), plus the following:
|
|
66
|
+
|
|
67
|
+
### `exclude`
|
|
68
|
+
|
|
69
|
+
Type: `String | RegExp | Array[...String|RegExp]`<br>
|
|
70
|
+
|
|
71
|
+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. When relaying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself.
|
|
72
|
+
|
|
73
|
+
### `include`
|
|
74
|
+
|
|
75
|
+
Type: `String | RegExp | Array[...String|RegExp]`<br>
|
|
76
|
+
|
|
77
|
+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there.
|
|
78
|
+
|
|
79
|
+
### `extensions`
|
|
80
|
+
|
|
81
|
+
Type: `Array[...String]`<br>
|
|
82
|
+
Default: `['.js', '.jsx', '.es6', '.es', '.mjs']`
|
|
83
|
+
|
|
84
|
+
An array of file extensions that Babel should transpile. If you want to tranpile TypeScript files with this plugin it's essential to include `.ts` and `.tsx` in this option.
|
|
85
|
+
|
|
86
|
+
### `babelHelpers`
|
|
87
|
+
|
|
88
|
+
Type: `'bundled' | 'runtime' | 'inline' | 'external'`<br>
|
|
89
|
+
Default: `'bundled'`
|
|
90
|
+
|
|
91
|
+
It is recommended to configure this option explicitly (even if with its default value) so an informed decision is taken on how those babel helpers are inserted into the code.
|
|
92
|
+
|
|
93
|
+
We recommend to follow those guidelines for each possible value:
|
|
94
|
+
|
|
95
|
+
- `'runtime'` - you should use it especially when building libraries with rollup. It has to be used in combination with `@babel/plugin-transform-runtime` and you should also specify `@babel/runtime` as dependency of your package (don't forget to tell rollup to treat it as your external dependency when bundling for `cjs` & `es` formats).
|
|
96
|
+
- `'bundled'` - you should use it if you want your resulting bundle to contain those helpers (at most one copy of each). Useful especially if you bundle an application code.
|
|
97
|
+
- `'external'` - use it only if you know what you are doing. It will reference helpers on **global** `babelHelpers` object. Used in combination with `@babel/plugin-external-helpers`.
|
|
98
|
+
- `'inline'` - this is not recommended. Helpers will be inserted in each file using them. This can cause serious code duplication (this is the default Babel behaviour).
|
|
99
|
+
|
|
100
|
+
### `skipPreflightCheck`
|
|
101
|
+
|
|
102
|
+
Type: `Boolean`<br>
|
|
103
|
+
Default: `false`
|
|
104
|
+
|
|
105
|
+
Before transpiling your input files this plugin also transpile a short piece of code **for each** input file. This is used to validate some misconfiguration errors, but for sufficiently big projects it can slow your build times so if you are confident about your configuration then you might disable those checks with this option.
|
|
106
|
+
|
|
107
|
+
### External dependencies
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
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.
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
### Helpers
|
|
116
|
+
|
|
117
|
+
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.
|
|
118
|
+
|
|
119
|
+
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.
|
|
120
|
+
|
|
121
|
+
You can customize how those helpers are being inserted into the transformed file with [`babelHelpers`](#babelhelpers) option.
|
|
122
|
+
|
|
123
|
+
### Modules
|
|
124
|
+
|
|
125
|
+
This is not needed since Babel 7 - it knows automatically that Rollup understands ES modules & that it shouldn't use any module transform with it. Unless you forcefully include a module transform in your Babel configuration.
|
|
126
|
+
|
|
127
|
+
If you have been pointed to this section by an error thrown by this plugin, please check your Babel configuration files and disable any module transforms when running Rollup builds.
|
|
128
|
+
|
|
129
|
+
## Running Babel on the generated code
|
|
130
|
+
|
|
131
|
+
You can run `@rollup/plugin-babel` on the output files instead of the input files by using `getBabelOutputPlugin(...)`. This can be used to perform code transformations on the resulting chunks and is the only way to transform Rollup's auto-generated code. By default, the plugin will be applied to all outputs:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
// rollup.config.js
|
|
135
|
+
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
|
|
136
|
+
|
|
137
|
+
export default {
|
|
138
|
+
input: 'main.js',
|
|
139
|
+
plugins: [
|
|
140
|
+
getBabelOutputPlugin({
|
|
141
|
+
presets: ['@babel/preset-env']
|
|
142
|
+
})
|
|
143
|
+
],
|
|
144
|
+
output: [
|
|
145
|
+
{ file: 'bundle.cjs.js', format: 'cjs' },
|
|
146
|
+
{ file: 'bundle.esm.js', format: 'esm' }
|
|
147
|
+
]
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
If you only want to apply it to specific outputs, you can use it as an output plugin (requires at least Rollup v1.27.0):
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
// rollup.config.js
|
|
155
|
+
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
|
|
156
|
+
|
|
157
|
+
export default {
|
|
158
|
+
input: 'main.js',
|
|
159
|
+
output: [
|
|
160
|
+
{ file: 'bundle.js', format: 'esm' },
|
|
161
|
+
{
|
|
162
|
+
file: 'bundle.es5.js',
|
|
163
|
+
format: 'esm',
|
|
164
|
+
plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'] })]
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The `include`, `exclude` and `extensions` options are ignored when the when using `getBabelOutputPlugin` and `createBabelOutputPluginFactory` will produce warnings, and there are a few more points to note that users should be aware of.
|
|
171
|
+
|
|
172
|
+
You can also run the plugin twice on the code, once when processing the input files to transpile special syntax to JavaScript and once on the output to transpile to a lower compatibility target:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
// rollup.config.js
|
|
176
|
+
import babel, { getBabelOutputPlugin } from '@rollup/plugin-babel';
|
|
177
|
+
|
|
178
|
+
export default {
|
|
179
|
+
input: 'main.js',
|
|
180
|
+
plugins: [babel({ presets: ['@babel/preset-react'] })],
|
|
181
|
+
output: [
|
|
182
|
+
{
|
|
183
|
+
file: 'bundle.js',
|
|
184
|
+
format: 'esm',
|
|
185
|
+
plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'] })]
|
|
186
|
+
}
|
|
187
|
+
]
|
|
188
|
+
};
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Babel configuration files
|
|
192
|
+
|
|
193
|
+
Unlike the regular `babel` plugin, `getBabelOutputPlugin(...)` will **not** automatically search for [Babel configuration files](https://babeljs.io/docs/en/config-files). Besides passing in Babel options directly, however, you can specify a configuration file manually via Babel's [`configFile`](https://babeljs.io/docs/en/options#configfile) option:
|
|
194
|
+
|
|
195
|
+
```js
|
|
196
|
+
getBabelOutputPlugin({
|
|
197
|
+
configFile: path.resolve(__dirname, 'babel.config.js')
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Using formats other than ES modules or CommonJS
|
|
202
|
+
|
|
203
|
+
As `getBabelOutputPlugin(...)` will run _after_ Rollup has done all its transformations, it needs to make sure it preserves the semantics of Rollup's output format. This is especially important for Babel plugins that add, modify or remove imports or exports, but also for other transformations that add new variables as they can accidentally become global variables depending on the format. Therefore it is recommended that for formats other than `esm` or `cjs`, you set Rollup to use the `esm` output format and let Babel handle the transformation to another format, e.g. via
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
presets: [['@babel/preset-env', { modules: 'umd' }], ...]
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
to create a UMD/IIFE compatible output. If you want to use `getBabelOutputPlugin(...)` with other formats, you need to specify `allowAllFormats: true` as plugin option:
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
rollup.rollup({...})
|
|
213
|
+
.then(bundle => bundle.generate({
|
|
214
|
+
format: 'iife',
|
|
215
|
+
plugins: [getBabelOutputPlugin({
|
|
216
|
+
allowAllFormats: true,
|
|
217
|
+
// ...
|
|
218
|
+
})]
|
|
219
|
+
}))
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Injected helpers
|
|
223
|
+
|
|
224
|
+
By default, helpers e.g. when transpiling classes will be inserted at the top of each chunk. In contrast to when applying this plugin on the input files, helpers will not be deduplicated across chunks.
|
|
225
|
+
|
|
226
|
+
Alternatively, you can use imported runtime helpers by adding the `@babel/transform-runtime` plugin. This will make `@babel/runtime` an external dependency of your project, see [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/babel-plugin-transform-runtime) for details.
|
|
227
|
+
|
|
228
|
+
Note that this will only work for `esm` and `cjs` formats, and you need to make sure to set the `useESModules` option of `@babel/plugin-transform-runtime` to `true` if you create ESM output:
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
rollup.rollup({...})
|
|
232
|
+
.then(bundle => bundle.generate({
|
|
233
|
+
format: 'esm',
|
|
234
|
+
plugins: [getBabelOutputPlugin({
|
|
235
|
+
presets: ['@babel/preset-env'],
|
|
236
|
+
plugins: [['@babel/plugin-transform-runtime', { useESModules: true }]]
|
|
237
|
+
})]
|
|
238
|
+
}))
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
// input
|
|
243
|
+
export default class Foo {}
|
|
244
|
+
|
|
245
|
+
// output
|
|
246
|
+
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
|
|
247
|
+
|
|
248
|
+
var Foo = function Foo() {
|
|
249
|
+
_classCallCheck(this, Foo);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export default Foo;
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
And for CommonJS:
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
rollup.rollup({...})
|
|
259
|
+
.then(bundle => bundle.generate({
|
|
260
|
+
format: 'cjs',
|
|
261
|
+
plugins: [getBabelOutputPlugin({
|
|
262
|
+
presets: ['@babel/preset-env'],
|
|
263
|
+
plugins: [['@babel/plugin-transform-runtime', { useESModules: false }]]
|
|
264
|
+
})]
|
|
265
|
+
}))
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
// input
|
|
270
|
+
export default class Foo {}
|
|
271
|
+
|
|
272
|
+
// output
|
|
273
|
+
('use strict');
|
|
274
|
+
|
|
275
|
+
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
276
|
+
|
|
277
|
+
var Foo = function Foo() {
|
|
278
|
+
_classCallCheck(this, Foo);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
module.exports = Foo;
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Another option is to use `@babel/plugin-external-helpers`, which will reference the global `babelHelpers` object. It is your responsibility to make sure this global variable exists.
|
|
285
|
+
|
|
286
|
+
## Custom plugin builder
|
|
287
|
+
|
|
288
|
+
`@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.
|
|
289
|
+
|
|
290
|
+
`createBabelInputPluginFactory` 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.
|
|
291
|
+
|
|
292
|
+
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.
|
|
293
|
+
|
|
294
|
+
### Example
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
import { createBabelInputPluginFactory } from '@rollup/plugin-babel';
|
|
298
|
+
|
|
299
|
+
export default createBabelInputPluginFactory(babelCore => {
|
|
300
|
+
function myPlugin() {
|
|
301
|
+
return {
|
|
302
|
+
visitor: {}
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
// Passed the plugin options.
|
|
308
|
+
options({ opt1, opt2, ...pluginOptions }) {
|
|
309
|
+
return {
|
|
310
|
+
// Pull out any custom options that the plugin might have.
|
|
311
|
+
customOptions: { opt1, opt2 },
|
|
312
|
+
|
|
313
|
+
// Pass the options back with the two custom options removed.
|
|
314
|
+
pluginOptions
|
|
315
|
+
};
|
|
316
|
+
},
|
|
317
|
+
|
|
318
|
+
config(
|
|
319
|
+
cfg /* Passed Babel's 'PartialConfig' object. */,
|
|
320
|
+
{ code, customOptions }
|
|
321
|
+
) {
|
|
322
|
+
if (cfg.hasFilesystemConfig()) {
|
|
323
|
+
// Use the normal config
|
|
324
|
+
return cfg.options;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
...cfg.options,
|
|
329
|
+
plugins: [
|
|
330
|
+
...(cfg.options.plugins || []),
|
|
331
|
+
|
|
332
|
+
// Include a custom plugin in the options.
|
|
333
|
+
myPlugin
|
|
334
|
+
]
|
|
335
|
+
};
|
|
336
|
+
},
|
|
337
|
+
|
|
338
|
+
result(result, { code, customOptions, config, transformOptions }) {
|
|
339
|
+
return {
|
|
340
|
+
...result,
|
|
341
|
+
code: result.code + '\n// Generated by some custom plugin'
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## Meta
|
|
349
|
+
|
|
350
|
+
[CONTRIBUTING](/.github/CONTRIBUTING.md)
|
|
351
|
+
|
|
352
|
+
[LICENSE (MIT)](/LICENSE)
|