Skip to content

Commit

Permalink
Use "babel-preset-react-native"
Browse files Browse the repository at this point in the history
Summary:
Rather than specifying Babel plugins in the `.babelrc` packaged with react-native, leverage a Babel preset to define the plugins (https://github.com/exponentjs/babel-preset-react-native).

This allows for a much better user experience for those who want (or need) to override options in their project's `.babelrc`.

Prior to this PR, if a user wanted to use a custom babel-plugin (or a custom set of babel plugins), they'd have either 1) manually override the `.babelrc` in the react-packager directory (or fork RN), or 2) specify a custom transformer to use when running the packager that loaded their own `.babelrc`. Note - the custom transformer was necessary because without it, RN's `.babelrc` options would supersede the options defined in the project's `.babelrc`...potentially causing issues with plugin ordering.

This PR makes the transformer check for the existence of a project-level `.babelrc`, and if it it's there, it _doesn't_ use the react-native `.babelrc`. This prevents any oddities with Babel plug
Closes #5214

Reviewed By: davidaurelio

Differential Revision: D2881814

Pulled By: martinbigio

fb-gh-sync-id: 4168144b7a365fae62bbeed094d8a03a48b4798c
  • Loading branch information
skevy authored and facebook-github-bot-3 committed Feb 3, 2016
1 parent 34389c5 commit e6cb02d
Show file tree
Hide file tree
Showing 19 changed files with 2,123 additions and 4,850 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* @providesModule ScrollViewTestModule
* @jsx React.DOM
*/

'use strict';
Expand Down
31 changes: 31 additions & 0 deletions babel-preset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# babel-preset-react-native

Babel presets for React Native applications. React Native itself uses this Babel preset by default when transforming your app's source code.

If you wish to use a custom Babel configuration by writing a `.babelrc` file in your project's root directory, you must specify all the plugins necessary to transform your code. React Native does not apply its default Babel configuration in this case. So, to make your life easier, you can use this preset to get the default configuration and then specify more plugins that run before it.

## Usage

As mentioned above, you only need to use this preset if you are writing a custom `.babelrc` file.

### Installation

Install `babel-preset-react-native` in your app:
```sh
npm i babel-preset-react-native --save-dev
```

### Configuring Babel

Then, create a file called `.babelrc` in your project's root directory. The existence of this `.babelrc` file will tell React Native to use your custom Babel configuration instead of its own. Then load this preset:
```
{
"presets": ["react-native"]
}
```

You can further customize your Babel configuration by specifying plugins and other options. See [Babel's `.babelrc` documentation](https://babeljs.io/docs/usage/babelrc/) to learn more.

## Help and Support

If you get stuck configuring Babel, please ask a question on Stack Overflow or find a consultant for help. If you discover a bug, please open up an issue.
28 changes: 28 additions & 0 deletions babel-preset/configs/hmr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var resolvePlugins = require('../lib/resolvePlugins');

module.exports = function(options) {
return {
plugins: resolvePlugins([
[
'react-transform',
{
transforms: [{
transform: 'react-transform-hmr/lib/index.js',
imports: ['React'],
locals: ['module'],
}]
},
]
])
};
}
16 changes: 16 additions & 0 deletions babel-preset/configs/internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var resolvePlugins = require('../lib/resolvePlugins');

module.exports = function(options) {
// For future internal pipeline usage
return null;
}
42 changes: 42 additions & 0 deletions babel-preset/configs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var resolvePlugins = require('../lib/resolvePlugins');

module.exports = {
comments: false,
compact: true,
plugins: resolvePlugins([
'syntax-async-functions',
'syntax-class-properties',
'syntax-trailing-function-commas',
'transform-class-properties',
'transform-es2015-arrow-functions',
'transform-es2015-block-scoping',
'transform-es2015-classes',
'transform-es2015-computed-properties',
'transform-es2015-constants',
'transform-es2015-destructuring',
['transform-es2015-modules-commonjs', { strict: false, allowTopLevelThis: true }],
'transform-es2015-parameters',
'transform-es2015-shorthand-properties',
'transform-es2015-spread',
'transform-es2015-template-literals',
'transform-flow-strip-types',
'transform-object-assign',
'transform-object-rest-spread',
'transform-react-display-name',
'transform-react-jsx',
'transform-regenerator',
['transform-es2015-for-of', { loose: true }],
]),
retainLines: true,
sourceMaps: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
*/
'use strict';

exports.getAll = function(options) {
return [];
};

module.exports = require('./configs/main');
File renamed without changes.
45 changes: 45 additions & 0 deletions babel-preset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "babel-preset-react-native",
"version": "1.2.3",
"description": "Babel preset for React Native applications",
"main": "index.js",
"repository": "https://github.com/facebook/react-native/tree/master/babel-preset",
"keywords": [
"babel",
"preset",
"react-native"
],
"license" : "BSD-3-Clause",
"bugs": {
"url": "https://github.com/facebook/react-native/issues"
},
"homepage": "https://github.com/facebook/react-native/tree/master/babel-preset/README.md",
"dependencies": {
"babel-plugin-react-transform": "2.0.0-beta1",
"babel-plugin-syntax-async-functions": "^6.3.13",
"babel-plugin-syntax-class-properties": "^6.3.13",
"babel-plugin-syntax-flow": "^6.3.13",
"babel-plugin-syntax-jsx": "^6.3.13",
"babel-plugin-syntax-trailing-function-commas": "^6.3.13",
"babel-plugin-transform-class-properties": "^6.4.0",
"babel-plugin-transform-es2015-arrow-functions": "^6.4.0",
"babel-plugin-transform-es2015-block-scoping": "^6.4.0",
"babel-plugin-transform-es2015-classes": "^6.4.0",
"babel-plugin-transform-es2015-computed-properties": "^6.4.0",
"babel-plugin-transform-es2015-constants": "^6.1.4",
"babel-plugin-transform-es2015-destructuring": "^6.4.0",
"babel-plugin-transform-es2015-for-of": "^6.3.13",
"babel-plugin-transform-es2015-modules-commonjs": "^6.4.0",
"babel-plugin-transform-es2015-parameters": "^6.4.2",
"babel-plugin-transform-es2015-shorthand-properties": "^6.3.13",
"babel-plugin-transform-es2015-spread": "^6.4.0",
"babel-plugin-transform-es2015-template-literals": "^6.3.13",
"babel-plugin-transform-flow-strip-types": "^6.4.0",
"babel-plugin-transform-object-assign": "^6.3.13",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-plugin-transform-react-display-name": "^6.4.0",
"babel-plugin-transform-react-jsx": "^6.4.0",
"babel-plugin-transform-regenerator": "^6.3.26",
"react-transform-hmr": "^1.0.2"
}
}
35 changes: 35 additions & 0 deletions babel-preset/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

module.exports = {
'babel-plugin-react-transform': require('babel-plugin-react-transform'),
'babel-plugin-syntax-async-functions': require('babel-plugin-syntax-async-functions'),
'babel-plugin-syntax-class-properties': require('babel-plugin-syntax-class-properties'),
'babel-plugin-syntax-trailing-function-commas': require('babel-plugin-syntax-trailing-function-commas'),
'babel-plugin-transform-class-properties': require('babel-plugin-transform-class-properties'),
'babel-plugin-transform-es2015-arrow-functions': require('babel-plugin-transform-es2015-arrow-functions'),
'babel-plugin-transform-es2015-block-scoping': require('babel-plugin-transform-es2015-block-scoping'),
'babel-plugin-transform-es2015-classes': require('babel-plugin-transform-es2015-classes'),
'babel-plugin-transform-es2015-computed-properties': require('babel-plugin-transform-es2015-computed-properties'),
'babel-plugin-transform-es2015-constants': require('babel-plugin-transform-es2015-constants'),
'babel-plugin-transform-es2015-destructuring': require('babel-plugin-transform-es2015-destructuring'),
'babel-plugin-transform-es2015-modules-commonjs': require('babel-plugin-transform-es2015-modules-commonjs'),
'babel-plugin-transform-es2015-parameters': require('babel-plugin-transform-es2015-parameters'),
'babel-plugin-transform-es2015-shorthand-properties': require('babel-plugin-transform-es2015-shorthand-properties'),
'babel-plugin-transform-es2015-spread': require('babel-plugin-transform-es2015-spread'),
'babel-plugin-transform-es2015-template-literals': require('babel-plugin-transform-es2015-template-literals'),
'babel-plugin-transform-flow-strip-types': require('babel-plugin-transform-flow-strip-types'),
'babel-plugin-transform-object-assign': require('babel-plugin-transform-object-assign'),
'babel-plugin-transform-object-rest-spread': require('babel-plugin-transform-object-rest-spread'),
'babel-plugin-transform-react-display-name': require('babel-plugin-transform-react-display-name'),
'babel-plugin-transform-react-jsx': require('babel-plugin-transform-react-jsx'),
'babel-plugin-transform-regenerator': require('babel-plugin-transform-regenerator'),
'babel-plugin-transform-es2015-for-of': require('babel-plugin-transform-es2015-for-of'),
};
Loading

0 comments on commit e6cb02d

Please sign in to comment.