Skip to content

Commit

Permalink
Minify ES6 String and Long Number
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Nov 20, 2021
1 parent 023e117 commit a747446
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Minify Extension for [Mecha](https://github.com/mecha-cms/mecha)
Release Notes
-------------

### 2.6.0
### 2.6.1

- Refactor.

Expand Down
2 changes: 1 addition & 1 deletion minify/about.page
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Minify
description: Remove white-spaces and unused comments in HTML, CSS and JavaScript files.
author: Taufik Nurrohman
type: Markdown
version: 2.6.0
version: 2.6.1

use:
'.\lot\x\layout': 0
Expand Down
82 changes: 51 additions & 31 deletions minify/engine/plug/minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
\define($n . "\\token_js_comment_2", '//[^\n]*');
\define($n . "\\token_js_name", '[a-z$_][a-z$_\d]*');
\define($n . "\\token_js_pattern", '/(?:(?![*+?])(?:[^\n\[/\\\]|\\\.|\[(?:[^\n\]\\\]|\\\.)*\])+)/[gimuy]*');
\define($n . "\\token_js_string", '`(?:[^`\\\]|\\\.)*`');

function every(array $tokens, callable $fn = null, string $in = null, string $flag = 'i') {
if ("" === ($in = \trim($in))) {
Expand Down Expand Up @@ -337,6 +338,10 @@ function is_token_js_pattern($token) {
return '/' === $token[0] && '/' === \substr($token, -1);
}

function is_token_js_string($token) {
return is_token_string($token) || '`' === $token[0] && '`' === \substr($token, -1);
}

function minify_number($token) {
if ('-' === $token[0] && \strlen($token) > 1) {
$number = minify_number(\substr($token, 1));
Expand All @@ -356,6 +361,17 @@ function minify_number($token) {
return $token;
}

// <https://stackoverflow.com/a/21417604/1163000>
function minify_number_long($token) {
if ('0' === $token) {
return $token;
}
$x = '-' === $token[0] ? '-' : "";
$token = (float) ($x ? \substr($token, 1) : $token);
$exp = \floor(\log($token, 10));
return $x . \sprintf('%.2fE%+03d', $token / \pow(10, $exp), $exp);
}

function minify_css_color($token) {
if (is_token_css_hex($token)) {
$token = \strtolower(\preg_replace('/^#([a-f\d])\1([a-f\d])\2([a-f\d])\3(?:([a-f\d])\4)?$/i', '#$1$2$3$4', $token));
Expand Down Expand Up @@ -532,8 +548,7 @@ function minify_css(string $in, int $comment = 2, int $quote = 2) {
false !== \strpos($token, '@preserve')
) {
$token = \ltrim(\substr($token, 2, -2), '!*');
$token = \preg_replace('/@preserve\s*/', "", $token);
return '/*' . \trim($token) . '*/';
return '/*' . \trim(\strtr($token, ['@preserve' => ""])) . '*/';
}
}
return ""; // Remove!
Expand Down Expand Up @@ -787,8 +802,7 @@ function minify_html(string $in, int $comment = 2, int $quote = 1) {
false !== \strpos($token, '@preserve')
) {
$token = \substr($token, 4, -3);
$token = \preg_replace('/@preserve\s*/', "", $token);
return '<!--' . \trim($token) . '-->';
return '<!--' . \trim(\strtr($token, ['@preserve' => ""])) . '-->';
}
}
return ""; // Remove!
Expand Down Expand Up @@ -873,17 +887,18 @@ function minify_js(string $in, int $comment = 2, int $quote = 2) {
}
$out = every([
token_js_comment,
token_js_comment_2,
token_string,
token_js_pattern,
token_js_comment_2
token_js_string,
token_js_pattern
], static function($token) use($comment, $quote) {
if (1 === $comment) {
if (0 === \strpos($token, '//')) {
return '/*' . \trim(\substr($token, -2)) . '*/';
}
return $token;
}
if (is_token_js_comment($token)) {
if (1 === $comment) {
if (0 === \strpos($token, '//')) {
return '/*' . \trim(\substr($token, 2)) . '*/';
}
return $token;
}
if (2 === $comment) {
if (
// Detect special comment(s) from the third character.
Expand All @@ -907,22 +922,30 @@ function minify_js(string $in, int $comment = 2, int $quote = 2) {
$out = every([
token_js_comment,
token_js_comment_2,
token_js_pattern,
token_string,
token_js_string,
token_js_pattern,
token_boolean,
token_number,
'\b(?:case|return|typeof|void)\s*(?=[-.\d])',
'[%&()*+,\-/:;<=>?\[\]^{|}]'
], static function($token, $chop) {
if (is_token_js_comment($token) || is_token_js_pattern($token) || is_token_string($token)) {
], static function($token, $chop) use($comment, $quote) {
if (is_token_js_comment($token) || is_token_js_pattern($token) || is_token_js_string($token)) {
if ('`' === $token[0] && false !== \strpos($token, '${')) {
return \preg_replace_callback('/\$\{\s*((?:' . token_string . '|[^{}]|(?R))*)\s*\}/', static function($m) use($comment, $quote) {
return '${' . minify_js($m[1], $comment, $quote) . '}';
}, $token);
}
return $token;
}
$token = \preg_replace('/\s+/', ' ', $token);
if (is_token_boolean($token)) {
return ['false' => '!1', 'true' => '!0'][$token] ?? $token;
}
if (is_token_number($token)) {
return minify_number($token);
$token = minify_number($token);
$e = minify_number_long($token);
return \strlen($e) < \strlen($token) ? \strtolower($e) : $token;
}
if (
'case ' === $chop ||
Expand All @@ -938,13 +961,18 @@ function minify_js(string $in, int $comment = 2, int $quote = 2) {
token_js_comment,
token_js_comment_2,
token_js_pattern,
'(?:(?:' . token_js_name . ')|[\]])(?:\[(?:"' . token_js_name . '"|\'' . token_js_name . '\')\])+',
'(?<=[\s{,])(?:"' . token_js_name . '"|\'' . token_js_name . '\')\s*:',
token_string
'(?:(?:' . token_js_name . ')|[\]])(?:\[(?:"' . token_js_name . '"|\'' . token_js_name . '\'|`' . token_js_name . '`)\])+',
'(?<=[\s{,])(?:"' . token_js_name . '"|\'' . token_js_name . '\'|`' . token_js_name . '`)\s*:',
token_string,
token_js_string,
'\(\s*' . token_js_name . '\s*\)\s*=>'
], static function($token, $chop) use($quote) {
if (is_token_js_comment($token) || is_token_js_pattern($token) || is_token_string($token)) {
if (is_token_js_comment($token) || is_token_js_pattern($token) || is_token_js_string($token)) {
return $token;
}
if ('=>' === \substr($token, -2)) {
return \trim(\substr(\trim(\substr($token, 1, -2)), 0, -1)) . '=>';
}
if (1 !== $quote) {
if (('"' === $token[0] || "'" === $token[0]) && ':' === \substr($token, -1)) {
return \trim(\trim(\substr($token, 0, -1)), '\'"') . ':';
Expand All @@ -959,6 +987,7 @@ function minify_js(string $in, int $comment = 2, int $quote = 2) {
}
}
return \strtr($token, [
'window.' => "",
',]' => ']',
',}' => '}',
';}' => '}'
Expand Down Expand Up @@ -1032,15 +1061,7 @@ function minify_php(string $in, int $comment = 2, int $quote = 1) {
$skip = false;
} else {
if (\T_OPEN_TAG === $id) {
if (
false !== \strpos($token, ' ') ||
false !== \strpos($token, "\n") ||
false !== \strpos($token, "\t") ||
false !== \strpos($token, "\r")
) {
$token = \rtrim($token);
}
$out .= $token . ' ';
$out .= \rtrim($token) . ' ';
$start = \T_OPEN_TAG;
$skip = true;
} else if (\T_OPEN_TAG_WITH_ECHO === $id) {
Expand Down Expand Up @@ -1122,8 +1143,7 @@ function minify_php(string $in, int $comment = 2, int $quote = 1) {
)
) {
$token = \ltrim(\substr($token, 2, -2), '!*');
$token = \preg_replace('/@preserve\s*/', "", $token);
$out .= '/*' . \trim($token) . '*/';
$out .= '/*' . \trim(\strtr($token, ['@preserve' => ""])) . '*/';
}
$skip = true;
} else {
Expand Down
26 changes: 25 additions & 1 deletion minify/test/js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function foo ( bar ) {
return { baz: 'qux' };
}

window.setTimeout(() => {
window.alert(window.location.href);
}, 100);

return [ 1 , 2 , 3 ];

return { foo : 1 , bar : 2 };
Expand Down Expand Up @@ -51,6 +55,24 @@ return -.5;
test = '//example.com';
test = 'http://example.com';

test = `
aaa
aaa
aaa
aaa
aaa
aaa
aaa
aaa
aaa
`;

test = ( foo , bar ) => foo + bar;

test = ( foo ) => foo + 4;

test = `aaa ${ window.location.hostname } aaa ${ 0.5 + 1.50 + (a / b) }`;

for ( let i = 0, j = data.length; i < j; ++i ) {
console.log( data[ i ] );
}
Expand Down Expand Up @@ -85,4 +107,6 @@ test = {
test = {
"foo-bar" : "baz",
"qux": true,
};
};

test = [123456789, -123456789, 0.123456789, -0.123456789];

0 comments on commit a747446

Please sign in to comment.