-
Notifications
You must be signed in to change notification settings - Fork 263
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 HTML & CSS, optimize PNGs #449
Conversation
Really happy to see a PR for this. Thanks! Will review this soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this! Great work so far, just a few small items.
This should also minify the JS files created by wasm-bindgen. One approach to do that is by using SWC |
@hamza1311 yea, I am tempted to say that we might want to tread carefully before we pull in SWC. I think it will be awesome to do so, however at that point, we will have everything needed to do full JS/TS transpilation. We have to be fairly measured about whether we really want to push Trunk into that space. I am fairly biased towards allowing folks to use hooks or something similar for integration with other tools. As soon as we cross that threshold, we take on the full yoke of the JS ecosystem. |
@hamza1311 @thedodd Despite its name, the We definitely should draw a line somewhere though, as thedodd mentioned. For example, for projects that build the whole application in Rust, it's great to have trunk minify everything, as you don't have to tap into any extra tools from the JS ecosystem. But for projects that only use wasm partially or mix and match with JS, it's in my opinion best to leave further bundling to another tool. |
Unfortunately, the JS minifier in that crate can't correctly parse the wasm-bindgen output. Incidentally, it's the one I intended to use, so I already created an issue upstream in the parser.
|
Oh, that's too bad. Can you link to the issue you created here? |
This one, I believe: wilsonzlin/ecma-rs#3 The |
Author of minify-html/minify-js added a fix for parsing import.meta so I'll see about adding JS minification to this PR later today. I'm thinking we could minify wasm-bindgen output by default and maybe make minifying other scripts optional either through an extra command-line argument to Trunk, or by checking some additional attribute on the script elements so people can choose case-by-case when to use JS minification? Latter option would be great if people run into issues with minification on one of their scripts but not others, I reckon. |
Just checking in to see if this PR is again ready for review? Maybe a rebase is a good idea too. |
At one point I was having issues with it, some with the crates being used (and some of it user error on my behalf), so I decided to let it rest a bit and let the minify-js crate mature a bit, as it was in fairly active development. I think now is a good time to take a new look at it and test it with new and improved dependencies. I'll try to get around to it in the next few days. |
@Carlrs I would like to see this feature merged as well. If there is anything I can do to help, feel free to ping me. Would love to help any way I can. |
Rebased and fixed the issues I'd had previously (using minify-js in wrong TopLevelMode + not checking for image type for before running oxipng). Let me know how it looks! |
Do you plan to keep the closing tags, as well as minify js/css in the html? (These flags are disabled by default) let mut minify_cfg = Cfg::spec_compliant();
minify_cfg.minify_css = true;
minify_cfg.minify_js = true;
minify_cfg.keep_closing_tags = true; In any case, thanks for this PR. I got the |
src/pipelines/mod.rs
Outdated
.await | ||
.with_context(|| format!("error reading file for copying {:?}", &self.path))?; | ||
|
||
bytes = if minify { | ||
match file_type { | ||
AssetFileType::Css => minify_html::minify(&bytes, &Cfg::spec_compliant()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, Cfg::spec_compliant()
only initializes basic flags, but leaves the rest to default, so the minify_css
field ends up as false
.
Was this intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, it was not intentional
… html files, keep closing tags
Added your suggested fixes and bumped the minify-html crate version just in case. @thedodd could we get a review here sometime? |
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
I cherry-picked this into |
This is now released with |
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
This was brought back to trunk with PR #623 and it should be part of the next release of trunk too. |
This is part of the trunk 0.18.0 release |
Based on issue #7
Saves a few kilobytes per fresh uncached page load, will be more significant if/when we can get JS minification working (I'm hoping to get one of the potential JS minification libraries for Rust working properly, rather than using yet another commandline tool, though I won't rule that out either).
Checklist
site
content with pertinent info (may not always apply).