Skip to content

Commit df6c48c

Browse files
davidaureliofacebook-github-bot
authored andcommitted
babel-preset-react-native: only require plugins once
Reviewed By: cpojer Differential Revision: D6795591 fbshipit-source-id: 30b689e2cf72a4bf4bdae49113369ef536ed81d6
1 parent b9d058a commit df6c48c

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed

babel-preset/configs/main.js

+55-36
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,105 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*
98
*/
109
'use strict';
1110

12-
var resolvePlugins = require('../lib/resolvePlugins');
11+
const resolvePlugins = require('../lib/resolvePlugins');
12+
const resolvePlugin = resolvePlugins.resolvePlugin;
13+
14+
const defaultPlugins = resolvePlugins([
15+
'syntax-class-properties',
16+
'syntax-trailing-function-commas',
17+
'transform-class-properties',
18+
'transform-es2015-block-scoping',
19+
'transform-es2015-computed-properties',
20+
'transform-es2015-destructuring',
21+
'transform-es2015-function-name',
22+
'transform-es2015-literals',
23+
'transform-es2015-parameters',
24+
'transform-es2015-shorthand-properties',
25+
'transform-flow-strip-types',
26+
'transform-react-jsx',
27+
'transform-regenerator',
28+
[
29+
'transform-es2015-modules-commonjs',
30+
{strict: false, allowTopLevelThis: true},
31+
],
32+
]);
33+
34+
const checkES2015Constants = resolvePlugin('check-es2015-constants');
35+
const es2015ArrowFunctions = resolvePlugin('transform-es2015-arrow-functions');
36+
const es2015Classes = resolvePlugin('transform-es2015-classes');
37+
const es2015ForOf = resolvePlugin(['transform-es2015-for-of', {loose: true}]);
38+
const es2015Spread = resolvePlugin('transform-es2015-spread');
39+
const es2015TemplateLiterals = resolvePlugin(
40+
'transform-es2015-template-literals'
41+
);
42+
const asyncFunctions = resolvePlugin('syntax-async-functions');
43+
const exponentiationOperator = resolvePlugin(
44+
'transform-exponentiation-operator'
45+
);
46+
const objectAssign = resolvePlugin('transform-object-assign');
47+
const objectRestSpread = resolvePlugin('transform-object-rest-spread');
48+
const reactDisplayName = resolvePlugin('transform-react-display-name');
49+
const reactJsxSource = resolvePlugin('transform-react-jsx-source');
50+
const symbolMember = [require('../transforms/transform-symbol-member')];
1351

1452
const getPreset = (src, options) => {
15-
const plugins = [];
1653
const isNull = src === null || src === undefined;
1754
const hasClass = isNull || src.indexOf('class') !== -1;
1855
const hasForOf =
1956
isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);
2057

21-
plugins.push(
22-
'syntax-class-properties',
23-
'syntax-trailing-function-commas',
24-
'transform-class-properties',
25-
'transform-es2015-block-scoping',
26-
'transform-es2015-computed-properties',
27-
'transform-es2015-destructuring',
28-
'transform-es2015-function-name',
29-
'transform-es2015-literals',
30-
'transform-es2015-parameters',
31-
'transform-es2015-shorthand-properties',
32-
'transform-flow-strip-types',
33-
'transform-react-jsx',
34-
'transform-regenerator',
35-
[
36-
'transform-es2015-modules-commonjs',
37-
{strict: false, allowTopLevelThis: true},
38-
]
39-
);
58+
const extraPlugins = [];
4059

4160
if (isNull || src.indexOf('async') !== -1 || src.indexOf('await') !== -1) {
42-
plugins.push('syntax-async-functions');
61+
extraPlugins.push(asyncFunctions);
4362
}
4463
if (hasClass) {
45-
plugins.push('transform-es2015-classes');
64+
extraPlugins.push(es2015Classes);
4665
}
4766
if (isNull || src.indexOf('=>') !== -1) {
48-
plugins.push('transform-es2015-arrow-functions');
67+
extraPlugins.push(es2015ArrowFunctions);
4968
}
5069
if (isNull || src.indexOf('const') !== -1) {
51-
plugins.push('check-es2015-constants');
70+
extraPlugins.push(checkES2015Constants);
5271
}
5372
if (isNull || hasClass || src.indexOf('...') !== -1) {
54-
plugins.push('transform-es2015-spread');
55-
plugins.push('transform-object-rest-spread');
73+
extraPlugins.push(es2015Spread);
74+
extraPlugins.push(objectRestSpread);
5675
}
5776
if (isNull || src.indexOf('`') !== -1) {
58-
plugins.push('transform-es2015-template-literals');
77+
extraPlugins.push(es2015TemplateLiterals);
5978
}
6079
if (isNull || src.indexOf('**') !== -1) {
61-
plugins.push('transform-exponentiation-operator');
80+
extraPlugins.push(exponentiationOperator);
6281
}
6382
if (isNull || src.indexOf('Object.assign') !== -1) {
64-
plugins.push('transform-object-assign');
83+
extraPlugins.push(objectAssign);
6584
}
6685
if (hasForOf) {
67-
plugins.push(['transform-es2015-for-of', {loose: true}]);
86+
extraPlugins.push(es2015ForOf);
6887
}
6988
if (hasForOf || src.indexOf('Symbol') !== -1) {
70-
plugins.push(require('../transforms/transform-symbol-member'));
89+
extraPlugins.push(symbolMember);
7190
}
7291
if (
7392
isNull ||
7493
src.indexOf('React.createClass') !== -1 ||
7594
src.indexOf('createReactClass') !== -1
7695
) {
77-
plugins.push('transform-react-display-name');
96+
extraPlugins.push(reactDisplayName);
7897
}
7998

8099
if (options && options.dev) {
81-
plugins.push('transform-react-jsx-source');
100+
extraPlugins.push(reactJsxSource);
82101
}
83102

84103
return {
85104
comments: false,
86105
compact: true,
87-
plugins: resolvePlugins(plugins),
106+
plugins: defaultPlugins.concat(extraPlugins),
88107
};
89108
};
90109

babel-preset/lib/resolvePlugins.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
* installed in the react-native package.
1616
*/
1717
function resolvePlugins(plugins) {
18-
return plugins.map(function(plugin) {
19-
// Normalise plugin to an array.
20-
if (!Array.isArray(plugin)) {
21-
plugin = [plugin];
22-
}
23-
// Only resolve the plugin if it's a string reference.
24-
if (typeof plugin[0] === 'string') {
25-
plugin[0] = require('babel-plugin-' + plugin[0]);
26-
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
27-
}
28-
return plugin;
29-
});
18+
return plugins.map(resolvePlugin);
19+
}
20+
21+
/**
22+
* Manually resolve a single Babel plugin.
23+
*/
24+
function resolvePlugin(plugin) {
25+
// Normalise plugin to an array.
26+
if (!Array.isArray(plugin)) {
27+
plugin = [plugin];
28+
}
29+
// Only resolve the plugin if it's a string reference.
30+
if (typeof plugin[0] === 'string') {
31+
plugin[0] = require('babel-plugin-' + plugin[0]);
32+
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
33+
}
34+
return plugin;
3035
}
3136

3237
module.exports = resolvePlugins;
38+
module.exports.resolvePlugin = resolvePlugin;

0 commit comments

Comments
 (0)