kangax / html-minifier

Javascript-based HTML compressor/minifier (with Node.js support)
http://kangax.github.io/html-minifier/
MIT License
4.92k stars 568 forks source link

Django template handling #1118

Closed MaartenUreel closed 2 years ago

MaartenUreel commented 2 years ago

Hello,

would you have any guidance on handling Django (similar to Jinja) templates?

I have currently this:

html-minifier --collapse-whitespace --remove-comments --remove-tag-whitespace --minify-css --minify-js true --ignore-custom-fragments '["{{.*}}"]' -o "${HTML_FILE}" "$HTML_FILE";`

But it fails to parse, for instance on this:

==> docker.backend: Parse Error: <span class="flag-icon flag-icon-      k88lggklew13k88lggklew  <small class="text-muted">(&euro;       k88lggklew14k88lggklew  / min)</small>

Actually that specific block of HTML looks like this:

                  <li>
                    <span class="flag-icon flag-icon-{{ d.did | did_country_code }}"></span> {{ d.did.as_national }}
                    <small class="text-muted">(&euro; {{ d.incoming_rate }} / min)</small>
                  </li>

So I think my regex is too broad.

Essentially for Django templates you want to ignore:

I'm also not entirely sure, I would have to get further with testing to see if everything works properly after that. But maybe someone has experience with this already and can provide a working command :)

MaartenUreel commented 2 years ago

I found out this works good:

html-minifier --collapse-whitespace --remove-comments --remove-tag-whitespace --minify-css --minify-js true --ignore-custom-fragments '[/{{[\s\S]*?}}/, /{%[\s\S]*?%}/]'  -o "${HTML_FILE}" "$HTML_FILE";

For anyone looking for a similar solution, we are running this command while building our Docker image:

for HTML_FILE in `find /opt/our-application -type f -name "*.html"`
do
    html-minifier --collapse-whitespace --remove-comments --remove-tag-whitespace --minify-css --minify-js true --ignore-custom-fragments '[/{{[\s\S]*?}}/, /{%[\s\S]*?%}/]'  -o "${HTML_FILE}" "$HTML_FILE";
done

This way we minify all HTML files once, reducing page size but keeping our template logic intact.