Make constant folding plugin use context for babel types

Summary: This makes the plugin use the passed on babel api rather than hardcoded pulling in babel 6 or 7. This is important since they are different packages now and we need to work with them side-by-side.

Reviewed By: mjesun

Differential Revision: D7083873

fbshipit-source-id: 2ce89e0627005bb29ff8d3e0e1ac51fe8ee4eabd
This commit is contained in:
Peter van der Zee 2018-02-26 07:43:25 -08:00 committed by Facebook Github Bot
parent f179352292
commit 2303f3b18d
6 changed files with 96 additions and 93 deletions

View File

@ -10,12 +10,29 @@
*/
'use strict';
const constantFolding = require('../constant-folding');
const constantFoldingPlugin = require('../constant-folding-plugin');
const {transformSync} = require('../../../babel-bridge');
const {transformFromAstSync} = require('../../../babel-bridge');
import type {TransformResult} from '@babel/core';
function constantFolding(
filename: string,
transformResult: TransformResult,
): TransformResult {
return transformFromAstSync(transformResult.ast, transformResult.code, {
filename,
plugins: [constantFoldingPlugin],
inputSourceMap: transformResult.map || undefined, // may not be null
sourceMaps: true,
sourceFileName: filename,
babelrc: false,
compact: true,
retainLines: true,
});
}
function parse(code: string): TransformResult {
return transformSync(code, {
code: false,

View File

@ -10,7 +10,7 @@
'use strict';
jest
.mock('../constant-folding')
.mock('../constant-folding-plugin')
.mock('../inline-plugin')
.mock('metro-minify-uglify');

View File

@ -0,0 +1,73 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import typeof {types as BabelTypes} from 'babel-core';
function constantFoldingPlugin(context: {types: BabelTypes}) {
const t = context.types;
const Conditional = {
exit(path: Object) {
const node = path.node;
const test = node.test;
if (t.isLiteral(test)) {
if (test.value || node.alternate) {
path.replaceWith(test.value ? node.consequent : node.alternate);
} else if (!test.value) {
path.remove();
}
}
},
};
return {
visitor: {
BinaryExpression: {
exit(path: Object) {
const node = path.node;
if (t.isLiteral(node.left) && t.isLiteral(node.right)) {
const result = path.evaluate();
if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
}
}
},
},
ConditionalExpression: Conditional,
IfStatement: Conditional,
LogicalExpression: {
exit(path: Object) {
const node = path.node;
const left = node.left;
if (t.isLiteral(left)) {
const value = t.isNullLiteral(left) ? null : left.value;
if (node.operator === '||') {
path.replaceWith(value ? left : node.right);
} else {
path.replaceWith(value ? node.right : left);
}
}
},
},
UnaryExpression: {
exit(path: Object) {
const node = path.node;
if (node.operator === '!' && t.isLiteral(node.argument)) {
path.replaceWith(t.valueToNode(!node.argument.value));
}
},
},
},
};
}
module.exports = constantFoldingPlugin;

View File

@ -1,86 +0,0 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const {babelTypes: t} = require('../../babel-bridge');
const {transformFromAstSync} = require('../../babel-bridge');
import type {TransformResult} from '@babel/core';
const Conditional = {
exit(path) {
const node = path.node;
const test = node.test;
if (t.isLiteral(test)) {
if (test.value || node.alternate) {
path.replaceWith(test.value ? node.consequent : node.alternate);
} else if (!test.value) {
path.remove();
}
}
},
};
const plugin = {
visitor: {
BinaryExpression: {
exit(path) {
const node = path.node;
if (t.isLiteral(node.left) && t.isLiteral(node.right)) {
const result = path.evaluate();
if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
}
}
},
},
ConditionalExpression: Conditional,
IfStatement: Conditional,
LogicalExpression: {
exit(path) {
const node = path.node;
const left = node.left;
if (t.isLiteral(left)) {
const value = t.isNullLiteral(left) ? null : left.value;
if (node.operator === '||') {
path.replaceWith(value ? left : node.right);
} else {
path.replaceWith(value ? node.right : left);
}
}
},
},
UnaryExpression: {
exit(path) {
const node = path.node;
if (node.operator === '!' && t.isLiteral(node.argument)) {
path.replaceWith(t.valueToNode(!node.argument.value));
}
},
},
},
};
function constantFolding(filename: string, transformResult: TransformResult) {
return transformFromAstSync(transformResult.ast, transformResult.code, {
filename,
plugins: [plugin],
inputSourceMap: transformResult.map || undefined, // may not be null
sourceMaps: true,
sourceFileName: filename,
babelrc: false,
compact: true,
retainLines: true,
});
}
constantFolding.plugin = plugin;
module.exports = constantFolding;

View File

@ -14,7 +14,7 @@ const JsFileWrapping = require('../../ModuleGraph/worker/JsFileWrapping');
const assetTransformer = require('../../assetTransformer');
const collectDependencies = require('../../ModuleGraph/worker/collectDependencies');
const constantFolding = require('./constant-folding');
const constantFoldingPlugin = require('./constant-folding-plugin');
const getMinifier = require('../../lib/getMinifier');
const inlinePlugin = require('./inline-plugin');
const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies');
@ -231,7 +231,7 @@ function transformCode(
const plugins = options.dev
? []
: [[inlinePlugin, options], [constantFolding.plugin, options]];
: [[inlinePlugin, options], [constantFoldingPlugin, options]];
// $FlowFixMe TODO t26372934 Plugin system
const transformer: Transformer<*> = require(transformerPath);

View File

@ -10,8 +10,7 @@
'use strict';
const constantFolding = require('../../JSTransformer/worker/constant-folding')
.plugin;
const constantFoldingPlugin = require('../../JSTransformer/worker/constant-folding-plugin');
const generate = require('./generate');
const getMinifier = require('../../lib/getMinifier');
const inlinePlugin = require('../../JSTransformer/worker/inline-plugin');
@ -117,7 +116,7 @@ function optimizeCode(
): BabelTransformResult {
return transformSync(code, {
plugins: [
[constantFolding],
[constantFoldingPlugin],
[inlinePlugin, {...inliningOptions, isWrapped: true}],
],
babelrc: false,