| 1 |
//go:generate go run main.go |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
_ "embed" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net/http" |
| 10 |
"os" |
| 11 |
|
| 12 |
"github.com/evanw/esbuild/pkg/api" |
| 13 |
) |
| 14 |
|
| 15 |
//go:embed livereload-hugo-plugin.js |
| 16 |
var livereloadHugoPluginJS string |
| 17 |
|
| 18 |
func main() { |
| 19 |
// 4.0.2 |
| 20 |
// To upgrade to a new version, change to the commit hash of the version you want to upgrade to |
| 21 |
// then run mage generate from the root. |
| 22 |
const liveReloadCommit = "d803a41804d2d71e0814c4e9e3233e78991024d9" |
| 23 |
liveReloadSourceURL := fmt.Sprintf("https://raw.githubusercontent.com/livereload/livereload-js/%s/dist/livereload.js", liveReloadCommit) |
| 24 |
|
| 25 |
func() { |
| 26 |
resp, err := http.Get(liveReloadSourceURL) |
| 27 |
must(err) |
| 28 |
defer resp.Body.Close() |
| 29 |
|
| 30 |
b, err := io.ReadAll(resp.Body) |
| 31 |
must(err) |
| 32 |
|
| 33 |
// Write the unminified livereload.js file. |
| 34 |
err = os.WriteFile("../livereload.js", b, 0o644) |
| 35 |
must(err) |
| 36 |
|
| 37 |
// Bundle and minify with ESBuild. |
| 38 |
result := api.Build(api.BuildOptions{ |
| 39 |
Stdin: &api.StdinOptions{ |
| 40 |
Contents: string(b) + livereloadHugoPluginJS, |
| 41 |
}, |
| 42 |
Outfile: "../livereload.min.js", |
| 43 |
Bundle: true, |
| 44 |
Target: api.ES2015, |
| 45 |
Write: true, |
| 46 |
MinifyWhitespace: true, |
| 47 |
MinifyIdentifiers: true, |
| 48 |
MinifySyntax: true, |
| 49 |
}) |
| 50 |
|
| 51 |
if len(result.Errors) > 0 { |
| 52 |
log.Fatal(result.Errors) |
| 53 |
} |
| 54 |
}() |
| 55 |
} |
| 56 |
|
| 57 |
func must(err error) { |
| 58 |
if err != nil { |
| 59 |
log.Fatal(err) |
| 60 |
} |
| 61 |
} |