Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Commit

Permalink
feat: find any preset with a matching -rollup suffixed version (#3)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This will differ in behavior since it may use presets that you didn’t expect. The previous version only used es2015-rollup in lieu of es2015.

Closes #1
  • Loading branch information
eventualbuddha committed Jul 13, 2016
1 parent cfb8876 commit 8fad7a7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export default {
If you use the `es2015` preset, make sure you install `es2015-rollup` too. See
this project's own [`rollup.config.js`][rollup-config] for an example.

For any presets you use in `.babelrc`, this module will look for one with the
same name but with a `-rollup` suffix.

[rollup-config]: https://github.com/eventualbuddha/babelrc-rollup/blob/master/rollup.config.js
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"resolve": "^1.1.7"
}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let pkg = require('./package.json');
export default {
entry: 'src/index.js',
plugins: [babel(babelrc())],
external: ['fs'],
external: Object.keys(pkg['dependencies']).concat(['fs']),
targets: [
{
dest: pkg['jsnext:main'],
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { readFileSync } from 'fs';
import { sync as resolve } from 'resolve';

function startsWith(string: string, prefix: string): boolean {
return string.lastIndexOf(prefix, 0) === 0;
Expand Down Expand Up @@ -48,9 +49,7 @@ export function configWithoutModules(config: BabelConfig): BabelConfig {
if (Object.prototype.hasOwnProperty.call(config, key)) {
if (key === 'presets' && config.presets) {
// Replace the es2015 preset with the es2015-rollup preset.
result.presets = config.presets.map(
preset => preset === 'es2015' ? 'es2015-rollup' : preset
);
result.presets = config.presets.map(mapPreset);
} else if (key === 'plugins' && config.plugins) {
// Remove any explicit module plugins, e.g. es2015-modules-commonjs.
result.plugins = config.plugins.filter(
Expand All @@ -67,3 +66,13 @@ export function configWithoutModules(config: BabelConfig): BabelConfig {

return result;
}

function mapPreset(preset: string): string {
try {
// this will throw if it can't resolve it
resolve(`babel-preset-${preset}-rollup`);
return `${preset}-rollup`;
} catch (err) {
return preset;
}
}

0 comments on commit 8fad7a7

Please sign in to comment.