Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opt-out for prestet-flow to work with @babel/preset-typescript #3865

Merged
merged 2 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/babel-preset-react-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,27 @@ npm install babel-preset-react-app --save-dev

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

```js
{
"presets": ["react-app"]
}
```
```js
{
"presets": ["react-app"]
}
```

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.

## Usage with TypeScript

To use this package with [`@babel/preset-typescript`](https://www.npmjs.com/package/@babel/preset-typescript), you need to disable `@babel/preset-flow` first.

You can achieve this by doing:

```
{
"presets": [
["react-app", {
"flow": false
}],
"@babel/typescript"
]
}
```
16 changes: 15 additions & 1 deletion packages/babel-preset-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
*/
'use strict';

const validateBoolOption = (name, value, defaultValue) => {
if (typeof value === 'undefined') {
value = defaultValue;
}

if (typeof value !== 'boolean') {
throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
}

return value;
};

module.exports = function(api, opts) {
if (!opts) {
opts = {};
Expand All @@ -21,6 +33,8 @@ module.exports = function(api, opts) {
var isEnvDevelopment = env === 'development';
var isEnvProduction = env === 'production';
var isEnvTest = env === 'test';
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);

if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
throw new Error(
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
Expand Down Expand Up @@ -64,7 +78,7 @@ module.exports = function(api, opts) {
development: isEnvDevelopment || isEnvTest,
},
],
[require('@babel/preset-flow').default],
isFlowEnabled && [require('@babel/preset-flow').default],
].filter(Boolean),
plugins: [
// Experimental macros support. Will be documented after it's had some time
Expand Down