Skip to content

Commit 2aedde7

Browse files
committed
Use "babel-preset-react-native"
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 plugin ordering, and leaves that up to the RN user. The RN user would then just include a `.babelrc` file in their project that looks like: ``` { "presets": ["react-native"] } ``` They can then add whatever other presets or plugins that they'd like (such as the Babel Relay Plugin or a decorators plugin, for example).
1 parent 73be933 commit 2aedde7

File tree

5 files changed

+32
-83
lines changed

5 files changed

+32
-83
lines changed

package.json

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,12 @@
107107
"dependencies": {
108108
"absolute-path": "^0.0.0",
109109
"art": "^0.10.0",
110-
"babel-core": "^6.1.20",
111-
"babel-plugin-external-helpers-2": "^6.1.4",
112-
"babel-plugin-syntax-async-functions": "^6.0.14",
113-
"babel-plugin-syntax-class-properties": "^6.0.14",
114-
"babel-plugin-syntax-flow": "^6.0.14",
115-
"babel-plugin-syntax-jsx": "^6.0.14",
116-
"babel-plugin-syntax-trailing-function-commas": "^6.0.14",
117-
"babel-plugin-transform-class-properties": "^6.0.14",
118-
"babel-plugin-transform-es2015-arrow-functions": "^6.0.14",
119-
"babel-plugin-transform-es2015-block-scoping": "^6.0.18",
120-
"babel-plugin-transform-es2015-classes": "^6.1.2",
121-
"babel-plugin-transform-es2015-computed-properties": "^6.0.14",
122-
"babel-plugin-transform-es2015-constants": "^6.0.15",
123-
"babel-plugin-transform-es2015-destructuring": "^6.0.18",
124-
"babel-plugin-transform-es2015-for-of": "^6.0.14",
125-
"babel-plugin-transform-es2015-modules-commonjs": "^6.1.3",
126-
"babel-plugin-transform-es2015-parameters": "^6.0.18",
127-
"babel-plugin-transform-es2015-shorthand-properties": "^6.0.14",
128-
"babel-plugin-transform-es2015-spread": "^6.0.14",
129-
"babel-plugin-transform-es2015-template-literals": "^6.0.14",
130-
"babel-plugin-transform-flow-strip-types": "^6.0.14",
131-
"babel-plugin-transform-object-assign": "^6.0.14",
132-
"babel-plugin-transform-object-rest-spread": "^6.0.14",
133-
"babel-plugin-transform-react-display-name": "^6.0.14",
134-
"babel-plugin-transform-react-jsx": "^6.0.18",
135-
"babel-plugin-transform-regenerator": "^6.0.18",
136-
"babel-polyfill": "^6.0.16",
137-
"babel-types": "^6.1.2",
138-
"babylon": "^6.1.2",
110+
"babel-core": "~6.4.0",
111+
"babel-plugin-external-helpers": "~6.4.0",
112+
"babel-preset-react-native": "^1.0.0",
113+
"babel-polyfill": "~6.3.14",
114+
"babel-types": "~6.4.1",
115+
"babylon": "~6.4.2",
139116
"base64-js": "^0.0.8",
140117
"bser": "^1.0.2",
141118
"chalk": "^1.1.1",

packager/babelRegisterOnly.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var path = require('path');
1616
var _only = [];
1717

1818
function readBabelRC() {
19-
var rcpath = path.join(__dirname, 'react-packager', '.babelrc');
19+
var rcpath = path.join(__dirname, 'react-packager', '.babelrc.json');
2020
var source = fs.readFileSync(rcpath).toString();
2121
return JSON.parse(source);
2222
}

packager/react-packager/.babelrc

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": [ "react-native" ],
3+
"plugins": []
4+
}

packager/transformer.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,42 @@ const json5 = require('json5');
1717
const path = require('path');
1818
const ReactPackager = require('./react-packager');
1919

20-
const babelRC =
21-
json5.parse(
22-
fs.readFileSync(
23-
path.resolve(__dirname, 'react-packager', '.babelrc')));
20+
const projectBabelRCPath = path.resolve(__dirname, '..', '..', '..', '.babelrc');
2421

25-
function transform(src, filename, options) {
26-
options = options || {};
22+
let babelRC = { plugins: [] };
23+
24+
// If a babelrc exists in the project,
25+
// don't use the one provided with react-native.
26+
if (!fs.existsSync(projectBabelRCPath)) {
27+
babelRC = json5.parse(
28+
fs.readFileSync(
29+
path.resolve(__dirname, 'react-packager', '.babelrc.json')));
30+
}
2731

28-
const extraPlugins = ['external-helpers-2'];
32+
function buildBabelConfig(filename, options) {
2933
const extraConfig = {
3034
filename,
3135
sourceFileName: filename,
3236
};
3337

3438
const config = Object.assign({}, babelRC, extraConfig);
3539

40+
// Add extra plugins
41+
const extraPlugins = [require('babel-plugin-external-helpers')];
42+
3643
if (options.inlineRequires) {
3744
extraPlugins.push(inlineRequires);
3845
}
3946
config.plugins = extraPlugins.concat(config.plugins);
4047

41-
// Manually resolve all default Babel plugins. babel.transform will attempt to resolve
42-
// all base plugins relative to the file it's compiling. This makes sure that we're
43-
// using the plugins installed in the react-native package.
44-
config.plugins = config.plugins.map(function(plugin) {
45-
// Normalise plugin to an array.
46-
if (!Array.isArray(plugin)) {
47-
plugin = [plugin];
48-
}
49-
// Only resolve the plugin if it's a string reference.
50-
if (typeof plugin[0] === 'string') {
51-
plugin[0] = require(`babel-plugin-${plugin[0]}`);
52-
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
53-
}
54-
return plugin;
55-
});
48+
return Object.assign({}, babelRC, extraConfig);
49+
}
50+
51+
function transform(src, filename, options) {
52+
options = options || {};
5653

57-
const result = babel.transform(src, Object.assign({}, babelRC, config));
54+
const babelConfig = buildBabelConfig(filename, options);
55+
const result = babel.transform(src, babelConfig);
5856

5957
return {
6058
code: result.code,

0 commit comments

Comments
 (0)