forked from LZMA-JS/LZMA-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.js
362 lines (305 loc) · 9.99 KB
/
minify.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
var uglify = require("uglify-js"),
fs = require("fs"),
p = require("path"),
zlib = require("zlib"),
files = [
"lzma_worker.js",
"lzma-c.js",
"lzma-d.js",
"lzma.js",
],
minify_props_files = [
"lzma_worker.js",
"lzma-c.js",
"lzma-d.js",
],
params = get_params(),
text_output = "",
stats = {};
function get_params(argv)
{
var i,
params = {};
argv = argv || process.argv;
for (i = process.argv.length - 1; i >= 2; i -= 1) {
params[process.argv[i].replace(/^-+/, "")] = 1;
}
return params;
}
function log(str)
{
text_output += str + "\n";
console.log(str);
}
function filesize(path, cb)
{
return fs.statSync(path).size;
}
function sort_obj(obj)
{
return Object.keys(obj).sort(function sorter(a, b)
{
return obj[b] - obj[a];
});
}
function calculate_minify_value(props)
{
Object.keys(props).forEach(function oneach(prop)
{
props[prop] = prop.length * props[prop];
});
}
function base54_64(arr)
{
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789",
i,
len = arr.length,
new_names = [],
str,
pos,
base;
for (i = 0; i < len; i += 1) {
pos = i;
str = "";
base = 54;
do {
///NOTE: This is not perfect. It skips the first char (a) on the last place when more than one letter.
str += chars[pos % base];
pos = Math.floor(pos / base);
/// After the first character, we can use numbers.
base = 64;
} while (pos > 0);
new_names[i] = str;
}
return new_names;
}
function minify_properties(code)
{
var props = {},
ignore = ["LZMA_WORKER", "on_progress", "on_finish"],
sorted_props,
new_names,
prop_regex = /\.([_$a-zA-Z]*[_$][_$a-zA-Z0-9]*|kFixHashSize|kNumHashDirectBytes|kMinMatchCheck|outBytesProcessed|decoder|encoder|nowPos64|outSize|alive|processedInSize|finished|inBytesProcessed|processedInSize|[Ss]tate|prevByte|Prev1IsChar|Prev2|BackPrev|PosPrev|Models|backRes|properties|processedOutSize|tempPrices|backRes|repLens|Price|Backs[0123]|NumBitLevels|Range|Stream|explicitLength|count|pos|buf|chunker|rep[0-3s]|Code|Low|data|mode|output)/g;
/// We want to replace propeters that have an underscore or a dollar sign.
code.replace(prop_regex, function calc(prop)
{
prop = prop.substr(1); /// Remove the perios.
if (ignore.indexOf(prop) === -1) {
if (!props[prop]) {
props[prop] = 1;
} else {
props[prop] += 1;
}
}
});
calculate_minify_value(props);
sorted_props = sort_obj(props);
new_names = base54_64(sorted_props);
code = code.replace(prop_regex, function calc(prop)
{
var index,
partial_prop = prop.substr(1); /// Remove the perios.
index = sorted_props.indexOf(partial_prop);
if (index > -1) {
return "." + new_names[index];
}
return prop;
});
return code;
}
function split_lzma_worker()
{
var data = fs.readFileSync(p.join(__dirname, "src", "lzma_worker.js"), "utf8"),
c,
d,
notice = "///NOTE: This file was generated by minify.js from lzma_worker.js. Do not modify.\n\n";
/// Delete both only code.
data = data.replace(/\/\*\* xs \*\/[\S\s]*?\/\*\* xe \*\//g, "");
/// Delete decompress code.
c = data.replace(/\/\*\* ds \*\/[\S\s]*?\/\*\* de \*\//g, "");
/// Enable compress only code.
c = c.replace(/\/\/\/ co:/g, "");
/// Delete decompress code.
d = data.replace(/\/\*\* cs \*\/[\S\s]*?\/\*\* ce \*\//g, "");
/// Enable compress only code.
d = d.replace(/\/\/\/ do:/g, "");
fs.writeFileSync(p.join(__dirname, "src", "lzma-c.js"), notice + c);
fs.writeFileSync(p.join(__dirname, "src", "lzma-d.js"), notice + d);
}
function get_kb(bytes)
{
return (bytes / 1024).toFixed(bytes < 1024 ? 2 : 1) + " KB";
}
function pad(str, len)
{
return str.length >= len ? str : new Array(len - str.length + 1).join(" ") + str;
}
function replace_data(str, filename)
{
return str.replace(new RegExp("(" + filename + "\\s*\\|\\s*\\S+\\s*\\|)([^|]+)\\|([^|]+)"), function replace(all, base, min, gzip)
{
var new_min = pad(get_kb(stats[filename].min) + " ", min.length),
new_gzip = pad(get_kb(stats[filename].gzip) + " ", gzip.length);
return base + new_min + "|" + new_gzip;
});
}
function update_readme()
{
var data,
readme_path = p.join(__dirname, "readme.md");
data = fs.readFileSync(readme_path, "utf8");
data = replace_data(data, "lzma_worker.js");
data = replace_data(data, "lzma-c.js");
data = replace_data(data, "lzma-d.js");
fs.writeFileSync(readme_path, data);
}
function staged_files_found(cb)
{
require("child_process").exec("git diff-index --quiet --cached HEAD", function onexec(err)
{
if (err) {
if (err.code === 1) {
return cb(true);
}
throw err;
}
cb(false);
});
}
function commit(cb)
{
var execFile = require("child_process").execFile;
function ret()
{
if (cb) {
cb();
}
}
execFile("git", ["add", "readme.md"], function onexec(err)
{
if (err) {
throw err;
}
execFile("git", ["add", "src"], function onexec(err)
{
if (err) {
throw err;
}
staged_files_found(function oncheck(found)
{
if (!found) {
console.log("No files need updating.");
return ret();
}
execFile("git", ["commit", "-m", "Minified:\n\n" + text_output.trim()], function onexec(err)
{
if (err) {
throw err;
}
ret();
});
});
});
});
}
function minify()
{
split_lzma_worker();
(function loop(i)
{
var file,
full_path,
min_path,
result,
orig_size,
min_size,
gzmin_size,
ext;
if (i < files.length) {
file = files[i];
ext = p.extname(file);
full_path = p.join(__dirname, "src", file);
min_path = p.join(__dirname, "src", p.basename(file, ext) + "-min" + ext);
orig_size = filesize(full_path);
log(file);
stats[file] = {};
result = uglify.minify(full_path, {
mangle: {
sort: false, /// As FALSE, the plain JS is bigger, but gzipped is smaller!
toplevel: true,
},
compress: {
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
loops: true,
if_return: true,
join_vars: true,
pure_getters: true,
cascade: true,
join_vars: true,
evaluate: true,
comparisons: true,
properties: true,
negate_iife: true,
keep_fargs: false,
hoist_vars: false, /// As FALSE, the plain JS is bigger, but gzipped is smaller!
hoist_funs: true, /// As TRUE, the plain JS is bigger, but gzipped is smaller!
warnings: true,
unsafe: true,
},
output: {
comments: /^!|@preserve|@license|@cc_on/i,
},
});
if (minify_props_files.indexOf(file) > -1) {
result.code = minify_properties(result.code);
}
fs.writeFileSync(min_path, result.code);
min_size = filesize(min_path);
stats[file].min = min_size;
log("Original size: " + orig_size + " bytes (" + get_kb(orig_size) + ")");
log("Minified size: " + min_size + " bytes (" + get_kb(min_size) + ")");
log("Compression: " + (orig_size / min_size).toFixed(4) + " x smaller");
zlib.gzip(result.code, function(err, buffer) {
if (err) {
throw err;
}
gzmin_size = buffer.length;
stats[file].gzip = gzmin_size;
///NOTE: We could write the file if we wanted to like this: fs.writeFileSync(p.join(__dirname, "src", p.basename(file, ext) + "-min" + ext + ".gz"), buffer);
log("Gzipped size: " + gzmin_size + " bytes (" + get_kb(gzmin_size) + ")");
log("Gzipped ratio: " + (orig_size / gzmin_size).toFixed(4) + " x smaller");
log("");
loop(i + 1);
});
} else if (params.save) {
console.log("Updating Readme");
update_readme();
console.log("Committing");
commit();
}
}(0));
}
if (params.help) {
console.log("");
console.log("Usage: node minify.js [FLAGS]");
console.log("");
console.log(" --save Update readme.md and commit.");
console.log("");
process.exit();
}
if (params.save) {
staged_files_found(function oncheck(found)
{
if (found) {
console.log("Found staged files. Commit first.");
process.exit(1);
}
minify();
});
} else {
minify();
}