-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
3,903 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters