Closed atjn closed 1 year ago
Please see https://github.com/terser/html-minifier-terser/issues/34#issuecomment-1073016429 and the other comments there.
I see, thank you! Although I will note that this is a relatively bad minification method, since it still keeps a lot of the whitespace. It might even add whitespace to some tags. Here is the above script after being minified:
<script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "MobileApplication", "name": "Fancy App", "description": "Does cool stuff", "applicationCategory": "Entertainment" } </script>
Although I will note that this is a relatively bad minification method, since it still keeps a lot of the whitespace. It might even add whitespace to some tags. Here is the above script after being minified:
It may depend on your minifier settings.
It may depend on your minifier settings.
I gave it a shot, but I can't find any setting that removes the extra whitespace. The example you posted also doesn't remove most of the extra whitespace.
It seems that disabling conservativeCollapse
removes the very first and very last whitespace character, as you also show in your example, but that is only a small amount of the whitespace removed.
Hi there and thanks for this very cool tool :)
It is relatively common for a site to add some form of
json-ld
metadata for search engines or other web crawlers to use. Here is a basic example for a PWA:Currently,
html-minifier-terser
ignores these scripts, which means whitespace is not removed. I would suggest adding a rule calledminifyJSON
that takes any script matching type/^application\/(?:\S*\+)?json$/ui
, and then removes whitespace withJSON.parse()
followed byJSON.stringify()
.If you want to be more advanced, there are also
json-ld
parsers which can theoretically minify the data even more, although I doubt the savings will be very large. For me personally, only removing the whitespace is fine :)