From f43b0cdb60ec199fdfaa4920a7b580621b0f1a93 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Thu, 15 Feb 2024 14:41:02 -0300 Subject: [PATCH] reload templates (not just rerender) on src change --- commands/serve.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/commands/serve.go b/commands/serve.go index 3d2bcea..1572e77 100644 --- a/commands/serve.go +++ b/commands/serve.go @@ -14,17 +14,13 @@ import ( // Generate and serve the site, rebuilding when the source files change. func Serve() error { - site, err := site.Load(SRC_DIR, LAYOUTS_DIR) - if err != nil { - return err - } - if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + if err := rebuild(); err != nil { return err } // watch for changes in src and layouts, and trigger a rebuild - watcher, err := setupWatcher(site) + watcher, err := setupWatcher() if err != nil { return err } @@ -39,6 +35,19 @@ func Serve() error { return nil } +func rebuild() error { + site, err := site.Load(SRC_DIR, LAYOUTS_DIR) + if err != nil { + return err + } + + if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + return err + } + + return nil +} + // Tweaks the http file system to construct a server that hides the .html suffix from requests. // Based on https://stackoverflow.com/a/57281956/993769 type HTMLDir struct { @@ -57,7 +66,7 @@ func (d HTMLDir) Open(name string) (http.File, error) { return f, err } -func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) { +func setupWatcher() (*fsnotify.Watcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { return nil, err @@ -88,7 +97,7 @@ func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) { return } - if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + if err := rebuild(); err != nil { fmt.Println("error:", err) return }