From 179d8e8ec9c78cf285860b16fb494d41e3dface8 Mon Sep 17 00:00:00 2001 From: Eduardo Rabelo Date: Fri, 19 Jan 2018 14:57:58 +1300 Subject: [PATCH 1/2] feat: add opt-out for prestet-flow to work with @babel/preset-typescript --- packages/babel-preset-react-app/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/babel-preset-react-app/index.js b/packages/babel-preset-react-app/index.js index 28b3df84593..612e2fd5fff 100644 --- a/packages/babel-preset-react-app/index.js +++ b/packages/babel-preset-react-app/index.js @@ -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 = {}; @@ -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 ' + @@ -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 From e50e9c12085c39cd01cd40313b3db059dff83492 Mon Sep 17 00:00:00 2001 From: Eduardo Rabelo Date: Fri, 19 Jan 2018 15:04:01 +1300 Subject: [PATCH 2/2] docs: adding example in readme --- packages/babel-preset-react-app/README.md | 27 ++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/babel-preset-react-app/README.md b/packages/babel-preset-react-app/README.md index 5653831ac7e..1d0047c1241 100644 --- a/packages/babel-preset-react-app/README.md +++ b/packages/babel-preset-react-app/README.md @@ -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" + ] +} +```