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"