Skip to content

Commit 533854a

Browse files
committed
Improved JavaScript minificator.
1 parent 38191aa commit 533854a

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

internal.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,11 +1146,12 @@ function minify_javascript(data) {
11461146
var alpha = /[0-9a-z$]/i;
11471147
var white = /\W/;
11481148
var skip = { '$': true, '_': true };
1149+
var newlines = { '\n': 1, '\r': 1 };
11491150
var regexp = false;
1150-
var scope;
1151-
var prev;
1152-
var next;
1153-
var last;
1151+
var scope, prev, next, last;
1152+
var vtmp = false;
1153+
var regvar = /^(\s)*var /;
1154+
var vindex = 0;
11541155

11551156
while (true) {
11561157

@@ -1181,7 +1182,7 @@ function minify_javascript(data) {
11811182
if (c === '/' && next === '/') {
11821183
isCI = true;
11831184
continue;
1184-
} else if (isCI && (c === '\n' || c === '\r')) {
1185+
} else if (isCI && newlines[c]) {
11851186
isCI = false;
11861187
alpha.test(last) && output.push(' ');
11871188
last = '';
@@ -1192,7 +1193,7 @@ function minify_javascript(data) {
11921193
continue;
11931194
}
11941195

1195-
if (c === '\t' || c === '\n' || c === '\r') {
1196+
if (c === '\t' || newlines[c]) {
11961197
if (!last || !alpha.test(last))
11971198
continue;
11981199
output.push(' ');
@@ -1201,8 +1202,11 @@ function minify_javascript(data) {
12011202
}
12021203

12031204
if (!regexp && (c === ' ' && (white.test(prev) || white.test(next)))) {
1204-
if (!skip[prev] && !skip[next])
1205-
continue;
1205+
// if (!skip[prev] && !skip[next])
1206+
if (!skip[prev]) {
1207+
if (!skip[next] || !alpha.test(prev))
1208+
continue;
1209+
}
12061210
}
12071211

12081212
if (regexp) {
@@ -1233,6 +1237,36 @@ function minify_javascript(data) {
12331237
scope = c;
12341238
}
12351239

1240+
// var
1241+
if (!scope && c === 'v' && next === 'a') {
1242+
var v = c + data[index] + data[index + 1] + data[index + 2];
1243+
if (v === 'var ') {
1244+
if (vtmp && output[output.length - 1] === ';') {
1245+
output.pop();
1246+
output.push(',');
1247+
} else
1248+
output.push('var ');
1249+
index += 3;
1250+
vtmp = true;
1251+
continue;
1252+
}
1253+
} else {
1254+
if (vtmp) {
1255+
1256+
1257+
vindex = index + 1;
1258+
1259+
while (true) {
1260+
if (!data[vindex] || !white.test(data[vindex]))
1261+
break;
1262+
vindex++;
1263+
}
1264+
1265+
if (c === '(' || c === ')' || (c === ';' && !regvar.test(data.substring(vindex, vindex + 20))))
1266+
vtmp = false;
1267+
}
1268+
}
1269+
12361270
if ((c === '}' && last === ';') || ((c === '}' || c === ']') && output[output.length - 1] === ' ' && alpha.test(output[output.length - 2])))
12371271
output.pop();
12381272

test/test-javascript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ buffer.push('console.log("OK");');
1212
buffer.push('</script>');
1313

1414

15-
var result1 = '<script type="text/javascript">function skuska(name,value){var arr=[1,2,3,4,5];var obj={Name:"Peter",Age:"28"}}console.log("OK");</script>';
15+
var result1 = '<script type="text/javascript">function skuska(name,value){var arr=[1,2,3,4,5],obj={Name:"Peter",Age:"28"}}console.log("OK");</script>';
1616
assert.ok(javascript.compile_javascript(buffer.join('\n')) === result1, 'javascript');
1717
assert.ok(Buffer.from(javascript.compile_javascript(fs.readFileSync('javascript.js').toString('utf8'))).toString('base64') === 'cmV0dXJuJ1xcJysyO3ZhciBhdHRyaWJ1dGVzPSJcXFsiK2ErIiooIitiKyIpKD86IitjKyIqKFsqXiR8IX5dPz0pIitkKyIqKD86JygoPzpcXFxcLnxbXlxcXFwnXSkqKSd8XCIoKD86XFxcXC58W15cXFxcXCJdKSopXCJ8KCIrZSsiKSl8KSIrZisiKlxcXSI7dmFyIGE9MjAwOw==', 'Problem 1');
1818

0 commit comments

Comments
 (0)