-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSMinify.chai
More file actions
179 lines (158 loc) · 4.7 KB
/
JSMinify.chai
File metadata and controls
179 lines (158 loc) · 4.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Ditto JavaScript Minifier - Production Grade v1.2 (Strict JS Detection)
var oldString = clip.GetAsciiString();
if (oldString.size() == 0 || oldString.size() > 500000)
{
return false;
}
// Trim leading whitespace
var startIdx = 0;
while (startIdx < oldString.size() &&
(oldString[startIdx] == ' ' || oldString[startIdx] == '\n' ||
oldString[startIdx] == '\r' || oldString[startIdx] == '\t'))
{
startIdx = startIdx + 1;
}
if (startIdx >= oldString.size())
{
return false;
}
// STRICT: Reject if starts with { or [ (it's JSON, not JS)
var firstChar = oldString[startIdx];
if (firstChar == '{' || firstChar == '[')
{
return false;
}
// STRICT: Must have STRONG JavaScript indicators
// These patterns don't appear in JSON
var hasFunction = oldString.find("function ") != -1 || oldString.find("function(") != -1;
var hasArrow = oldString.find("=>") != -1;
var hasDeclaration = oldString.find("const ") != -1 || oldString.find("let ") != -1 || oldString.find("var ") != -1;
var hasClass = oldString.find("class ") != -1;
var hasImport = oldString.find("import ") != -1 || oldString.find("export ") != -1;
var hasFor = oldString.find("for (") != -1 || oldString.find("for(") != -1;
var hasIf = oldString.find("if (") != -1 || oldString.find("if(") != -1;
var hasReturn = oldString.find("return ") != -1 || oldString.find("return;") != -1;
// Require at least 2 strong indicators
var jsScore = 0;
if (hasFunction) { jsScore = jsScore + 2; }
if (hasArrow) { jsScore = jsScore + 2; }
if (hasDeclaration) { jsScore = jsScore + 2; }
if (hasClass) { jsScore = jsScore + 2; }
if (hasImport) { jsScore = jsScore + 2; }
if (hasFor) { jsScore = jsScore + 1; }
if (hasIf) { jsScore = jsScore + 1; }
if (hasReturn) { jsScore = jsScore + 1; }
// Need score >= 2 to be considered JavaScript
if (jsScore < 2)
{
return false;
}
// Already minified check
if (oldString.find("\n") == -1)
{
return false;
}
// ... rest of minification code stays the same ...
var result = "";
var inString = false;
var stringChar = ' ';
var escapeNext = false;
for (var i = 0; i < oldString.size(); ++i)
{
var ch = oldString[i];
var nextCh = ' ';
if (i + 1 < oldString.size())
{
nextCh = oldString[i + 1];
}
if (escapeNext)
{
result = result + ch;
escapeNext = false;
continue;
}
if (inString)
{
result = result + ch;
if (ch == '\\')
{
escapeNext = true;
}
else if (ch == stringChar)
{
inString = false;
}
continue;
}
if (ch == '"' || ch == '\'' || ch == '`')
{
inString = true;
stringChar = ch;
result = result + ch;
continue;
}
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
{
if (nextCh == '/' && i + 2 < oldString.size() && oldString[i + 2] == '/')
{
result = result + ch;
continue;
}
if (result.size() > 0)
{
var lc = result[result.size() - 1];
var needSpace = false;
if ((lc >= 'a' && lc <= 'z') || (lc >= 'A' && lc <= 'Z') ||
(lc >= '0' && lc <= '9') || lc == '_' || lc == '$')
{
if ((nextCh >= 'a' && nextCh <= 'z') || (nextCh >= 'A' && nextCh <= 'Z') ||
(nextCh >= '0' && nextCh <= '9') || nextCh == '_' || nextCh == '$')
{
needSpace = true;
}
}
if (needSpace)
{
result = result + ' ';
}
}
continue;
}
if (ch == '/' && nextCh == '/')
{
result = result + ch;
i = i + 1;
while (i < oldString.size() && oldString[i] != '\n')
{
result = result + oldString[i];
i = i + 1;
}
if (i < oldString.size())
{
result = result + '\n';
}
continue;
}
if (ch == '/' && nextCh == '*')
{
result = result + ch;
i = i + 1;
result = result + oldString[i];
i = i + 1;
while (i < oldString.size())
{
result = result + oldString[i];
if (oldString[i] == '*' && i + 1 < oldString.size() && oldString[i + 1] == '/')
{
i = i + 1;
result = result + oldString[i];
break;
}
i = i + 1;
}
continue;
}
result = result + ch;
}
clip.SetAsciiString(result);
return false;