mirror of
https://github.com/status-im/react-native.git
synced 2025-02-10 00:23:26 +00:00
babel-preset-react-native
: only require plugins once
Reviewed By: cpojer Differential Revision: D6795591 fbshipit-source-id: 30b689e2cf72a4bf4bdae49113369ef536ed81d6
This commit is contained in:
parent
b9d058a05c
commit
df6c48cf36
@ -5,86 +5,105 @@
|
|||||||
* This source code is licensed under the BSD-style license found in the
|
* 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
|
* 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.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var resolvePlugins = require('../lib/resolvePlugins');
|
const resolvePlugins = require('../lib/resolvePlugins');
|
||||||
|
const resolvePlugin = resolvePlugins.resolvePlugin;
|
||||||
|
|
||||||
|
const defaultPlugins = resolvePlugins([
|
||||||
|
'syntax-class-properties',
|
||||||
|
'syntax-trailing-function-commas',
|
||||||
|
'transform-class-properties',
|
||||||
|
'transform-es2015-block-scoping',
|
||||||
|
'transform-es2015-computed-properties',
|
||||||
|
'transform-es2015-destructuring',
|
||||||
|
'transform-es2015-function-name',
|
||||||
|
'transform-es2015-literals',
|
||||||
|
'transform-es2015-parameters',
|
||||||
|
'transform-es2015-shorthand-properties',
|
||||||
|
'transform-flow-strip-types',
|
||||||
|
'transform-react-jsx',
|
||||||
|
'transform-regenerator',
|
||||||
|
[
|
||||||
|
'transform-es2015-modules-commonjs',
|
||||||
|
{strict: false, allowTopLevelThis: true},
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const checkES2015Constants = resolvePlugin('check-es2015-constants');
|
||||||
|
const es2015ArrowFunctions = resolvePlugin('transform-es2015-arrow-functions');
|
||||||
|
const es2015Classes = resolvePlugin('transform-es2015-classes');
|
||||||
|
const es2015ForOf = resolvePlugin(['transform-es2015-for-of', {loose: true}]);
|
||||||
|
const es2015Spread = resolvePlugin('transform-es2015-spread');
|
||||||
|
const es2015TemplateLiterals = resolvePlugin(
|
||||||
|
'transform-es2015-template-literals'
|
||||||
|
);
|
||||||
|
const asyncFunctions = resolvePlugin('syntax-async-functions');
|
||||||
|
const exponentiationOperator = resolvePlugin(
|
||||||
|
'transform-exponentiation-operator'
|
||||||
|
);
|
||||||
|
const objectAssign = resolvePlugin('transform-object-assign');
|
||||||
|
const objectRestSpread = resolvePlugin('transform-object-rest-spread');
|
||||||
|
const reactDisplayName = resolvePlugin('transform-react-display-name');
|
||||||
|
const reactJsxSource = resolvePlugin('transform-react-jsx-source');
|
||||||
|
const symbolMember = [require('../transforms/transform-symbol-member')];
|
||||||
|
|
||||||
const getPreset = (src, options) => {
|
const getPreset = (src, options) => {
|
||||||
const plugins = [];
|
|
||||||
const isNull = src === null || src === undefined;
|
const isNull = src === null || src === undefined;
|
||||||
const hasClass = isNull || src.indexOf('class') !== -1;
|
const hasClass = isNull || src.indexOf('class') !== -1;
|
||||||
const hasForOf =
|
const hasForOf =
|
||||||
isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);
|
isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);
|
||||||
|
|
||||||
plugins.push(
|
const extraPlugins = [];
|
||||||
'syntax-class-properties',
|
|
||||||
'syntax-trailing-function-commas',
|
|
||||||
'transform-class-properties',
|
|
||||||
'transform-es2015-block-scoping',
|
|
||||||
'transform-es2015-computed-properties',
|
|
||||||
'transform-es2015-destructuring',
|
|
||||||
'transform-es2015-function-name',
|
|
||||||
'transform-es2015-literals',
|
|
||||||
'transform-es2015-parameters',
|
|
||||||
'transform-es2015-shorthand-properties',
|
|
||||||
'transform-flow-strip-types',
|
|
||||||
'transform-react-jsx',
|
|
||||||
'transform-regenerator',
|
|
||||||
[
|
|
||||||
'transform-es2015-modules-commonjs',
|
|
||||||
{strict: false, allowTopLevelThis: true},
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isNull || src.indexOf('async') !== -1 || src.indexOf('await') !== -1) {
|
if (isNull || src.indexOf('async') !== -1 || src.indexOf('await') !== -1) {
|
||||||
plugins.push('syntax-async-functions');
|
extraPlugins.push(asyncFunctions);
|
||||||
}
|
}
|
||||||
if (hasClass) {
|
if (hasClass) {
|
||||||
plugins.push('transform-es2015-classes');
|
extraPlugins.push(es2015Classes);
|
||||||
}
|
}
|
||||||
if (isNull || src.indexOf('=>') !== -1) {
|
if (isNull || src.indexOf('=>') !== -1) {
|
||||||
plugins.push('transform-es2015-arrow-functions');
|
extraPlugins.push(es2015ArrowFunctions);
|
||||||
}
|
}
|
||||||
if (isNull || src.indexOf('const') !== -1) {
|
if (isNull || src.indexOf('const') !== -1) {
|
||||||
plugins.push('check-es2015-constants');
|
extraPlugins.push(checkES2015Constants);
|
||||||
}
|
}
|
||||||
if (isNull || hasClass || src.indexOf('...') !== -1) {
|
if (isNull || hasClass || src.indexOf('...') !== -1) {
|
||||||
plugins.push('transform-es2015-spread');
|
extraPlugins.push(es2015Spread);
|
||||||
plugins.push('transform-object-rest-spread');
|
extraPlugins.push(objectRestSpread);
|
||||||
}
|
}
|
||||||
if (isNull || src.indexOf('`') !== -1) {
|
if (isNull || src.indexOf('`') !== -1) {
|
||||||
plugins.push('transform-es2015-template-literals');
|
extraPlugins.push(es2015TemplateLiterals);
|
||||||
}
|
}
|
||||||
if (isNull || src.indexOf('**') !== -1) {
|
if (isNull || src.indexOf('**') !== -1) {
|
||||||
plugins.push('transform-exponentiation-operator');
|
extraPlugins.push(exponentiationOperator);
|
||||||
}
|
}
|
||||||
if (isNull || src.indexOf('Object.assign') !== -1) {
|
if (isNull || src.indexOf('Object.assign') !== -1) {
|
||||||
plugins.push('transform-object-assign');
|
extraPlugins.push(objectAssign);
|
||||||
}
|
}
|
||||||
if (hasForOf) {
|
if (hasForOf) {
|
||||||
plugins.push(['transform-es2015-for-of', {loose: true}]);
|
extraPlugins.push(es2015ForOf);
|
||||||
}
|
}
|
||||||
if (hasForOf || src.indexOf('Symbol') !== -1) {
|
if (hasForOf || src.indexOf('Symbol') !== -1) {
|
||||||
plugins.push(require('../transforms/transform-symbol-member'));
|
extraPlugins.push(symbolMember);
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
isNull ||
|
isNull ||
|
||||||
src.indexOf('React.createClass') !== -1 ||
|
src.indexOf('React.createClass') !== -1 ||
|
||||||
src.indexOf('createReactClass') !== -1
|
src.indexOf('createReactClass') !== -1
|
||||||
) {
|
) {
|
||||||
plugins.push('transform-react-display-name');
|
extraPlugins.push(reactDisplayName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options && options.dev) {
|
if (options && options.dev) {
|
||||||
plugins.push('transform-react-jsx-source');
|
extraPlugins.push(reactJsxSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
comments: false,
|
comments: false,
|
||||||
compact: true,
|
compact: true,
|
||||||
plugins: resolvePlugins(plugins),
|
plugins: defaultPlugins.concat(extraPlugins),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -15,18 +15,24 @@
|
|||||||
* installed in the react-native package.
|
* installed in the react-native package.
|
||||||
*/
|
*/
|
||||||
function resolvePlugins(plugins) {
|
function resolvePlugins(plugins) {
|
||||||
return plugins.map(function(plugin) {
|
return plugins.map(resolvePlugin);
|
||||||
// Normalise plugin to an array.
|
}
|
||||||
if (!Array.isArray(plugin)) {
|
|
||||||
plugin = [plugin];
|
/**
|
||||||
}
|
* Manually resolve a single Babel plugin.
|
||||||
// Only resolve the plugin if it's a string reference.
|
*/
|
||||||
if (typeof plugin[0] === 'string') {
|
function resolvePlugin(plugin) {
|
||||||
plugin[0] = require('babel-plugin-' + plugin[0]);
|
// Normalise plugin to an array.
|
||||||
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
|
if (!Array.isArray(plugin)) {
|
||||||
}
|
plugin = [plugin];
|
||||||
return plugin;
|
}
|
||||||
});
|
// Only resolve the plugin if it's a string reference.
|
||||||
|
if (typeof plugin[0] === 'string') {
|
||||||
|
plugin[0] = require('babel-plugin-' + plugin[0]);
|
||||||
|
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
|
||||||
|
}
|
||||||
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = resolvePlugins;
|
module.exports = resolvePlugins;
|
||||||
|
module.exports.resolvePlugin = resolvePlugin;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user