Improve transform speed by 8.5%

Reviewed By: jeanlauliac

Differential Revision: D5443966

fbshipit-source-id: 06a151f44b7b9f8307be2ea47ce35deb3663869f
This commit is contained in:
Christoph Nakazawa 2017-07-21 09:37:02 -07:00 committed by Facebook Github Bot
parent 365aea3415
commit 4caf79471b
2 changed files with 76 additions and 48 deletions

View File

@ -1,16 +0,0 @@
/**
* 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

@ -5,55 +5,99 @@
* 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.
*
* @format
*/ */
'use strict'; 'use strict';
var resolvePlugins = require('../lib/resolvePlugins'); var resolvePlugins = require('../lib/resolvePlugins');
var base = { const getPreset = (src, options) => {
comments: false, const plugins = [];
compact: true, const isNull = src === null || src === undefined;
plugins: resolvePlugins([ const hasClass = isNull || src.indexOf('class') !== -1;
'syntax-async-functions', const hasForOf =
isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);
plugins.push(
'syntax-class-properties', 'syntax-class-properties',
'syntax-trailing-function-commas', 'syntax-trailing-function-commas',
'transform-class-properties', 'transform-class-properties',
'transform-es2015-function-name',
'transform-es2015-arrow-functions',
'transform-es2015-block-scoping', 'transform-es2015-block-scoping',
'transform-es2015-classes',
'transform-es2015-computed-properties', 'transform-es2015-computed-properties',
'check-es2015-constants',
'transform-es2015-destructuring', 'transform-es2015-destructuring',
['transform-es2015-modules-commonjs', { strict: false, allowTopLevelThis: true }], 'transform-es2015-function-name',
'transform-es2015-literals',
'transform-es2015-parameters', 'transform-es2015-parameters',
'transform-es2015-shorthand-properties', 'transform-es2015-shorthand-properties',
'transform-es2015-spread',
'transform-es2015-template-literals',
'transform-es2015-literals',
'transform-flow-strip-types', 'transform-flow-strip-types',
'transform-object-assign',
'transform-object-rest-spread',
'transform-react-display-name',
'transform-react-jsx', 'transform-react-jsx',
'transform-regenerator', 'transform-regenerator',
['transform-es2015-for-of', { loose: true }],
require('../transforms/transform-regenerator-runtime-insertion'), require('../transforms/transform-regenerator-runtime-insertion'),
require('../transforms/transform-symbol-member'), [
]), 'transform-es2015-modules-commonjs',
}; {strict: false, allowTopLevelThis: true},
],
);
var devTools = Object.assign({}, base); if (isNull || src.indexOf('async') !== -1 || src.indexOf('await') !== -1) {
devTools.plugins = devTools.plugins.concat( plugins.push('syntax-async-functions');
resolvePlugins(['transform-react-jsx-source'])
);
module.exports = function(options) {
var withDevTools = options.withDevTools;
if (withDevTools == null) {
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
withDevTools = !env || env === 'development';
} }
return withDevTools ? devTools : base; if (hasClass) {
plugins.push('transform-es2015-classes');
}
if (isNull || src.indexOf('=>') !== -1) {
plugins.push('transform-es2015-arrow-functions');
}
if (isNull || src.indexOf('const') !== -1) {
plugins.push('check-es2015-constants');
}
if (isNull || hasClass || src.indexOf('...') !== -1) {
plugins.push('transform-es2015-spread');
plugins.push('transform-object-rest-spread');
}
if (isNull || src.indexOf('`') !== -1) {
plugins.push('transform-es2015-template-literals');
}
if (isNull || src.indexOf('Object.assign') !== -1) {
plugins.push('transform-object-assign');
}
if (hasForOf) {
plugins.push(['transform-es2015-for-of', {loose: true}]);
}
if (hasForOf || src.indexOf('Symbol') !== -1) {
plugins.push(require('../transforms/transform-symbol-member'));
}
if (
isNull ||
src.indexOf('React.createClass') !== -1 ||
src.indexOf('createReactClass') !== -1
) {
plugins.push('transform-react-display-name');
}
if (options && options.dev) {
plugins.push('transform-react-jsx-source');
}
return {
comments: false,
compact: true,
plugins: resolvePlugins(plugins),
};
}; };
const base = getPreset(null);
const devTools = getPreset(null, {dev: true});
module.exports = options => {
if (options.withDevTools == null) {
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
if (!env || env === 'development') {
return devTools;
}
}
return base;
};
module.exports.getPreset = getPreset;