Skip to content

Commit

Permalink
feat: add HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Sato1995 committed Jan 26, 2024
1 parent ecc8720 commit b03c0b0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ simple-ssg-cli init
- create cmd to copy the template
- [ ] write tests
- [ ] SEO stuff
- [ ] Custom Error pages(404)
- [ ] Custom Metadata
- [ ] HMR
- [x] Custom Error pages(404)
- [x] HMR
5 changes: 4 additions & 1 deletion example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module example

go 1.21.6

require github.com/K-Sato1995/go-simple-ssg v0.0.4
require (
github.com/K-Sato1995/go-simple-ssg v0.0.4
github.com/radovskyb/watcher v1.0.7
)

require (
github.com/evanw/esbuild v0.19.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/evanw/esbuild v0.19.12 h1:p5WGo4o6TCN+kt+uZtYSGS3ZHPa+iIZ0SX+ys8UnP10
github.com/evanw/esbuild v0.19.12/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
29 changes: 28 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

gosimplessg "github.com/K-Sato1995/go-simple-ssg"
"github.com/radovskyb/watcher"

"github.com/K-Sato1995/go-simple-ssg/config"
)
Expand All @@ -17,7 +18,7 @@ func main() {
},
})
engine := gosimplessg.New(baseConfig)
engine.Build()
go startHMR(baseConfig.TemplatePath, engine)
serveFiles()
}

Expand All @@ -30,3 +31,29 @@ func serveFiles() {
log.Fatal(err)
}
}

func startHMR(templatePath string, engine *gosimplessg.Engine) {
w := watcher.New()
w.FilterOps(watcher.Write)
if err := w.AddRecursive(templatePath); err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case <-w.Event:
log.Println("Change detected. Rebuilding...")
engine.Build()
log.Println("Rebuild completed.")
case err := <-w.Error:
log.Println("Watcher error:", err)
case <-w.Closed:
return
}
}
}()

if err := w.Start(250); err != nil {
log.Fatal(err)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ require (
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47
)

require golang.org/x/sys v0.16.0 // indirect
require (
github.com/radovskyb/watcher v1.0.7 // indirect
golang.org/x/sys v0.16.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/evanw/esbuild v0.19.12 h1:p5WGo4o6TCN+kt+uZtYSGS3ZHPa+iIZ0SX+ys8UnP10
github.com/evanw/esbuild v0.19.12/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

0 comments on commit b03c0b0

Please sign in to comment.