From d02f0622b471d6a8ee805a7a8387a3984694a4ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 2 May 2024 10:35:43 +0200 Subject: [PATCH] livereload: Improve the livereload script build and update to v4.0.2 This script has very infrequent updates, but just copy pasting the minified source creates some potential trust issues. This JS will now be pulled from a Git version and both the unminified and minified version gets written to disk. This way it should be easier to reason about changes in the future. To upgrade, change the commit hash and run `mage generate`. Closes #12451 Closes #6290 --- livereload/gen/livereload-hugo-plugin.js | 34 + livereload/gen/main.go | 61 + livereload/livereload.go | 49 +- livereload/livereload.js | 3796 +++++++++++++++++++++- livereload/livereload.min.js | 7 + magefile.go | 3 +- 6 files changed, 3903 insertions(+), 47 deletions(-) create mode 100644 livereload/gen/livereload-hugo-plugin.js create mode 100644 livereload/gen/main.go create mode 100644 livereload/livereload.min.js diff --git a/livereload/gen/livereload-hugo-plugin.js b/livereload/gen/livereload-hugo-plugin.js new file mode 100644 index 000000000..c4c6aa487 --- /dev/null +++ b/livereload/gen/livereload-hugo-plugin.js @@ -0,0 +1,34 @@ +/* +Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal +navigation to another content page. +*/ +function HugoReload() {} + +HugoReload.identifier = 'hugoReloader'; +HugoReload.version = '0.9'; + +HugoReload.prototype.reload = function (path, options) { + var prefix = '__hugo_navigate'; + + if (path.lastIndexOf(prefix, 0) !== 0) { + return false; + } + + path = path.substring(prefix.length); + + var portChanged = options.overrideURL && options.overrideURL != window.location.port; + + if (!portChanged && window.location.pathname === path) { + window.location.reload(); + } else { + if (portChanged) { + window.location = location.protocol + '//' + location.hostname + ':' + options.overrideURL + path; + } else { + window.location.pathname = path; + } + } + + return true; +}; + +LiveReload.addPlugin(HugoReload); diff --git a/livereload/gen/main.go b/livereload/gen/main.go new file mode 100644 index 000000000..d69ff9206 --- /dev/null +++ b/livereload/gen/main.go @@ -0,0 +1,61 @@ +//go:generate go run main.go +package main + +import ( + _ "embed" + "fmt" + "io" + "log" + "net/http" + "os" + + "github.com/evanw/esbuild/pkg/api" +) + +//go:embed livereload-hugo-plugin.js +var livereloadHugoPluginJS string + +func main() { + // 4.0.2 + // To upgrade to a new version, change to the commit hash of the version you want to upgrade to + // then run mage generate from the root. + const liveReloadCommit = "d803a41804d2d71e0814c4e9e3233e78991024d9" + liveReloadSourceURL := fmt.Sprintf("https://raw.githubusercontent.com/livereload/livereload-js/%s/dist/livereload.js", liveReloadCommit) + + func() { + resp, err := http.Get(liveReloadSourceURL) + must(err) + defer resp.Body.Close() + + b, err := io.ReadAll(resp.Body) + must(err) + + // Write the unminified livereload.js file. + err = os.WriteFile("../livereload.js", b, 0o644) + must(err) + + // Bundle and minify with ESBuild. + result := api.Build(api.BuildOptions{ + Stdin: &api.StdinOptions{ + Contents: string(b) + livereloadHugoPluginJS, + }, + Outfile: "../livereload.min.js", + Bundle: true, + Target: api.ES2015, + Write: true, + MinifyWhitespace: true, + MinifyIdentifiers: true, + MinifySyntax: true, + }) + + if len(result.Errors) > 0 { + log.Fatal(result.Errors) + } + }() +} + +func must(err error) { + if err != nil { + log.Fatal(err) + } +} diff --git a/livereload/livereload.go b/livereload/livereload.go index f24b42f37..0d24ada98 100644 --- a/livereload/livereload.go +++ b/livereload/livereload.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Hugo Authors. All rights reserved. +// Copyright 2024 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import ( ) // Prefix to signal to LiveReload that we need to navigate to another path. +// Do not change this. const hugoNavigatePrefix = "__hugo_navigate" var upgrader = &websocket.Upgrader{ @@ -144,48 +145,8 @@ func ServeJS(w http.ResponseWriter, r *http.Request) { } func liveReloadJS() []byte { - return []byte(livereloadJS + hugoLiveReloadPlugin) + return []byte(livereloadJS) } -var ( - // This is a patched version, see https://github.com/livereload/livereload-js/pull/84 - //go:embed livereload.js - livereloadJS string - hugoLiveReloadPlugin = fmt.Sprintf(` -/* -Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal -navigation to another content page. -*/ - -function HugoReload() {} - -HugoReload.identifier = 'hugoReloader'; -HugoReload.version = '0.9'; - -HugoReload.prototype.reload = function(path, options) { - var prefix = %q; - - if (path.lastIndexOf(prefix, 0) !== 0) { - return false - } - - path = path.substring(prefix.length); - - var portChanged = options.overrideURL && options.overrideURL != window.location.port - - if (!portChanged && window.location.pathname === path) { - window.location.reload(); - } else { - if (portChanged) { - window.location = location.protocol + "//" + location.hostname + ":" + options.overrideURL + path; - } else { - window.location.pathname = path; - } - } - - return true; -}; - -LiveReload.addPlugin(HugoReload) -`, hugoNavigatePrefix) -) +//go:embed livereload.min.js +var livereloadJS string diff --git a/livereload/livereload.js b/livereload/livereload.js index 1f4aa0fbc..f7b1e3884 100644 --- a/livereload/livereload.js +++ b/livereload/livereload.js @@ -1 +1,3795 @@ -!function(){return function e(t,o,n){function r(s,c){if(!o[s]){if(!t[s]){var a="function"==typeof require&&require;if(!c&&a)return a(s,!0);if(i)return i(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var h=o[s]={exports:{}};t[s][0].call(h.exports,function(e){return r(t[s][1][e]||e)},h,h.exports,e,t,o,n)}return o[s].exports}for(var i="function"==typeof require&&require,s=0;sh;)if((c=a[h++])!=c)return!0}else for(;l>h;h++)if((e||h in a)&&a[h]===o)return e||h||0;return!e&&-1}}},{"./_to-absolute-index":38,"./_to-iobject":40,"./_to-length":41}],5:[function(e,t,o){var n={}.toString;t.exports=function(e){return n.call(e).slice(8,-1)}},{}],6:[function(e,t,o){var n=t.exports={version:"2.6.5"};"number"==typeof __e&&(__e=n)},{}],7:[function(e,t,o){var n=e("./_a-function");t.exports=function(e,t,o){if(n(e),void 0===t)return e;switch(o){case 1:return function(o){return e.call(t,o)};case 2:return function(o,n){return e.call(t,o,n)};case 3:return function(o,n,r){return e.call(t,o,n,r)}}return function(){return e.apply(t,arguments)}}},{"./_a-function":1}],8:[function(e,t,o){t.exports=function(e){if(null==e)throw TypeError("Can't call method on "+e);return e}},{}],9:[function(e,t,o){t.exports=!e("./_fails")(function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})},{"./_fails":13}],10:[function(e,t,o){var n=e("./_is-object"),r=e("./_global").document,i=n(r)&&n(r.createElement);t.exports=function(e){return i?r.createElement(e):{}}},{"./_global":15,"./_is-object":21}],11:[function(e,t,o){t.exports="constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(",")},{}],12:[function(e,t,o){var n=e("./_global"),r=e("./_core"),i=e("./_hide"),s=e("./_redefine"),c=e("./_ctx"),a=function(e,t,o){var l,h,u,d,f=e&a.F,p=e&a.G,_=e&a.S,m=e&a.P,g=e&a.B,y=p?n:_?n[t]||(n[t]={}):(n[t]||{}).prototype,v=p?r:r[t]||(r[t]={}),w=v.prototype||(v.prototype={});for(l in p&&(o=t),o)u=((h=!f&&y&&void 0!==y[l])?y:o)[l],d=g&&h?c(u,n):m&&"function"==typeof u?c(Function.call,u):u,y&&s(y,l,u,e&a.U),v[l]!=u&&i(v,l,d),m&&w[l]!=u&&(w[l]=u)};n.core=r,a.F=1,a.G=2,a.S=4,a.P=8,a.B=16,a.W=32,a.U=64,a.R=128,t.exports=a},{"./_core":6,"./_ctx":7,"./_global":15,"./_hide":17,"./_redefine":34}],13:[function(e,t,o){t.exports=function(e){try{return!!e()}catch(e){return!0}}},{}],14:[function(e,t,o){t.exports=e("./_shared")("native-function-to-string",Function.toString)},{"./_shared":37}],15:[function(e,t,o){var n=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=n)},{}],16:[function(e,t,o){var n={}.hasOwnProperty;t.exports=function(e,t){return n.call(e,t)}},{}],17:[function(e,t,o){var n=e("./_object-dp"),r=e("./_property-desc");t.exports=e("./_descriptors")?function(e,t,o){return n.f(e,t,r(1,o))}:function(e,t,o){return e[t]=o,e}},{"./_descriptors":9,"./_object-dp":28,"./_property-desc":33}],18:[function(e,t,o){var n=e("./_global").document;t.exports=n&&n.documentElement},{"./_global":15}],19:[function(e,t,o){t.exports=!e("./_descriptors")&&!e("./_fails")(function(){return 7!=Object.defineProperty(e("./_dom-create")("div"),"a",{get:function(){return 7}}).a})},{"./_descriptors":9,"./_dom-create":10,"./_fails":13}],20:[function(e,t,o){var n=e("./_cof");t.exports=Object("z").propertyIsEnumerable(0)?Object:function(e){return"String"==n(e)?e.split(""):Object(e)}},{"./_cof":5}],21:[function(e,t,o){t.exports=function(e){return"object"==typeof e?null!==e:"function"==typeof e}},{}],22:[function(e,t,o){"use strict";var n=e("./_object-create"),r=e("./_property-desc"),i=e("./_set-to-string-tag"),s={};e("./_hide")(s,e("./_wks")("iterator"),function(){return this}),t.exports=function(e,t,o){e.prototype=n(s,{next:r(1,o)}),i(e,t+" Iterator")}},{"./_hide":17,"./_object-create":27,"./_property-desc":33,"./_set-to-string-tag":35,"./_wks":45}],23:[function(e,t,o){"use strict";var n=e("./_library"),r=e("./_export"),i=e("./_redefine"),s=e("./_hide"),c=e("./_iterators"),a=e("./_iter-create"),l=e("./_set-to-string-tag"),h=e("./_object-gpo"),u=e("./_wks")("iterator"),d=!([].keys&&"next"in[].keys()),f=function(){return this};t.exports=function(e,t,o,p,_,m,g){a(o,t,p);var y,v,w,b=function(e){if(!d&&e in L)return L[e];switch(e){case"keys":case"values":return function(){return new o(this,e)}}return function(){return new o(this,e)}},S=t+" Iterator",R="values"==_,k=!1,L=e.prototype,x=L[u]||L["@@iterator"]||_&&L[_],j=x||b(_),C=_?R?b("entries"):j:void 0,O="Array"==t&&L.entries||x;if(O&&(w=h(O.call(new e)))!==Object.prototype&&w.next&&(l(w,S,!0),n||"function"==typeof w[u]||s(w,u,f)),R&&x&&"values"!==x.name&&(k=!0,j=function(){return x.call(this)}),n&&!g||!d&&!k&&L[u]||s(L,u,j),c[t]=j,c[S]=f,_)if(y={values:R?j:b("values"),keys:m?j:b("keys"),entries:C},g)for(v in y)v in L||i(L,v,y[v]);else r(r.P+r.F*(d||k),t,y);return y}},{"./_export":12,"./_hide":17,"./_iter-create":22,"./_iterators":25,"./_library":26,"./_object-gpo":30,"./_redefine":34,"./_set-to-string-tag":35,"./_wks":45}],24:[function(e,t,o){t.exports=function(e,t){return{value:t,done:!!e}}},{}],25:[function(e,t,o){t.exports={}},{}],26:[function(e,t,o){t.exports=!1},{}],27:[function(e,t,o){var n=e("./_an-object"),r=e("./_object-dps"),i=e("./_enum-bug-keys"),s=e("./_shared-key")("IE_PROTO"),c=function(){},a=function(){var t,o=e("./_dom-create")("iframe"),n=i.length;for(o.style.display="none",e("./_html").appendChild(o),o.src="javascript:",(t=o.contentWindow.document).open(),t.write("