Skip to content

Commit d67a9e7

Browse files
oieduardorabelogaearon
authored andcommitted
feat: add opt-out for prestet-flow to work with @babel/preset-typescript (#3865)
* feat: add opt-out for prestet-flow to work with @babel/preset-typescript * docs: adding example in readme
1 parent 0e51eef commit d67a9e7

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/babel-preset-react-app/README.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@ npm install babel-preset-react-app --save-dev
2424

2525
Then create a file named `.babelrc` with following contents in the root folder of your project:
2626

27-
```js
28-
{
29-
"presets": ["react-app"]
30-
}
31-
```
27+
```js
28+
{
29+
"presets": ["react-app"]
30+
}
31+
```
3232

3333
This preset uses the `useBuiltIns` option with [transform-object-rest-spread](http://babeljs.io/docs/plugins/transform-object-rest-spread/) and [transform-react-jsx](http://babeljs.io/docs/plugins/transform-react-jsx/), which assumes that `Object.assign` is available or polyfilled.
34+
35+
## Usage with TypeScript
36+
37+
To use this package with [`@babel/preset-typescript`](https://www.npmjs.com/package/@babel/preset-typescript), you need to disable `@babel/preset-flow` first.
38+
39+
You can achieve this by doing:
40+
41+
```
42+
{
43+
"presets": [
44+
["react-app", {
45+
"flow": false
46+
}],
47+
"@babel/typescript"
48+
]
49+
}
50+
```

packages/babel-preset-react-app/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
*/
77
'use strict';
88

9+
const validateBoolOption = (name, value, defaultValue) => {
10+
if (typeof value === 'undefined') {
11+
value = defaultValue;
12+
}
13+
14+
if (typeof value !== 'boolean') {
15+
throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
16+
}
17+
18+
return value;
19+
};
20+
921
module.exports = function(api, opts) {
1022
if (!opts) {
1123
opts = {};
@@ -21,6 +33,8 @@ module.exports = function(api, opts) {
2133
var isEnvDevelopment = env === 'development';
2234
var isEnvProduction = env === 'production';
2335
var isEnvTest = env === 'test';
36+
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
37+
2438
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
2539
throw new Error(
2640
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
@@ -64,7 +78,7 @@ module.exports = function(api, opts) {
6478
development: isEnvDevelopment || isEnvTest,
6579
},
6680
],
67-
[require('@babel/preset-flow').default],
81+
isFlowEnabled && [require('@babel/preset-flow').default],
6882
].filter(Boolean),
6983
plugins: [
7084
// Experimental macros support. Will be documented after it's had some time

0 commit comments

Comments
 (0)