Skip to content

Commit

Permalink
Improve transform speed by 8.5%
Browse files Browse the repository at this point in the history
Reviewed By: jeanlauliac

Differential Revision: D5443966

fbshipit-source-id: 06a151f44b7b9f8307be2ea47ce35deb3663869f
  • Loading branch information
cpojer authored and facebook-github-bot committed Jul 21, 2017
1 parent 365aea3 commit 4caf794
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 46 deletions.
16 changes: 0 additions & 16 deletions babel-preset/configs/internal.js

This file was deleted.

104 changes: 74 additions & 30 deletions babel-preset/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,99 @@
* 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.
*
* @format
*/
'use strict';

var resolvePlugins = require('../lib/resolvePlugins');

var base = {
comments: false,
compact: true,
plugins: resolvePlugins([
'syntax-async-functions',
const getPreset = (src, options) => {
const plugins = [];
const isNull = src === null || src === undefined;
const hasClass = isNull || src.indexOf('class') !== -1;
const hasForOf =
isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);

plugins.push(
'syntax-class-properties',
'syntax-trailing-function-commas',
'transform-class-properties',
'transform-es2015-function-name',
'transform-es2015-arrow-functions',
'transform-es2015-block-scoping',
'transform-es2015-classes',
'transform-es2015-computed-properties',
'check-es2015-constants',
'transform-es2015-destructuring',
['transform-es2015-modules-commonjs', { strict: false, allowTopLevelThis: true }],
'transform-es2015-function-name',
'transform-es2015-literals',
'transform-es2015-parameters',
'transform-es2015-shorthand-properties',
'transform-es2015-spread',
'transform-es2015-template-literals',
'transform-es2015-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 }],
require('../transforms/transform-regenerator-runtime-insertion'),
require('../transforms/transform-symbol-member'),
]),
};
[
'transform-es2015-modules-commonjs',
{strict: false, allowTopLevelThis: true},
],
);

if (isNull || src.indexOf('async') !== -1 || src.indexOf('await') !== -1) {
plugins.push('syntax-async-functions');
}
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');
}

var devTools = Object.assign({}, base);
devTools.plugins = devTools.plugins.concat(
resolvePlugins(['transform-react-jsx-source'])
);
if (options && options.dev) {
plugins.push('transform-react-jsx-source');
}

return {
comments: false,
compact: true,
plugins: resolvePlugins(plugins),
};
};

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';
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 withDevTools ? devTools : base;
return base;
};

module.exports.getPreset = getPreset;

1 comment on commit 4caf794

@orta
Copy link
Contributor

@orta orta commented on 4caf794 Jul 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 impressive

Please sign in to comment.