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

Commit 8fad7a7

Browse files
feat: find any preset with a matching -rollup suffixed version (#3)
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
1 parent cfb8876 commit 8fad7a7

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ export default {
3636
If you use the `es2015` preset, make sure you install `es2015-rollup` too. See
3737
this project's own [`rollup.config.js`][rollup-config] for an example.
3838

39+
For any presets you use in `.babelrc`, this module will look for one with the
40+
same name but with a `-rollup` suffix.
41+
3942
[rollup-config]: https://github.com/eventualbuddha/babelrc-rollup/blob/master/rollup.config.js

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
},
4343
"publishConfig": {
4444
"registry": "https://registry.npmjs.org/"
45+
},
46+
"dependencies": {
47+
"resolve": "^1.1.7"
4548
}
4649
}

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let pkg = require('./package.json');
66
export default {
77
entry: 'src/index.js',
88
plugins: [babel(babelrc())],
9-
external: ['fs'],
9+
external: Object.keys(pkg['dependencies']).concat(['fs']),
1010
targets: [
1111
{
1212
dest: pkg['jsnext:main'],

src/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

33
import { readFileSync } from 'fs';
4+
import { sync as resolve } from 'resolve';
45

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

6867
return result;
6968
}
69+
70+
function mapPreset(preset: string): string {
71+
try {
72+
// this will throw if it can't resolve it
73+
resolve(`babel-preset-${preset}-rollup`);
74+
return `${preset}-rollup`;
75+
} catch (err) {
76+
return preset;
77+
}
78+
}

0 commit comments

Comments
 (0)