/
index.js
185 lines (163 loc) · 5.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict";
const evaluate = require("babel-helper-evaluate-path");
const { FALLBACK_HANDLER } = require("./replacements");
function getName(member) {
if (member.computed) {
switch (member.property.type) {
case "StringLiteral":
case "NumericLiteral":
return member.property.value;
case "TemplateLiteral":
return;
}
} else {
return member.property.name;
}
}
function swap(path, member, handlers, ...args) {
const key = getName(member);
if (key === undefined) return;
let handler = handlers[key];
if (
typeof handler !== "function" ||
!Object.hasOwnProperty.call(handlers, key)
) {
if (typeof handlers[FALLBACK_HANDLER] === "function") {
handler = handlers[FALLBACK_HANDLER].bind(member.object, key);
} else {
return false;
}
}
const replacement = handler.apply(member.object, args);
if (replacement) {
path.replaceWith(replacement);
return true;
}
return false;
}
module.exports = babel => {
const replacements = require("./replacements.js")(babel);
const seen = Symbol("seen");
const { types: t, traverse } = babel;
return {
name: "minify-constant-folding",
visitor: {
// Evaluate string expressions that are next to each other
// but are not actually a binary expression.
// "a" + b + "c" + "d" -> "a" + b + "cd"
BinaryExpression(path) {
let literal, bin;
if (path.get("right").isStringLiteral()) {
literal = path.get("right");
if (path.get("left").isBinaryExpression({ operator: "+" })) {
bin = path.get("left");
} else {
return;
}
} else if (path.get("left").isStringLiteral()) {
literal = path.get("left");
if (path.get("right").isBinaryExpression({ operator: "+" })) {
bin = path.get("right");
} else {
return;
}
} else {
return;
}
const relevant = getLeaf(bin, literal.key);
if (!relevant) {
return;
}
const value = literal.key === "right"
? relevant.node.value + literal.node.value
: literal.node.value + relevant.node.value;
relevant.replaceWith(t.stringLiteral(value));
path.replaceWith(bin.node);
function getLeaf(path, direction) {
if (path.isStringLiteral()) {
return path;
} else if (path.isBinaryExpression({ operator: "+" })) {
return getLeaf(path.get(direction), direction);
}
}
},
// TODO: look into evaluating binding too (could result in more code, but gzip?)
Expression(path) {
const { node } = path;
if (node[seen]) {
return;
}
if (path.isLiteral()) {
return;
}
if (!path.isPure()) {
return;
}
if (
traverse.hasType(node, path.scope, "Identifier", t.FUNCTION_TYPES)
) {
return;
}
// -0 maybe compared via dividing and then checking against -Infinity
// Also -X will always be -X.
if (
t.isUnaryExpression(node, { operator: "-" }) &&
t.isNumericLiteral(node.argument)
) {
return;
}
// We have a transform that converts true/false to !0/!1
if (
t.isUnaryExpression(node, { operator: "!" }) &&
t.isNumericLiteral(node.argument)
) {
if (node.argument.value === 0 || node.argument.value === 1) {
return;
}
}
// void 0 is used for undefined.
if (
t.isUnaryExpression(node, { operator: "void" }) &&
t.isNumericLiteral(node.argument, { value: 0 })
) {
return;
}
const res = evaluate(path);
if (res.confident) {
// Avoid fractions because they can be longer than the original expression.
// There is also issues with number percision?
if (typeof res.value === "number" && !Number.isInteger(res.value)) {
return;
}
// Preserve -0
if (typeof res.value === "number" && res.value === 0) {
if (1 / res.value === -Infinity) {
const node = t.unaryExpression("-", t.numericLiteral(0), true);
node[seen] = true;
path.replaceWith(node);
return;
}
}
const node = t.valueToNode(res.value);
node[seen] = true;
path.replaceWith(node);
}
},
CallExpression(path) {
const { node } = path;
const { callee: member } = node;
if (t.isMemberExpression(member)) {
const helpers = replacements[member.object.type];
if (!helpers || !helpers.calls) return;
swap(path, member, helpers.calls, ...node.arguments);
}
},
MemberExpression(path) {
const { node: member } = path;
const helpers = replacements[member.object.type];
if (!helpers || !helpers.members) return;
swap(path, member, helpers.members);
}
}
};
};