How to use updated JavaScript minification?

Hugo 0.76 introduced an update to the github.com/tdewolff/minify/v2 dependency that now has full support for JavaScript minification (that was added in v2.9.0).

But how do I use that updated JavaScript minifier?

hugo --minify on version 0.75.1 turns a piece of JavaScript into:

var _self=(typeof window!==‘undefined’)?window:((typeof WorkerGlobalScope!==‘undefined’&&self instanceof WorkerGlobalScope)?self:{});var Prism=(function(_self){var lang=/\blang(?:uage)?-([\w-]+)\b/i;var uniqueId=0;var _={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function encode(tokens){if(tokens instanceof Token){return new Token(tokens.type,encode(tokens.content),tokens.alias);}else if(Array.isArray(tokens)){return tokens.map(encode);}else{return tokens.replace(/&/g,‘&’).replace(/</g,‘<’).replace(/\u00a0/g,’ ‘);}},type:function(o){return Object.prototype.toString.call(o).slice(8,-1);},objId:function(obj){if(!obj[’__id’]){Object.defineProperty(obj,‘__id’,{value:++uniqueId});}

hugo --minify on version 0.76.5 turns that same JavaScript into:

var _self=(typeof window!==‘undefined’)?window:((typeof WorkerGlobalScope!==‘undefined’&&self instanceof WorkerGlobalScope)?self:{});var Prism=(function(_self){var lang=/\blang(?:uage)?-([\w-]+)\b/i;var uniqueId=0;var _={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function encode(tokens){if(tokens instanceof Token){return new Token(tokens.type,encode(tokens.content),tokens.alias);}else if(Array.isArray(tokens)){return tokens.map(encode);}else{return tokens.replace(/&/g,‘&’).replace(/</g,‘<’).replace(/\u00a0/g,’ ‘);}},type:function(o){return Object.prototype.toString.call(o).slice(8,-1);},objId:function(obj){if(!obj[’__id’]){Object.defineProperty(obj,‘__id’,{value:++uniqueId});}

This output is completely identical, with no JavaScript minification benefits.

But if I copy/paste my JavaScript into Taco’s online minifier (which is what Hugo uses behind the scenes), I get:

var _self=typeof window!=‘undefined’?window:typeof WorkerGlobalScope!=‘undefined’&&self instanceof WorkerGlobalScope?self:{},Prism=function(b){var i=/\blang(?:uage)?-([\w-]+)\b/i,l=0,a={manual:b.Prism&&b.Prism.manual,disableWorkerMessageHandler:b.Prism&&b.Prism.disableWorkerMessageHandler,util:{encode:function b(a){return a instanceof c?new c(a.type,b(a.content),a.alias):Array.isArray(a)?a.map(b):a.replace(/&/g,‘&’).replace(/</g,‘<’).replace(/\u00a0/g,’ ‘)},type:function(a){return Object.prototype.toString.call(a).slice(8,-1)},objId:function(a){return a.__id||Object.defineProperty(a,’__id’,{value:++l}),a.__id},

This time my JavaScript is aggressively minified with all kinds of tricks to make the code smaller. How do I get this behaviour in Hugo?

(I have no configuration options set related to minification. Is there perhaps one I need to enable? I couldn’t find it in the minify config).


> hugo env
Hugo Static Site Generator v0.76.5/extended windows/amd64 BuildDate: unknown
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.15.1"

The updated minification was introduced in 0.76.0, but, was reverted back to the previous one in 0.76.3 because it was breaking a few websites. Read here: 0.76.0 breaks lazy loading on my website

If you want to use it, you can use 0.76.0, 0.76.1 or 0.76.2.

1 Like

Thanks for your reply. That explains a lot.

Do you know what the path forward is? Because improved JavaScript minification is more than welcome in Hugo.

I see that the bug you reported couldn’t be reproduced by the developers, but I see that there are also 4 JavaScript minifier bugs fixed in the meantime.

Unfortunately, I’m not the one who can answer that.

I had felt so too, read here: Is obfuscation of JavaScript possible in Hugo?

It was reproduced as you can see in the last answer of the previous link. If it was an error on my end, it probably wouldn’t have been reverted.

It was not reproduced by Taco, the minifier developer, and that is what’s needed to fix minifier issues unfortunately.

I wasn’t implying that there was a mistake on your end. I simply said that some JavaScript minifications errors were fixed since then, so the minifier might work better now.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.