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

Commit 5cfa3be

Browse files
feat: update to work with babel v6.13.x by passing options (#4)
BREAKING CHANGE: we no longer look for `-rollup` suffixed presets unless you specifically ask to. You can restore the old behavior as described in the README. Works around rollup/babel-preset-es2015-rollup#7.
1 parent 8fad7a7 commit 5cfa3be

File tree

7 files changed

+292
-52
lines changed

7 files changed

+292
-52
lines changed

.babelrc

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"presets": ["es2015"],
3-
"plugins": ["syntax-flow", "transform-flow-strip-types"]
4-
}
2+
"presets": [
3+
["es2015", { "modules": false }]
4+
],
5+
"plugins": ["syntax-flow", "transform-flow-strip-types", "syntax-object-rest-spread", "transform-object-rest-spread"]
6+
}

README.md

+69-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,75 @@ export default {
3333
};
3434
```
3535

36-
If you use the `es2015` preset, make sure you install `es2015-rollup` too. See
37-
this project's own [`rollup.config.js`][rollup-config] for an example.
36+
### Options
3837

39-
For any presets you use in `.babelrc`, this module will look for one with the
40-
same name but with a `-rollup` suffix.
38+
#### `path` (default: `'.babelrc'`)
4139

40+
If you'd like to customize the path of your babelrc file, pass this option with
41+
a string path that can be read using `fs.readFile`.
42+
43+
#### `config`
44+
45+
Use this to avoid reading a babelrc file at all. You could use this to pull the
46+
config from `package.json` instead, for example.
47+
48+
#### `addModuleOptions` (default: `true`)
49+
50+
Disable this option if you do not want the `modules: false` option to be given
51+
to presets in your babel config. You probably don't want to change this unless
52+
you're using `findRollupPresets`.
53+
54+
#### `findRollupPresets` (default: `false`)
55+
56+
Enable this to replace presets with the equivalent rollup-compatible preset, if
57+
available. When this option is enabled, babelrc-rollup will try to resolve e.g.
58+
`es2015-rollup` instead of `es2015`. If no such preset can be found the original
59+
will be used.
60+
61+
#### `addExternalHelpersPlugin` (default: `true`)
62+
63+
By default, babelrc-rollup adds the [`external-helpers` plugin][external-helpers],
64+
which ensures that only one copy of each helper ends up in your bundle. Disable
65+
this option to prevent adding this plugin.
66+
67+
68+
## A note on babel versions
69+
70+
Since babel [v6.13.0][6-13-0], presets may be given options by using a tuple of
71+
`[name, opts]`. For example, instead of
72+
73+
```js
74+
{
75+
"presets": ["es2015"]
76+
}
77+
```
78+
79+
You can do this:
80+
81+
```js
82+
{
83+
"presets": [
84+
["es2015", { "modules": false }]
85+
]
86+
}
87+
```
88+
89+
babelrc-rollup is meant to work with this version of babel or later, but earlier
90+
versions are supported by using the right options. The old way to configure the
91+
`es2015` preset for use with rollup was to use the `es2015-rollup` preset
92+
instead. To continue doing that, call `babelrc` like so:
93+
94+
```js
95+
babelrc({
96+
addModuleOptions: false,
97+
findRollupPresets: true,
98+
addExternalHelpersPlugin: false
99+
})
100+
```
101+
102+
If you use the `es2015` preset, make sure you install `es2015-rollup` too. If
103+
you can use babel v6.13.0 or later, you should do so.
104+
105+
[external-helpers]: https://babeljs.io/docs/plugins/external-helpers/
106+
[6-13-0]: https://github.com/babel/babel/blob/master/CHANGELOG.md#v6130-2016-08-04
42107
[rollup-config]: https://github.com/eventualbuddha/babelrc-rollup/blob/master/rollup.config.js

package.json

+13-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"flow": "flow check",
88
"build": "rollup -c",
9-
"test": "npm run flow && npm run bootstrap",
9+
"pretest": "npm run flow && npm run bootstrap",
10+
"test": "mocha",
1011
"bootstrap": "./script/bootstrap",
1112
"prepublish": "npm test",
1213
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
@@ -29,14 +30,18 @@
2930
},
3031
"homepage": "https://github.com/eventualbuddha/babelrc-rollup#readme",
3132
"devDependencies": {
32-
"babel-cli": "^6.10.1",
33-
"babel-plugin-syntax-flow": "^6.8.0",
34-
"babel-plugin-transform-es2015-parameters": "^6.9.0",
33+
"babel-cli": "^6.11.4",
34+
"babel-plugin-external-helpers": "^6.8.0",
35+
"babel-plugin-syntax-flow": "^6.13.0",
36+
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
37+
"babel-plugin-transform-es2015-parameters": "^6.11.4",
3538
"babel-plugin-transform-flow-strip-types": "^6.8.0",
36-
"babel-preset-es2015": "^6.9.0",
37-
"babel-preset-es2015-rollup": "^1.1.1",
38-
"flow-bin": "^0.28.0",
39-
"rollup": "^0.33.0",
39+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
40+
"babel-preset-es2015": "^6.13.2",
41+
"flow-bin": "^0.30.0",
42+
"mocha": "^3.0.2",
43+
"reify": "^0.3.6",
44+
"rollup": "^0.34.7",
4045
"rollup-plugin-babel": "^2.6.1",
4146
"semantic-release": "^4.3.5"
4247
},

script/bootstrap

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ mkdir -p dist
99
# use babel directly to create the ES module version
1010
babel \
1111
src/index.js \
12-
--no-babelrc \
13-
--plugins syntax-flow,transform-flow-strip-types,transform-es2015-parameters \
1412
-o dist/babelrc-rollup.mjs
1513

1614
# test our rollup build with the babel-built version

src/index.js

+103-35
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,78 @@ function startsWith(string: string, prefix: string): boolean {
77
return string.lastIndexOf(prefix, 0) === 0;
88
}
99

10+
type Preset = any;
11+
1012
type BabelConfig = {
11-
filename?: string,
12-
filenameRelative?: string,
13-
presets?: Array<string>,
14-
plugins?: Array<string>,
15-
highlightCode?: boolean,
16-
only?: ?(RegExp | string | Array<RegExp | string>),
17-
ignore?: ?(RegExp | string | Array<RegExp | string>),
18-
auxiliaryCommentBefore?: ?string,
19-
auxiliaryCommentAfter?: ?string,
20-
sourceMaps?: boolean,
21-
inputSourceMap?: ?Object,
22-
sourceMapTarget?: string,
23-
sourceFileName?: string,
24-
sourceRoot?: string,
25-
moduleRoot?: string,
26-
moduleIds?: boolean,
27-
moduleId?: string,
28-
getModuleId?: (moduleName: string) => ?string,
29-
resolveModuleSource?: (source: string, filename: string) => ?string,
30-
code?: boolean,
31-
babelrc?: boolean,
32-
ast?: boolean,
33-
compact?: boolean | 'auto',
34-
comments?: boolean,
35-
shouldPrintComment?: (commentContents: string) => boolean,
36-
env?: {[key: string]: BabelConfig},
37-
retainLines?: boolean,
38-
extends?: ?string,
13+
filename?: string;
14+
filenameRelative?: string;
15+
presets?: Array<Preset>;
16+
plugins?: Array<string>;
17+
highlightCode?: boolean;
18+
only?: ?(RegExp | string | Array<RegExp | string>);
19+
ignore?: ?(RegExp | string | Array<RegExp | string>);
20+
auxiliaryCommentBefore?: ?string;
21+
auxiliaryCommentAfter?: ?string;
22+
sourceMaps?: boolean;
23+
inputSourceMap?: ?Object;
24+
sourceMapTarget?: string;
25+
sourceFileName?: string;
26+
sourceRoot?: string;
27+
moduleRoot?: string;
28+
moduleIds?: boolean;
29+
moduleId?: string;
30+
getModuleId?: (moduleName: string) => ?string;
31+
resolveModuleSource?: (source: string, filename: string) => ?string;
32+
code?: boolean;
33+
babelrc?: boolean;
34+
ast?: boolean;
35+
compact?: boolean | 'auto';
36+
comments?: boolean;
37+
shouldPrintComment?: (commentContents: string) => boolean;
38+
env?: {[key: string]: BabelConfig};
39+
retainLines?: boolean;
40+
extends?: ?string;
41+
};
42+
43+
type Options = {
44+
path?: string;
45+
config?: BabelConfig;
46+
findRollupPresets?: boolean;
47+
addModuleOptions?: boolean;
48+
addExternalHelpersPlugin?: boolean;
49+
resolve?: (path: string) => string;
3950
};
4051

41-
export default function babelrc(path: string='.babelrc'): BabelConfig {
42-
return configWithoutModules(JSON.parse(readFileSync(path, { encoding: 'utf8' })));
52+
const DEFAULT_OPTIONS = {
53+
path: '.babelrc',
54+
findRollupPresets: false,
55+
addModuleOptions: true,
56+
addExternalHelpersPlugin: true,
57+
resolve
58+
};
59+
60+
export default function babelrc(options: Options|string={}): BabelConfig {
61+
if (typeof options === 'string') {
62+
options = { path: options };
63+
}
64+
65+
let resolvedOptions = { ...DEFAULT_OPTIONS, ...options };
66+
67+
if (!resolvedOptions.config && typeof resolvedOptions.path === 'string') {
68+
resolvedOptions.config = JSON.parse(readFileSync(resolvedOptions.path, { encoding: 'utf8' }));
69+
}
70+
71+
return configWithoutModules(resolvedOptions.config, resolvedOptions);
4372
}
4473

45-
export function configWithoutModules(config: BabelConfig): BabelConfig {
74+
export function configWithoutModules(config: BabelConfig, options: Options): BabelConfig {
4675
let result = {};
4776

4877
for (let key in config) {
4978
if (Object.prototype.hasOwnProperty.call(config, key)) {
5079
if (key === 'presets' && config.presets) {
5180
// Replace the es2015 preset with the es2015-rollup preset.
52-
result.presets = config.presets.map(mapPreset);
81+
result.presets = config.presets.map(preset => mapPreset(preset, options));
5382
} else if (key === 'plugins' && config.plugins) {
5483
// Remove any explicit module plugins, e.g. es2015-modules-commonjs.
5584
result.plugins = config.plugins.filter(
@@ -61,18 +90,57 @@ export function configWithoutModules(config: BabelConfig): BabelConfig {
6190
}
6291
}
6392

93+
if (options.addExternalHelpersPlugin) {
94+
if (!result.plugins) {
95+
result.plugins = ['external-helpers'];
96+
} else {
97+
result.plugins = [...result.plugins, 'external-helpers'];
98+
}
99+
}
100+
64101
// Make sure babel does not look for the babelrc file.
65102
result.babelrc = false;
66103

67104
return result;
68105
}
69106

70-
function mapPreset(preset: string): string {
107+
function mapPreset(preset: any, options: Options): any {
108+
let info = getPresetInfo(preset);
109+
110+
if (!info) {
111+
return preset;
112+
}
113+
114+
if (options.findRollupPresets && hasRollupVersionOfPreset(info.name, options.resolve || resolve)) {
115+
return [`${info.name}-rollup`, info.options];
116+
} else if (options.addModuleOptions) {
117+
return [info.name, { ...info.options, modules: false }];
118+
} else {
119+
return preset;
120+
}
121+
}
122+
123+
function getPresetInfo(preset: any): ?{ name: string, options: Object } {
124+
if (typeof preset === 'string') {
125+
return { name: preset, options: {} };
126+
} else if (Array.isArray(preset)) {
127+
let name = preset[0];
128+
let options = preset[1] || {};
129+
130+
if (typeof name === 'string' && typeof options === 'object') {
131+
return { name, options };
132+
}
133+
}
134+
135+
return null;
136+
}
137+
138+
function hasRollupVersionOfPreset(preset: string, resolve: (path: string) => string): boolean {
71139
try {
72140
// this will throw if it can't resolve it
73141
resolve(`babel-preset-${preset}-rollup`);
74-
return `${preset}-rollup`;
142+
return true;
75143
} catch (err) {
76-
return preset;
144+
return false;
77145
}
78146
}

test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require reify

0 commit comments

Comments
 (0)