-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
dependencies.js
421 lines (405 loc) · 12.9 KB
/
dependencies.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// @ts-check
"use strict";
/**
* @typedef { {
* argv: string[];
* env: {[key: string]: string | undefined};
* chdir (cwd: string): void;
* cwd (): string;
* exit (err?: number): void;
* } } ProcessType
* @typedef { {
* [ind: number]: number; length: number;
* toString (encoding?: string, start?: number, end?: number): string;
* } } Buffer
* @typedef { {
* size: number;
* mtime: Date; atime: Date;
* isDirectory (): boolean;
* } } FileStat
* @typedef { {
* openSync (path: string, method: "a" | "r" | "w"): number;
* fstatSync (fd: number): FileStat;
* futimesSync (fd: number, atime: number, mtime: number): void;
* close (fd: number): void;
* closeSync (fd: number): void;
* existsSync (path: string): boolean;
* statSync (path: string): FileStat;
* mkdirSync (path: string, options?: {recursive?: boolean}): void;
* readFileSync (path: string): Buffer;
* writeFile (path: string, data: string | Buffer, callback?: (err?: Error) => void): void;
* writeFileSync (path: string, data: string | Buffer): void;
* createReadStream (path: string): any;
* } } FileSystem
*
* @typedef { {
* compress: any; output: any; mangle: any; ecma?: 5 | 2015 | 2016 | 2017 | 2018;
* } } TerserOptions
*/
/** @type {FileSystem} */
// @ts-ignore
var fs = require("fs");
var fsPath = require("path");
var projectRoot = fsPath.dirname(fsPath.dirname(__filename)).replace(/\\/g, "/");
/**
* Read file to string and remember info like BOM (support utf-8 / utf16le)
* @param {string} fileName - file path
* @param { { bom?: "\uFFFE" | "\uFEFF" | "" } | null } [info] - object to store extra info
* @returns {string} file text content
*/
function readFile(fileName, info) {
info == null && (info = {});
var buffer = fs.readFileSync(fileName);
var len = buffer.length;
// console.log("[DEBUG] read %o (%o bytes)", fileName, len);
if (len >= 2 && buffer[0] === 0xFE && buffer[1] === 0xFF) {
// Big endian UTF-16 byte order mark detected. Since big endian is not supported by node.js,
// flip all byte pairs and treat as little endian.
len &= ~1;
for (var i = 0; i < len; i += 2) {
var temp = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = temp;
}
info.bom = "\uFFFE";
return buffer.toString("utf16le", 2);
}
if (len >= 2 && buffer[0] === 0xFF && buffer[1] === 0xFE) {
// Little endian UTF-16 byte order mark detected
info.bom = "\uFEFF";
return buffer.toString("utf16le", 2);
}
if (len >= 3 && buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
// UTF-8 byte order mark detected
info.bom = "\uFEFF";
return buffer.toString("utf8", 3);
}
info.bom = "";
// Default is UTF-8 with no byte order mark
return buffer.toString("utf8");
}
/**
* @callback ReadJson
* @param {string} fileName - file path
* @param {boolean} [throwError]
* @returns {any} parsed JSON object
*/
/** @type {ReadJson} */
var _readJSON;
/**
* Read json file to any object or throw error
* @type {ReadJson}
* @param {boolean} [throwError] - throw on error or just log it
*/
function readJSON(fileName, throwError) {
if (!_readJSON) {
_makeJSONReader();
}
return _readJSON(fileName, throwError);
}
function _makeJSONReader() {
var stringOrComment = /"(?:\\[\\\"]|[^"])*"|'(?:\\[\\\']|[^'])*'|\/\/[^\r\n]*|\/\*[^]*?\*\//g
, notLF = /[^\r\n]+/g, notWhiteSpace = /\S/;
/** @type { {[path: string]: string} } */
var cached = {};
/** @param {string} str */
function spaceN(str) {
return ' '.repeat(str.length);
}
/** @param {string} str */
function onReplace(str) {
switch (str[0]) {
case '/': case '#':
if (str.slice(0, 2) === "/*") {
// replace comments with whitespace to preserve original character positions
return str.replace(notLF, spaceN);
}
return spaceN(str);
case '"': case "'": // falls through
default:
return str;
}
}
/** @type {ReadJson} */
function readJSON1(fileName, throwError) {
fileName = fileName.replace(/\\/g, "/");
/** @type string | undefined */
var text = cached[fileName];
if (text == null) {
text = readFile(fileName);
text = text.replace(stringOrComment, onReplace);
cached[fileName] = text;
}
try {
return notWhiteSpace.test(text) ? JSON.parse(text) : {};
} catch (e) {
if (throwError === true) {
throw e;
}
var err = "Failed to parse file '" + fileName + "': " + e + ".";
console.warn("%s", err);
return {};
}
}
_readJSON = readJSON1;
}
var _uglifyjsConfig = null, _configWarningLogged = false;
/**
* Load configuration of UglifyJS or terser
* @param {string} path - file path
* @param {boolean} [reload] - force to reload or return the cache if possible
* @returns {TerserOptions} parsed configuration object
*/
function loadUglifyConfig(path, reload) {
var a = _uglifyjsConfig;
if (a == null || reload) {
a = readJSON(path);
if (!reload) {
_uglifyjsConfig = a;
}
var o = a.output || (a.output = {});
var c = a.compress || (a.compress = {}); // gd = c.global_defs || (c.global_defs = {});
function ToRegExp(literal) {
var re = literal.match(/^\/(.*)\/([a-z]*)$/);
return re ? new RegExp(re[1], re[2]) : literal;
}
if (typeof c.keep_fnames === "string") {
c.keep_fnames = ToRegExp(c.keep_fnames);
}
var m = a.mangle, p = m && m.properties;
if (p && typeof p.regex === "string") {
p.regex = ToRegExp(p.regex);
}
if (m && typeof m.keep_fnames === "string") {
m.keep_fnames = ToRegExp(m.keep_fnames);
}
else if (m && !m.keep_fnames) {
m.keep_fnames = c.keep_fnames;
}
var comments = o.comments;
if (comments && typeof comments === "string") {
o.comments = ToRegExp(comments);
}
var ver = "", terser = null;
try {
// @ts-ignore
ver = require("terser/package").version;
} catch (e) {
console.log("Can not read the version of terser.");
}
try {
// @ts-ignore
terser = require("terser");
} catch (e) {
console.log("Can not read the module of terser.");
}
if (ver) {
if (m) {
if (p.undeclared == null) {
p.undeclared = true;
}
}
}
// @ts-ignore
if (terser && terser.default_options) {
// @ts-ignore
var allowed = terser.default_options();
if (allowed) {
var allowedKeys = new Set(Object.keys(allowed.compress)), skipped = [];
for (var key1 in c) {
if (!allowedKeys.has(key1)) {
skipped.push(key1);
delete c[key1];
}
}
if (skipped.length > 0 && !_configWarningLogged) {
_configWarningLogged = true;
require("fancy-log")(`Skip these terser options: ${skipped.join(", ")}`);
}
}
}
}
return a;
}
/**
* Touch a file if it's older than `fileToCompareTime`
* @param {string} targetPath - target file to check and touch
* @param {string} [fileToCompareTime] - source file to compare its mtime
* @param {boolean} [virtual] - whether to skip real actions
* @returns {boolean | null} whether it's needed to touch or not
*/
function touchFileIfNeeded(targetPath, fileToCompareTime, virtual) {
if (fileToCompareTime === targetPath) { fileToCompareTime = null; }
if (!fs.existsSync(targetPath) || fileToCompareTime && !fs.existsSync(fileToCompareTime)) {
return null;
}
var fd = fs.openSync(targetPath, "a"), fd2 = null;
try {
var stat = fs.fstatSync(fd);
if (stat.mtime != null && fileToCompareTime) {
var srcMtime = fs.fstatSync(fd2 = fs.openSync(fileToCompareTime, "r")).mtime;
if (srcMtime != null && srcMtime < stat.mtime) {
return false;
}
}
virtual ||
// @ts-ignore
fs.futimesSync(fd, parseInt(stat.atime.getTime() / 1000, 10), parseInt(Date.now() / 1000, 10));
} finally {
fs.closeSync(fd);
if (fd2 != null) {
fs.closeSync(fd2);
}
}
return true;
}
/**
* Compare modification time of two files
* @param {string} src - source file
* @param {string} dest - target file
* @returns {boolean} whether src is older than dest or not
*/
function compareFileTime(src, dest) {
return touchFileIfNeeded(dest, src, true) === false;
}
/**
* Copy members of `a` into `b`
* @template T
* @param {T} b - dest object
* @param {Partial<T>} a - source object
* @return {T} b
*/
function extendIf(b, a) {
Object.setPrototypeOf(a, null);
for (var i in a) {
(i in b) || (b[i] = a[i]);
}
return b;
}
/**
* Get git commit id (7-character version) or null
* @return {string | null}
*/
function getGitCommit() {
try {
var branch = readFile(".git/HEAD");
branch = branch && branch.replace("ref:", "").trim();
if (branch) {
var commit = readFile(".git/" + branch);
return commit ? commit.trim().slice(0, 7) : null;
}
} catch (e) {}
return null;
}
/**
*
* @param {string} source
* @param {boolean} locally
* @param {{ (...args: any[]): any; error(message: string): any; }} [logger]
*/
function patchExtendClick(source, locally, logger) {
logger && logger('Patch the extend_click module');
source = source.replace(/\b(addEventListener|toString) ?: ?function ?\w*/g, "$1");
var match = /\/: \?function \\w\+\/g, ?(""|'')/.exec(source);
if (match) {
var start = Math.max(0, match.index - 128), end = match.index;
var prefix = source.slice(0, start), suffix = source.slice(end);
source = source.slice(start, end).replace(/>= ?45/, "< 45").replace(/45 ?<=/, "45 >");
suffix = '/\\b(addEventListener|toString)\\(/g, "$1:function $1("' + suffix.slice(match[0].length);
source = prefix + source + suffix;
}
match = /' ?\+ ?\(?function VC ?\(/.exec(source);
if (match) {
var start = match.index;
var end1 = source.indexOf('}).toString()', start) + 1 || source.indexOf('}.toString()', start) + 1;
var end2 = source.indexOf('")();"', end1 + 2) + 1 || source.indexOf("')();'", end1 + 2) + 1;
if (end2 <= 0) {
throw new Error('Can not find the end ".toString() + \')();\'" around the injected function.');
}
var prefix = source.slice(0, start), suffix = source.slice(end2 + ")();'".length);
source = source.slice(start + match[0].length, end1).replace(/ \/\/[^\n]*?$/g, "").replace(/'/g, '"');
source = source.replace(/\\/g, "\\\\");
if (locally) {
source = source.replace(/([\r\n]) {4}/g, "$1").replace(/\r\n?|\n/g, "\\n\\\n");
} else {
source = source.replace(/[\r\n]\s*/g, "");
}
source = "function(" + source;
source = prefix + source + ")();'" + suffix;
} else if (! (/= ?'"use strict";\(function\b/).test(source)) {
(logger || console).error("Error: can not wrap extend_click scripts!!!");
}
return source;
}
/**
* @argument {string} filePath
* @argument {{ (...args: any[]): any; error(message: string): any; }} logger
* @returns {boolean} whether successful or not
*/
function logFileSize(filePath, logger) {
try {
var fd = fs.openSync(filePath, "r"),
size = fs.fstatSync(fd).size;
fs.close(fd);
logger("%o: %o bytes = %o KB", filePath, size, ((size / 1024 * 100 + 0.5) | 0) / 100);
return true;
} catch (e) {
logger.error('fail to read "' + filePath + '": ' + e);
return false;
}
}
/**
* @param {string} path
* @param {string} data
* @return {string}
*/
function addMetaData(path, data) {
const isAMDModule = data.startsWith("define") || data.startsWith("(factory")
|| data.startsWith("(function(factory)") || data.startsWith("(function (factory)");
if (!isAMDModule) { return data; }
path = path.replace(/\\/g, "/").replace(projectRoot, "").replace(/^\//, "")
var banner = "var __filename = " + JSON.stringify(path) + ";\n";
return banner + data;
}
/**
* @param { string } code
* @returns { string }
*/
function inlineAllSetters (code) {
const allNames = code.match(/\bset\$\w+\b/g);
for (let i = 0; i < allNames.length; i++) {
const name = allNames[i].slice(4);
if (!new RegExp("\\b" + name + " ?=").test(code)) {
throw new Error('Can not find symbol "' + name + '"');
}
}
return code.replace(/\bset\$(\w+)\(([^\n]+)/g, function (fullStr, name, data, ind) {
var parenthesis = 1;
if (code.slice(ind - 16, ind).trim().endsWith("function")) {
return fullStr;
}
for (var i = 0; i < data.length; i++) {
if (data[i] === "(") { parenthesis++; }
else if (data[i] === ")") {
parenthesis--;
if (parenthesis === 0) {
break;
}
}
}
return "(" + name + " = " + data;
});
}
module.exports = {
readFile: readFile,
readJSON: readJSON,
loadUglifyConfig: loadUglifyConfig,
touchFileIfNeeded: touchFileIfNeeded,
compareFileTime: compareFileTime,
extendIf: extendIf,
getGitCommit: getGitCommit,
patchExtendClick: patchExtendClick,
addMetaData: addMetaData,
logFileSize: logFileSize,
inlineAllSetters: inlineAllSetters,
};