Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify in parallel, compress/mangle JS ES6+. #860

Conversation

n3vu0r
Copy link
Contributor

@n3vu0r n3vu0r commented Mar 4, 2020

Improve JS minification by enabling compression -c and mangling -m. This should save around 175 kB in total for the ath79.ath10k-large profile. I have know idea, is this something?

I used the opportunity to parallelize both JS and CSS minification via xargs, respecting BUILD_THREADS, and to switch from uglify-js to terser:

uglify-es is no longer maintained and uglify-js does not support ES6+.

terser is a fork of uglify-es that mostly retains API and CLI compatibility
with uglify-es and uglify-js@3.

According to the documentation, the options -c -m are safe. Top level (global scope) variable names and function names are not mangled.

With mangling enabled, global variables should be avoided whenever possible as these won't be mangled, assuming they consume more characters than prepending var which will also be grouped for multiple local variables whenever possible.

@lantis1008
Copy link
Contributor

Thanks, can you provide (via Pastebin) a comparison of common.js using both methods please? Would like to understand the difference.

@n3vu0r
Copy link
Contributor Author

n3vu0r commented Mar 4, 2020

uglifyjs common.js -o common.old.js
terser common.js -o common.new.js -c -m

common.js
common.old.js
common.new.js

@lantis1008
Copy link
Contributor

Cool, so the big difference appears to be that variables internal to functions have been obfuscated and minimised right?
Have you tested that this operates as expected? It can't imagine it wouldn't, but the biggest worry is the i18n variables.

@n3vu0r
Copy link
Contributor Author

n3vu0r commented Mar 5, 2020

Cool, so the big difference appears to be that variables internal to functions have been obfuscated and minimised right?

Exactly.

Have you tested that this operates as expected? It can't imagine it wouldn't, but the biggest worry is the i18n variables.

Yes, I've build the image with these modifications. So far, I haven't seen any JS error in the browser's web console. The i18n variables should never be touched as they are global, otherwise it would be a bug of the minifier because we haven't enabled the --toplevel option.

The i18n JS files themselves are not processed by do_js_compress(). That would require modifications to the gargoyle_header_footer.c which expects line breaks after each i18n variable. I haven't found how haserl parses the JS files, might be similar. I wanted to estimate what we can additionally save if we would process these files as well. It would be 69 kB:

# modify rebuild.sh to compress every single `*.js` file,
# then compare only i18n/theme/hook files (-not -path '*/www/js/*.js'):
find package/ -path '*.js' -a -not -path '*/www/js/*.js' -print0 | du --files0-from=- -cb
find compressed_javascript/ -path '*.js' -a -not -path '*/www/js/*.js' -print0 | du --files0-from=- -cb

By doing so, I found JS files with invalid syntax, see #862.

@lantis1008
Copy link
Contributor

Apologies that it took me so long to get back to this. Thanks for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants