mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
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
This commit is contained in:
parent
6dfeb9f038
commit
d02f0622b4
6 changed files with 3903 additions and 47 deletions
34
livereload/gen/livereload-hugo-plugin.js
Normal file
34
livereload/gen/livereload-hugo-plugin.js
Normal file
|
@ -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);
|
61
livereload/gen/main.go
Normal file
61
livereload/gen/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with 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.
|
// Prefix to signal to LiveReload that we need to navigate to another path.
|
||||||
|
// Do not change this.
|
||||||
const hugoNavigatePrefix = "__hugo_navigate"
|
const hugoNavigatePrefix = "__hugo_navigate"
|
||||||
|
|
||||||
var upgrader = &websocket.Upgrader{
|
var upgrader = &websocket.Upgrader{
|
||||||
|
@ -144,48 +145,8 @@ func ServeJS(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func liveReloadJS() []byte {
|
func liveReloadJS() []byte {
|
||||||
return []byte(livereloadJS + hugoLiveReloadPlugin)
|
return []byte(livereloadJS)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
//go:embed livereload.min.js
|
||||||
// This is a patched version, see https://github.com/livereload/livereload-js/pull/84
|
var livereloadJS string
|
||||||
//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)
|
|
||||||
)
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
7
livereload/livereload.min.js
vendored
Normal file
7
livereload/livereload.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -79,8 +79,7 @@ func flagEnv() map[string]string {
|
||||||
// Generate autogen packages
|
// Generate autogen packages
|
||||||
func Generate() error {
|
func Generate() error {
|
||||||
generatorPackages := []string{
|
generatorPackages := []string{
|
||||||
//"tpl/tplimpl/embedded/generate",
|
"livereload/gen",
|
||||||
//"resources/page/generate",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pkg := range generatorPackages {
|
for _, pkg := range generatorPackages {
|
||||||
|
|
Loading…
Reference in a new issue