tedious / JShrink

Javascript Minifier built in PHP
http://www.tedivm.com
BSD 3-Clause "New" or "Revised" License
749 stars 152 forks source link

Result larger than input? #128

Closed splitbrain closed 1 year ago

splitbrain commented 1 year ago

I am currently evaluating replacing the JavaScript compression in DokuWiki with JShrink.

Our current implementation is a port of the jsstrip python library. The source code is here: https://github.com/dokuwiki/dokuwiki/blob/master/lib/exe/js.php#L339

However something is not working as expected. The result of running JShrink somehow increases the size of the resulting JavaScript?

I attached some files to this issue: files.zip

Something is going wrong here but I'm not sure what. On a first glance, I notice that JShrink outputs a lot of newlines which I would not expect in a JavaScript compressor. But I'm not sure that this should make up an actual increase in the resulting output!?

tedivm commented 1 year ago

When I ran your original file through I got a file with 187K bytes.

Looking at your file, it looks like you echod the jshrink output to the file twice. If you pick a function from the original file (say function split) and search for it in your output you'll see it get defined twice. From there you can pull any individual line and see it's there twice. That's why you're seeing an increase - your file it literally twice as big as it should be because it contains the entire script twice.

I've attached an output without the duplication error so you can compare for yourself.

jshrink_output.js.zip

tedivm commented 1 year ago

Also the reason you see new lines is because I try to minimize the number of changes that end up in the same output. To put it another way- some characters require whitespace before them to get properly tokenized by javascript. Some minifiers replace all of those whitespace characters with spaces or semicolons, but I leave whatever the original character was. There are probably some area where this can be improved though.

splitbrain commented 1 year ago

Huh. You're right. But I don't really understand how the duplication happens. It's as if the minify() function prints and returns the output.

Digging a bit deeper, I found the culprit. It has already been fixed in b367a6ce4ac2a93b557477081a607732e3e58ed3 but I guess you haven't updated the composer version yet?

splitbrain commented 1 year ago

Ah. Now I understand. DokuWiki ist currently pinned to PHP 7.2 level. The last available version with PHP 7 support is 1.6.0 and this fix is only available in 1.6.4

tedivm commented 1 year ago

I just added back php7 support. I had originally removed it to use a function that was only present in php8, but in dealing with a bug that code was removed anyways. PHP7 is back in the test suites, and you should be able to use v1.6.5 now.