Use "babel-preset-react-native"

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 https://github.com/facebook/react-native/pull/5214

Reviewed By: davidaurelio

Differential Revision: D2881814

Pulled By: martinbigio

fb-gh-sync-id: 4168144b7a365fae62bbeed094d8a03a48b4798c
This commit is contained in:
Adam Miskiewicz 2016-02-03 08:14:38 -08:00 committed by facebook-github-bot-3
parent 34389c529d
commit e6cb02d61a
19 changed files with 2071 additions and 4798 deletions

View File

@ -1,6 +1,5 @@
/**
* @providesModule ScrollViewTestModule
* @jsx React.DOM
*/
'use strict';

31
babel-preset/README.md Normal file
View File

@ -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.

View File

@ -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'],
}]
},
]
])
};
}

View File

@ -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;
}

View File

@ -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,
};

View File

@ -8,7 +8,4 @@
*/
'use strict';
exports.getAll = function(options) {
return [];
};
module.exports = require('./configs/main');

45
babel-preset/package.json Normal file
View File

@ -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
babel-preset/plugins.js Normal file
View File

@ -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'),
};

6464
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,6 @@
"/node_modules/(?!react|fbjs|react-native|parse|react-transform-hmr|core-js|promise)/",
"node_modules/react/lib/React.js",
"node_modules/react/lib/ReactDOM.js",
"node_modules/fbjs/lib/Map.js",
"node_modules/fbjs/lib/Promise.js",
"node_modules/fbjs/lib/fetch.js",
@ -49,7 +48,6 @@
"node_modules/fbjs/lib/someObject.js",
"node_modules/fbjs/lib/sprintf.js",
"node_modules/fbjs/lib/xhrSimpleDataSerializer.js",
"downstream/core/CSSCore.js",
"downstream/core/TouchEventUtils.js",
"downstream/core/camelize.js",
@ -69,7 +67,6 @@
"downstream/core/invariant.js",
"downstream/core/nativeRequestAnimationFrame.js",
"downstream/core/toArray.js",
"node_modules/jest-cli",
"node_modules/react/dist"
],
@ -108,35 +105,13 @@
"dependencies": {
"absolute-path": "^0.0.0",
"art": "^0.10.0",
"babel-core": "^6.1.20",
"babel-plugin-external-helpers-2": "^6.1.4",
"babel-plugin-syntax-async-functions": "^6.0.14",
"babel-plugin-syntax-class-properties": "^6.0.14",
"babel-plugin-syntax-flow": "^6.0.14",
"babel-plugin-syntax-jsx": "^6.0.14",
"babel-plugin-syntax-trailing-function-commas": "^6.0.14",
"babel-plugin-transform-class-properties": "^6.0.14",
"babel-plugin-transform-es2015-arrow-functions": "^6.0.14",
"babel-plugin-transform-es2015-block-scoping": "^6.0.18",
"babel-plugin-transform-es2015-classes": "^6.1.2",
"babel-plugin-transform-es2015-computed-properties": "^6.0.14",
"babel-plugin-transform-es2015-constants": "^6.0.15",
"babel-plugin-transform-es2015-destructuring": "^6.0.18",
"babel-plugin-transform-es2015-for-of": "^6.0.14",
"babel-plugin-transform-es2015-modules-commonjs": "^6.1.3",
"babel-plugin-transform-es2015-parameters": "^6.0.18",
"babel-plugin-transform-es2015-shorthand-properties": "^6.0.14",
"babel-plugin-transform-es2015-spread": "^6.0.14",
"babel-plugin-transform-es2015-template-literals": "^6.0.14",
"babel-plugin-transform-flow-strip-types": "^6.0.14",
"babel-plugin-transform-object-assign": "^6.0.14",
"babel-plugin-transform-object-rest-spread": "^6.0.14",
"babel-plugin-transform-react-display-name": "^6.0.14",
"babel-plugin-transform-react-jsx": "^6.0.18",
"babel-plugin-transform-regenerator": "^6.0.18",
"babel-polyfill": "^6.0.16",
"babel-types": "^6.1.2",
"babylon": "^6.1.2",
"babel-core": "~6.4.5",
"babel-plugin-external-helpers": "~6.4.0",
"babel-polyfill": "~6.3.14",
"babel-preset-react-native": "^1.2.4",
"babel-register": "~6.4.3",
"babel-types": "~6.4.5",
"babylon": "~6.4.5",
"base64-js": "^0.0.8",
"bser": "^1.0.2",
"chalk": "^1.1.1",
@ -160,8 +135,7 @@
"promise": "^7.1.1",
"react": "^0.14.5",
"react-timer-mixin": "^0.13.2",
"react-transform-hmr": "^1.0.1",
"babel-plugin-react-transform": "2.0.0-beta1",
"react-transform-hmr": "^1.0.2",
"rebound": "^0.0.13",
"regenerator": "^0.8.36",
"sane": "^1.2.0",

View File

@ -16,7 +16,7 @@ var path = require('path');
var _only = [];
function readBabelRC() {
var rcpath = path.join(__dirname, 'react-packager', '.babelrc');
var rcpath = path.join(__dirname, 'react-packager', 'rn-babelrc.json');
var source = fs.readFileSync(rcpath).toString();
return JSON.parse(source);
}
@ -25,5 +25,5 @@ module.exports = function(onlyList) {
_only = _only.concat(onlyList);
var config = readBabelRC();
config.only = _only;
require('babel-core/register')(config);
require('babel-register')(config);
};

View File

@ -1,30 +0,0 @@
{
"retainLines": true,
"compact": true,
"comments": false,
"plugins": [
"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"
],
"sourceMaps": false
}

View File

@ -15,7 +15,6 @@ useGracefulFs();
var debug = require('debug');
var omit = require('underscore').omit;
var Activity = require('./src/Activity');
var Transforms = require('./src/transforms');
exports.createServer = createServer;
exports.middleware = function(options) {
@ -24,7 +23,6 @@ exports.middleware = function(options) {
};
exports.Activity = Activity;
exports.getTransforms = Transforms.getAll;
// Renamed "package" to "bundle". But maintain backwards
// compat.

View File

@ -0,0 +1,4 @@
{
"presets": [ "react-native" ],
"plugins": []
}

View File

@ -66,6 +66,7 @@ class Transformer {
this._cache = opts.cache;
this._transformModulePath = opts.transformModulePath;
this._projectRoots = opts.projectRoots;
if (opts.transformModulePath != null) {
let transformer;
@ -129,6 +130,7 @@ class Transformer {
filename: filePath,
options: {
...options,
projectRoots: this._projectRoots,
externalTransformModulePath: this._transformModulePath,
},
}).then(res => {

View File

@ -9,30 +9,25 @@
'use strict';
var babel = require('babel-core');
var resolvePlugins = require('./resolvePlugins');
var Transforms = require('../transforms');
var makeInternalConfig = require('babel-preset-react-native/configs/internal');
// Runs internal transforms on the given sourceCode. Note that internal
// transforms should be run after the external ones to ensure that they run on
// Javascript code
function internalTransforms(sourceCode, filename, options) {
var plugins = resolvePlugins(Transforms.getAll(options));
if (plugins.length === 0) {
var internalBabelConfig = makeInternalConfig(options);
if (!internalBabelConfig) {
return {
code: sourceCode,
filename: filename,
};
}
var result = babel.transform(sourceCode, {
retainLines: true,
compact: true,
comments: false,
var result = babel.transform(sourceCode, Object.assign({
filename: filename,
sourceFileName: filename,
sourceMaps: false,
plugins: plugins,
});
}, internalBabelConfig));
return {
code: result.code,

View File

@ -93,7 +93,6 @@ class Resolver {
// should work after this release and we can
// remove it from here.
'parse',
'react-transform-hmr',
],
platforms: ['ios', 'android'],
preferNativePlatform: true,

View File

@ -11,47 +11,95 @@
'use strict';
const babel = require('babel-core');
const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs');
const inlineRequires = require('fbjs-scripts/babel-6/inline-requires');
const makeHMRConfig = require('babel-preset-react-native/configs/hmr');
const resolvePlugins = require('babel-preset-react-native/lib/resolvePlugins');
const inlineRequiresPlugin = require('fbjs-scripts/babel-6/inline-requires');
const json5 = require('json5');
const path = require('path');
const ReactPackager = require('./react-packager');
const resolvePlugins = require('./react-packager/src/JSTransformer/resolvePlugins');
const babelRC =
json5.parse(
fs.readFileSync(
path.resolve(__dirname, 'react-packager', '.babelrc')));
/**
* Return a memoized function that checks for the existence of a
* project level .babelrc file, and if it doesn't exist, reads the
* default RN babelrc file and uses that.
*/
const getBabelRC = (function() {
let babelRC = null;
function transform(src, filename, options) {
options = options || {};
return function _getBabelRC(projectRoots) {
if (babelRC !== null) {
return babelRC;
}
babelRC = { plugins: [] }; // empty babelrc
// Let's look for the .babelrc in the first project root.
// In the future let's look into adding a command line option to specify
// this location.
//
// NOTE: we're not reading the project's .babelrc here. We leave it up to
// Babel to do that automatically and apply the transforms accordingly
// (which works because we pass in `filename` and `sourceFilename` to
// Babel when we transform).
let projectBabelRCPath;
if (projectRoots && projectRoots.length > 0) {
projectBabelRCPath = path.resolve(projectRoots[0], '.babelrc');
}
// If a .babelrc file doesn't exist in the project,
// use the Babel config provided with react-native.
if (!projectBabelRCPath || !fs.existsSync(projectBabelRCPath)) {
babelRC = json5.parse(
fs.readFileSync(
path.resolve(__dirname, 'react-packager', 'rn-babelrc.json'))
);
// Require the babel-preset's listed in the default babel config
babelRC.presets = babelRC.presets.map((preset) => require('babel-preset-' + preset));
babelRC.plugins = resolvePlugins(babelRC.plugins);
}
return babelRC;
}
})();
/**
* Given a filename and options, build a Babel
* config object with the appropriate plugins.
*/
function buildBabelConfig(filename, options) {
const babelRC = getBabelRC(options.projectRoots);
const extraPlugins = ['external-helpers-2'];
const extraConfig = {
filename,
sourceFileName: filename,
};
const config = Object.assign({}, babelRC, extraConfig);
if (options.hot) {
extraPlugins.push([
'react-transform',
{
transforms: [{
transform: 'react-transform-hmr/lib/index.js',
imports: ['React'],
locals: ['module'],
}]
},
]);
}
let config = Object.assign({}, babelRC, extraConfig);
// Add extra plugins
const extraPlugins = [externalHelpersPlugin];
if (options.inlineRequires) {
extraPlugins.push(inlineRequires);
extraPlugins.push(inlineRequiresPlugin);
}
config.plugins = resolvePlugins(extraPlugins.concat(config.plugins));
const result = babel.transform(src, Object.assign({}, babelRC, config));
config.plugins = extraPlugins.concat(config.plugins);
if (options.hot) {
const hmrConfig = makeHMRConfig(options);
config = Object.assign({}, config, hmrConfig);
}
return Object.assign({}, babelRC, config);
}
function transform(src, filename, options) {
options = options || {};
const babelConfig = buildBabelConfig(filename, options);
const result = babel.transform(src, babelConfig);
return {
code: result.code,