mangleString.js
5.13 KB
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
var esprima = require('esprima');
var escodegen = require('escodegen');
var estraverse = require('estraverse');
var SYNTAX = estraverse.Syntax;
var STR_MIN_LENGTH = 5;
var STR_MIN_DIST = 1000;
var STR_MIN_COUNT = 2;
function createDeclaration(declarations) {
return {
type: SYNTAX.VariableDeclaration,
declarations: declarations,
kind: 'var'
};
}
function createDeclarator(id, init) {
return {
type: SYNTAX.VariableDeclarator,
id: {
type: SYNTAX.Identifier,
name: id
},
init: {
type: SYNTAX.Literal,
value: init
}
};
}
function base54Digits() {
return 'etnrisouaflchpdvmgybwESxTNCkLAOM_DPHBjFIqRUzWXV$JKQGYZ0516372984';
}
var base54 = (function(){
var DIGITS = base54Digits();
return function(num) {
var ret = '';
var base = 54;
do {
ret += DIGITS.charAt(num % base);
num = Math.floor(num / base);
base = 64;
} while (num > 0);
return ret;
};
})();
function mangleString(source) {
var ast = esprima.parse(source, {
loc: true
});
var stringVariables = {};
var stringRelaceCount = 0;
estraverse.traverse(ast, {
enter: function (node, parent) {
if (node.type === SYNTAX.Literal
&& typeof node.value === 'string'
) {
// Ignore if string is the key of property
if (parent.type === SYNTAX.Property) {
return;
}
var value = node.value;
if (value.length > STR_MIN_LENGTH) {
if (!stringVariables[value]) {
stringVariables[value] = {
count: 0,
lastLoc: node.loc.start.line,
name: '__echartsString__' + base54(stringRelaceCount++)
};
}
var diff = node.loc.start.line - stringVariables[value].lastLoc;
// GZIP ?
if (diff >= STR_MIN_DIST) {
stringVariables[value].lastLoc = node.loc.start.line;
stringVariables[value].count++;
}
}
}
if (node.type === SYNTAX.MemberExpression && !node.computed) {
if (node.property.type === SYNTAX.Identifier) {
var value = node.property.name;
if (value.length > STR_MIN_LENGTH) {
if (!stringVariables[value]) {
stringVariables[value] = {
count: 0,
lastLoc: node.loc.start.line,
name: '__echartsString__' + base54(stringRelaceCount++)
};
}
var diff = node.loc.start.line - stringVariables[value].lastLoc;
if (diff >= STR_MIN_DIST) {
stringVariables[value].lastLoc = node.loc.start.line;
stringVariables[value].count++;
}
}
}
}
}
});
estraverse.replace(ast, {
enter: function (node, parent) {
if ((node.type === SYNTAX.Literal
&& typeof node.value === 'string')
) {
// Ignore if string is the key of property
if (parent.type === SYNTAX.Property) {
return;
}
var str = node.value;
if (stringVariables[str] && stringVariables[str].count > STR_MIN_COUNT) {
return {
type: SYNTAX.Identifier,
name: stringVariables[str].name
};
}
}
if (node.type === SYNTAX.MemberExpression && !node.computed) {
if (node.property.type === SYNTAX.Identifier) {
var str = node.property.name;
if (stringVariables[str] && stringVariables[str].count > STR_MIN_COUNT) {
return {
type: SYNTAX.MemberExpression,
object: node.object,
property: {
type: SYNTAX.Identifier,
name: stringVariables[str].name
},
computed: true
};
}
}
}
}
});
// Add variables in the top
for (var str in stringVariables) {
// Used more than once
if (stringVariables[str].count > STR_MIN_COUNT) {
ast.body.unshift(createDeclaration([
createDeclarator(stringVariables[str].name, str)
]));
}
}
return escodegen.generate(
ast,
{
format: {escapeless: true},
comment: true
}
);
}
exports = module.exports = mangleString;