Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

livereload: Improve the livereload script build and update to v4.0.2 #12455

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions livereload/gen/livereload-hugo-plugin.js
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);
61 changes: 61 additions & 0 deletions livereload/gen/main.go
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)
}
}
49 changes: 5 additions & 44 deletions livereload/livereload.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
3,796 changes: 3,795 additions & 1 deletion livereload/livereload.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions livereload/livereload.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func flagEnv() map[string]string {
// Generate autogen packages
func Generate() error {
generatorPackages := []string{
//"tpl/tplimpl/embedded/generate",
//"resources/page/generate",
"livereload/gen",
}

for _, pkg := range generatorPackages {
Expand Down
Loading