-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathuglify.js
executable file
·293 lines (281 loc) · 8.73 KB
/
uglify.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2011-2014 YY Digital Pty Ltd. All Rights Reserved.
* Please see the LICENSE file included with this distribution for details.
*/
var UglifyJS = require('uglify-js'),
_ = require('underscore'),
config = require('./config'),
path = require('path'),
babel = require('@babel/core'),
env = require('@babel/preset-env');
function functionCall(name, args) {
return new UglifyJS.AST_Call({
expression: new UglifyJS.AST_SymbolRef({ name: name }),
args: args
});
}
function functionCallByNode(node, args) {
return new UglifyJS.AST_Call({
expression: node,
args: args
});
}
function binaryAdd(left, right) {
return new UglifyJS.AST_Binary({
left: left,
operator: '+',
right: right
});
}
function symbol(s) {
return new UglifyJS.AST_Symbol({ name: s });
}
function addAppName(node) {
return binaryAdd(node, symbol('require("/api/TiShadow").currentApp + "/"'));
}
function argsToPath(args) {
var path = args[0];
if (args.length > 1) {
for (var i = 1; i < args.length - 1; ++i) {
path = binaryAdd(path, binaryAdd(args[i], symbol('"/"')));
}
path = binaryAdd(path, _.last(args));
}
return path;
}
function couldBeAsset(name) {
return (
typeof name === 'string' &&
(name.toLowerCase().match('image$') ||
name.toLowerCase().match('icon$') ||
[
'file',
'sound',
'icon',
'url',
'leftButton',
'rightButton',
'images'
].indexOf(name) !== -1)
);
}
function doNotTouch(node) {
return (
node instanceof UglifyJS.AST_Atom || node instanceof UglifyJS.AST_Lambda //Booleans, Nulls, Undefined, etc
); //Functions, etc
}
function toFullPath(p) {
if (typeof p === 'string' && p.match(/^\.{1,2}\//) && current_file) {
var full = path.join(path.dirname(current_file), p);
if (path.sep == '\\') {
full = full.replace(/\\/g, '/');
}
return full.substring(full.indexOf('Resources/') + 10);
}
return p;
}
var is_titaniumified = false;
var convert = new UglifyJS.TreeTransformer(null, function(node) {
//function call replacement
if (node instanceof UglifyJS.AST_SymbolFunarg) {
if (node.name === 'require') {
is_titaniumified = true;
}
}
if (node instanceof UglifyJS.AST_Call) {
// redirect require function
if (
!is_titaniumified &&
((!config.isTicommonjs && node.expression.name === 'require') ||
(config.isTicommonjs &&
(node.expression.name === 'tirequire' ||
node.expression.name === '_require')))
) {
node.expression.name = '__p.require';
node.args[0].value = toFullPath(node.args[0].value);
return node;
}
if (node.expression.start.value === 'console') {
if (typeof node.expression.property === 'string') {
// console.log(...) => __log.log('info', ...)
if (node.expression.property === 'log') {
node.args.unshift(new UglifyJS.AST_String({ value: 'info' }));
}
return functionCall('__log.' + node.expression.end.value, node.args);
} else {
return functionCallByNode(
new UglifyJS.AST_Sub({
expression: new UglifyJS.AST_SymbolRef({ name: '__log' }),
property: node.expression.property
}),
node.args
);
}
}
if (node.expression.start.value === 'L') {
node.expression.name = '__L';
node.name = '__L';
return node;
}
if (
node.expression.start.value.match &&
node.expression.start.value.match('^Ti(tanium)?$')
) {
// redirect include
if (
node.expression.end.value === 'include' &&
node.expression.expression.property === undefined
) {
return functionCall('eval', [
functionCall('__p.fileContent', node.args)
]);
}
//reroute resources directory -- FILESYSTEM
if (
node.expression.end.value === 'getResourcesDirectory' &&
node.expression.expression.property === 'Filesystem'
) {
node.expression.property = 'getApplicationDataDirectory';
return addAppName(node);
}
if (
node.expression.end.value === 'getFile' &&
node.expression.expression.property === 'Filesystem'
) {
node.args = [functionCall('__p.getFile', node.args)];
return node;
}
//control localisation -- LOCALE
if (
node.expression.end.value === 'getString' &&
node.expression.expression.property === 'Locale'
) {
return functionCall('__L', node.args);
}
//control localisation -- UI
if (
node.expression.end.value.match(
'^(createWindow|createTabGroup|createAlertDialog|createOptionDialog)$'
) &&
node.expression.expression.property === 'UI'
) {
return functionCall('__ui.' + node.expression.end.value, node.args);
}
if (
node.expression.end.value === 'createNavigationWindow' &&
node.expression.expression.property === 'iOS'
) {
return functionCall('__ui.createNavigationWindow', node.args);
}
/*if (node.expression.end.value.match("^(createSplitWindow|createPopover)$") &&
node.expression.expression.property === "iPad") {
return functionCall("__ui."+node.expression.end.value, node.args);
}*/
//control global listener -- App
if (
node.expression.end.value.match(
'^(addEventListener|removeEventListener|fireEvent)$'
) &&
['App', 'Gesture', 'Geolocation'].indexOf(
node.expression.expression.property
) > -1
) {
return functionCall(
'__app.' + node.expression.end.value,
[
new UglifyJS.AST_String({
value: node.expression.expression.property
})
].concat(node.args)
);
}
//control localisation -- API
if (node.expression.expression.property === 'API') {
if (typeof node.expression.property === 'string') {
return functionCall('__log.' + node.expression.end.value, node.args);
} else {
return functionCallByNode(
new UglifyJS.AST_Sub({
expression: new UglifyJS.AST_SymbolRef({ name: '__log' }),
property: node.expression.property
}),
node.args
);
}
}
//control database source - Database
if (
node.expression.end.value === 'install' &&
node.expression.expression.property === 'Database'
) {
node.args[0] = functionCall('__p.file', [node.args[0]]);
return node;
}
}
//assets
if (
node.expression.end.value.match('^set') &&
!doNotTouch(node.args) &&
node.args[0] !== undefined &&
couldBeAsset(node.expression.end.value.replace('set', '').toLowerCase())
) {
node.args[0].value = toFullPath(node.args[0].value);
node.args = [functionCall('__p.file', node.args)];
return node;
}
} else if (node instanceof UglifyJS.AST_Assign && !doNotTouch(node.right)) {
if (
node.left.property &&
node.left.property.match &&
node.left.property.match('^(title|text)id$')
) {
node.left.property = node.left.property.replace('id', '');
node.right = functionCall('__L', [node.right]);
return node;
} else if (couldBeAsset(node.left.property)) {
if (node.left.property === 'url' && node.operator === '+=') {
//issue #377
return node;
}
node.right.value = toFullPath(node.right.value);
node.right = functionCall('__p.file', [node.right]);
return node;
}
} else if (
node instanceof UglifyJS.AST_ObjectKeyVal &&
!doNotTouch(node.value)
) {
if (typeof node.key === 'string' && node.key.match('^(title|text)id$')) {
node.key = node.key.replace('id', '');
node.value = functionCall('__L', [node.value]);
return node;
} else if (couldBeAsset(node.key)) {
node.value.value = toFullPath(node.value.value);
node.value = functionCall('__p.file', [node.value]);
return node;
}
} else if (node instanceof UglifyJS.AST_PropAccess) {
if (
node.property === 'resourcesDirectory' &&
node.start.value.match('^Ti(tanium)?$') &&
node.expression.property === 'Filesystem'
) {
node.property = 'applicationDataDirectory';
return addAppName(node);
}
}
});
var current_file;
exports.toAST = function(input, file) {
current_file = file;
is_titaniumified = false;
var ast = UglifyJS.parse(input);
return ast.transform(convert);
};
exports.toString = function(input, file) {
if (config.transpile) {
var presets = [env];
input = babel.transform(input, { presets }).code;
}
return exports.toAST(input, file).print_to_string({ beautify: true });
};