Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Run prettier over the repo files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Sep 17, 2018
1 parent 6bb0e49 commit 01d2095
Show file tree
Hide file tree
Showing 21 changed files with 221 additions and 231 deletions.
81 changes: 41 additions & 40 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,119 @@
Babel 7 compatible! (dropped Babel 6 compatibility though).

Additionally:
* 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
* 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.

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

## 3.0.3

* Drop babel7 support. Use 4.0.0-beta if you use babel 7
* Use "module" in addition to "jsnext:main" ([#150](https://github.com/rollup/rollup-plugin-babel/issues/150))
* Remove unused babel helpers namespace declaration & expression ([#164](https://github.com/rollup/rollup-plugin-babel/issues/164))
- Drop babel7 support. Use 4.0.0-beta if you use babel 7
- Use "module" in addition to "jsnext:main" ([#150](https://github.com/rollup/rollup-plugin-babel/issues/150))
- Remove unused babel helpers namespace declaration & expression ([#164](https://github.com/rollup/rollup-plugin-babel/issues/164))

## 3.0.2

* Fix regression with Babel 6 ([#158](https://github.com/rollup/rollup-plugin-babel/issues/158))
- Fix regression with Babel 6 ([#158](https://github.com/rollup/rollup-plugin-babel/issues/158))

## 3.0.1

* Wasn't working, fix bug with transform (not using es2015-classes for preflight check)
- Wasn't working, fix bug with transform (not using es2015-classes for preflight check)

## 3.0.0

* Drop Node 0.10/0.12 (Use native `Object.assign`)
* Change `babel-core` to be a peerDependency
* Support `babel-core` v7 as well as a peerDep (no changes necessary)
- Drop Node 0.10/0.12 (Use native `Object.assign`)
- Change `babel-core` to be a peerDependency
- Support `babel-core` v7 as well as a peerDep (no changes necessary)

## 2.7.1

* Prevent erroneous warnings about duplicated runtime helpers ([#105](https://github.com/rollup/rollup-plugin-babel/issues/105))
* Ignore `ignore` option in preflight check ([#102](https://github.com/rollup/rollup-plugin-babel/issues/102))
* Allow custom `moduleName` with `runtime-helpers` ([#95](https://github.com/rollup/rollup-plugin-babel/issues/95))
- Prevent erroneous warnings about duplicated runtime helpers ([#105](https://github.com/rollup/rollup-plugin-babel/issues/105))
- Ignore `ignore` option in preflight check ([#102](https://github.com/rollup/rollup-plugin-babel/issues/102))
- Allow custom `moduleName` with `runtime-helpers` ([#95](https://github.com/rollup/rollup-plugin-babel/issues/95))

## 2.7.0

* Add `externalHelpersWhitelist` option ([#92](https://github.com/rollup/rollup-plugin-babel/pull/92))
* Ignore `only` option during preflight checks ([#98](https://github.com/rollup/rollup-plugin-babel/issues/98))
* Use `options.onwarn` if available ([#84](https://github.com/rollup/rollup-plugin-babel/issues/84))
* Update documentation and dependencies
- Add `externalHelpersWhitelist` option ([#92](https://github.com/rollup/rollup-plugin-babel/pull/92))
- Ignore `only` option during preflight checks ([#98](https://github.com/rollup/rollup-plugin-babel/issues/98))
- Use `options.onwarn` if available ([#84](https://github.com/rollup/rollup-plugin-babel/issues/84))
- Update documentation and dependencies

## 2.6.1

* Return a `name`
- Return a `name`

## 2.6.0

* Use `\0` convention for helper module ID ([#64](https://github.com/rollup/rollup-plugin-babel/issues/64))
- Use `\0` convention for helper module ID ([#64](https://github.com/rollup/rollup-plugin-babel/issues/64))

## 2.5.1

* Don't mutate `options.plugins` ([#47](https://github.com/rollup/rollup-plugin-babel/issues/47))
- Don't mutate `options.plugins` ([#47](https://github.com/rollup/rollup-plugin-babel/issues/47))

## 2.5.0

* Import `babelHelpers` rather than injecting them – allows `transform` function to be pure ([#rollup/658](https://github.com/rollup/rollup/pull/658#issuecomment-223876824))
- Import `babelHelpers` rather than injecting them – allows `transform` function to be pure ([#rollup/658](https://github.com/rollup/rollup/pull/658#issuecomment-223876824))

## 2.4.0

* Add `externalHelpers` option ([#41](https://github.com/rollup/rollup-plugin-babel/pull/41))
- Add `externalHelpers` option ([#41](https://github.com/rollup/rollup-plugin-babel/pull/41))

## 2.3.9

* Do not rename Babel helpers ([#34](https://github.com/rollup/rollup-plugin-babel/pull/34))
- Do not rename Babel helpers ([#34](https://github.com/rollup/rollup-plugin-babel/pull/34))

## 2.3.8

* Create new version to (hopefully) solve bizarre CI issue
- Create new version to (hopefully) solve bizarre CI issue

## 2.3.7

* Be less clever about renaming Babel helpers ([#19](https://github.com/rollup/rollup-plugin-babel/issues/19))
- Be less clever about renaming Babel helpers ([#19](https://github.com/rollup/rollup-plugin-babel/issues/19))

## 2.3.6

* Fix cache misses in preflight check ([#29](https://github.com/rollup/rollup-plugin-babel/pull/29))
- Fix cache misses in preflight check ([#29](https://github.com/rollup/rollup-plugin-babel/pull/29))

## 2.3.5

* Use class transformer local to plugin, not project being built
- Use class transformer local to plugin, not project being built

## 2.3.4

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

## 2.3.3

* Fix helper renaming ([#22](https://github.com/rollup/rollup-plugin-babel/issues/22))
- Fix helper renaming ([#22](https://github.com/rollup/rollup-plugin-babel/issues/22))

## 2.3.1-2

* Include correct files in npm package
- Include correct files in npm package

## 2.3.0

* Allow `transform-runtime` Babel plugin, if combined with `runtimeHelpers: true` option ([#17](https://github.com/rollup/rollup-plugin-babel/issues/17))
* More permissive handling of helpers – only warn if inline helpers are duplicated
* Handle plugins that change export patterns ([#18](https://github.com/rollup/rollup-plugin-babel/issues/18))
- Allow `transform-runtime` Babel plugin, if combined with `runtimeHelpers: true` option ([#17](https://github.com/rollup/rollup-plugin-babel/issues/17))
- More permissive handling of helpers – only warn if inline helpers are duplicated
- Handle plugins that change export patterns ([#18](https://github.com/rollup/rollup-plugin-babel/issues/18))

## 2.2.0

* Preflight checks are run per-file, to avoid configuration snafus ([#16](https://github.com/rollup/rollup-plugin-babel/issues/16))
- Preflight checks are run per-file, to avoid configuration snafus ([#16](https://github.com/rollup/rollup-plugin-babel/issues/16))

## 2.1.0

* Generate sourcemaps by default
- Generate sourcemaps by default

## 2.0.1

* Use object-assign ponyfill
* Add travis support
* Fix test
- Use object-assign ponyfill
- Add travis support
- Fix test

## 2.0.0

* Babel 6 compatible
- Babel 6 compatible

## 1.0.0

* First release
- First release
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ 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:

* 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.
- 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
Expand Down Expand Up @@ -48,23 +47,23 @@ rollup({

All options are as per the [Babel documentation](https://babeljs.io/), 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)
- `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.
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.
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.

Expand Down Expand Up @@ -97,18 +96,19 @@ rollup.rollup({

### 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.*
_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](http://babeljs.io/docs/plugins/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:

```js
plugins: [
babel({
babelrc: false,
presets: [['env', { modules: false }]]
})
]
babel({
babelrc: false,
presets: [['env', { modules: false }]],
}),
];
```

## Configuring Babel 6
Expand Down
5 changes: 2 additions & 3 deletions src/helperPlugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HELPERS } from './constants.js';
import { addNamed } from '@babel/helper-module-imports';

export default function importHelperPlugin () {
export default function importHelperPlugin() {
return {
pre (file) {
pre(file) {
const cachedHelpers = {};
file.set('helperGenerator', name => {
if (cachedHelpers[name]) {
Expand All @@ -14,4 +14,3 @@ export default function importHelperPlugin () {
},
};
}

50 changes: 25 additions & 25 deletions src/preflightCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,69 @@ const MODULE_ERROR =
'Unfortunately it looks like your configuration specifies a module transformer ' +
'to replace ES6 modules with another module format. To continue you have to disable it.' +
'\n\n' +
'Most commonly it\'s a CommonJS transform added by @babel/preset-env - ' +
"Most commonly it's a CommonJS transform added by @babel/preset-env - " +
'in such case you should disable it by adding `modules: false` option to that preset ' +
'(described in more detail here - https://github.com/rollup/rollup-plugin-babel#modules ).';

const UNEXPECTED_ERROR =
'An unexpected situation arose. Please raise an issue at ' +
'https://github.com/rollup/rollup-plugin-babel/issues. Thanks!';

function fallbackClassTransform () {
function fallbackClassTransform() {
return {
visitor: {
ClassDeclaration (path, state) {
ClassDeclaration(path, state) {
path.replaceWith(state.file.addHelper('inherits'));
}
}
},
},
};
}

export default function createPreflightCheck () {
export default function createPreflightCheck() {
let preflightCheckResults = {};

return ( ctx, options, file ) => {
if ( preflightCheckResults[ file ] === undefined ) {
return (ctx, options, file) => {
if (preflightCheckResults[file] === undefined) {
let helpers;

options = Object.assign( {}, options );
options = Object.assign({}, options);
delete options.only;
delete options.ignore;

options.filename = join( dirname( file ), 'x.js' );
options.filename = join(dirname(file), 'x.js');

const inputCode = 'class Foo extends Bar {};\nexport default Foo;';
const transformed = transform( inputCode, options );
const transformed = transform(inputCode, options);

if (!transformed) {
return (preflightCheckResults[ file ] = null);
return (preflightCheckResults[file] = null);
}

let check = transformed.code;

if ( ~check.indexOf('class ') ) {
options.plugins = (options.plugins || []).concat( fallbackClassTransform );
check = transform( inputCode, options ).code;
if (~check.indexOf('class ')) {
options.plugins = (options.plugins || []).concat(fallbackClassTransform);
check = transform(inputCode, options).code;
}

if (
!~check.indexOf( 'export default' ) &&
!~check.indexOf( 'export default Foo' ) &&
!~check.indexOf( 'export { Foo as default }' )
!~check.indexOf('export default') &&
!~check.indexOf('export default Foo') &&
!~check.indexOf('export { Foo as default }')
) {
ctx.error( MODULE_ERROR );
ctx.error(MODULE_ERROR);
}

if ( check.match( /\/helpers\/(esm\/)?inherits/ ) ) helpers = RUNTIME;
else if ( ~check.indexOf( 'function _inherits' ) ) helpers = INLINE;
else if ( ~check.indexOf( 'babelHelpers' ) ) helpers = EXTERNAL;
if (check.match(/\/helpers\/(esm\/)?inherits/)) helpers = RUNTIME;
else if (~check.indexOf('function _inherits')) helpers = INLINE;
else if (~check.indexOf('babelHelpers')) helpers = EXTERNAL;
else {
ctx.error( UNEXPECTED_ERROR );
ctx.error(UNEXPECTED_ERROR);
}

preflightCheckResults[ file ] = helpers;
preflightCheckResults[file] = helpers;
}

return preflightCheckResults[ file ];
return preflightCheckResults[file];
};
}
8 changes: 4 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let warned = {};
export function warnOnce ( ctx, msg ) {
if ( warned[ msg ] ) return;
warned[ msg ] = true;
ctx.warn( msg );
export function warnOnce(ctx, msg) {
if (warned[msg]) return;
warned[msg] = true;
ctx.warn(msg);
}

const regExpCharactersRegExp = /[\\^$.*+?()[\]{}|]/g;
Expand Down
2 changes: 1 addition & 1 deletion test/samples/basic/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const answer = 42;
console.log( `the answer is ${answer}` );
console.log(`the answer is ${answer}`);
2 changes: 1 addition & 1 deletion test/samples/checks/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import Foo from './foo/index.js';

class Bar {}

console.log( { Foo, Bar } );
console.log({ Foo, Bar });
4 changes: 2 additions & 2 deletions test/samples/class-loose/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Foo extends Bar {
baz () {
console.log( 'xyz' );
baz() {
console.log('xyz');
}
}
4 changes: 2 additions & 2 deletions test/samples/class/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Foo {
bar () {
console.log( 'baz' );
bar() {
console.log('baz');
}
}
2 changes: 1 addition & 1 deletion test/samples/exclusions/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { foo } from './foo';

console.log( `the answer is ${foo()}` );
console.log(`the answer is ${foo()}`);
2 changes: 1 addition & 1 deletion test/samples/named-function-helper/_config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
solo: true,
description: 'correctly renames helpers'
description: 'correctly renames helpers',
};

0 comments on commit 01d2095

Please sign in to comment.