Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
faadc3eb6f | |||
51be76d4ad | |||
2efa8b43a3 | |||
64b594c4ac |
6
build.go
6
build.go
@@ -35,7 +35,11 @@ func buildAction(ctx *cli.Context) error {
|
|||||||
|
|
||||||
esBuildCfg.Plugins = append(esBuildCfg.Plugins, contentSwapPlugin(o))
|
esBuildCfg.Plugins = append(esBuildCfg.Plugins, contentSwapPlugin(o))
|
||||||
|
|
||||||
api.Build(esBuildCfg)
|
result := api.Build(esBuildCfg)
|
||||||
|
if len(result.Errors) > 0 {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
replace(o)
|
replace(o)
|
||||||
|
|
||||||
if ctx.Bool("p") && o.ProductionBuildOptions.CmdPostBuild != "" {
|
if ctx.Bool("p") && o.ProductionBuildOptions.CmdPostBuild != "" {
|
||||||
|
14
helpers.go
14
helpers.go
@@ -148,7 +148,7 @@ func replace(opts options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func injectLR(opts options) {
|
func injectLR(lrport uint, opts options) {
|
||||||
if opts.Watch.InjectLiveReload == "" {
|
if opts.Watch.InjectLiveReload == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -164,7 +164,7 @@ func injectLR(opts options) {
|
|||||||
|
|
||||||
if !opts.Watch.SkipCSPInject {
|
if !opts.Watch.SkipCSPInject {
|
||||||
// First modify CSP
|
// First modify CSP
|
||||||
htmlContent, err = updateContentPolicyTag(htmlContent)
|
htmlContent, err = updateContentPolicyTag(lrport, htmlContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error modifying CSP:", err)
|
fmt.Println("Error modifying CSP:", err)
|
||||||
return
|
return
|
||||||
@@ -172,7 +172,7 @@ func injectLR(opts options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Then inject script
|
// Then inject script
|
||||||
finalHTML, err := injectLiveReloadScript(htmlContent)
|
finalHTML, err := injectLiveReloadScript(lrport, htmlContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error injecting script:", err)
|
fmt.Println("Error injecting script:", err)
|
||||||
return
|
return
|
||||||
@@ -187,7 +187,7 @@ func injectLR(opts options) {
|
|||||||
fmt.Printf("Injected live reload script reference into %s\n", opts.Watch.InjectLiveReload)
|
fmt.Printf("Injected live reload script reference into %s\n", opts.Watch.InjectLiveReload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func injectLiveReloadScript(html string) (string, error) {
|
func injectLiveReloadScript(lrport uint, html string) (string, error) {
|
||||||
// Check if script is already present
|
// Check if script is already present
|
||||||
if strings.Contains(html, "livereload.js") {
|
if strings.Contains(html, "livereload.js") {
|
||||||
return html, nil
|
return html, nil
|
||||||
@@ -199,19 +199,19 @@ func injectLiveReloadScript(html string) (string, error) {
|
|||||||
return html, nil // Return unchanged if no body tag found
|
return html, nil // Return unchanged if no body tag found
|
||||||
}
|
}
|
||||||
|
|
||||||
scriptTag := `<script src="http://localhost:35729/livereload.js" type="text/javascript"></script>`
|
scriptTag := fmt.Sprintf(`<script src="http://localhost:%d/livereload.js" type="text/javascript"></script>`, lrport)
|
||||||
newHTML := bodyCloseRegex.ReplaceAllString(html, scriptTag+"</body>")
|
newHTML := bodyCloseRegex.ReplaceAllString(html, scriptTag+"</body>")
|
||||||
|
|
||||||
return newHTML, nil
|
return newHTML, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateContentPolicyTag(html string) (string, error) {
|
func updateContentPolicyTag(lrport uint, html string) (string, error) {
|
||||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return html, err
|
return html, err
|
||||||
}
|
}
|
||||||
|
|
||||||
liveReloadHost := "localhost:35729"
|
liveReloadHost := fmt.Sprintf("localhost:%d", lrport)
|
||||||
liveReloadURL := "http://" + liveReloadHost
|
liveReloadURL := "http://" + liveReloadHost
|
||||||
liveReloadWS := "ws://" + liveReloadHost
|
liveReloadWS := "ws://" + liveReloadHost
|
||||||
|
|
||||||
|
7
main.go
7
main.go
@@ -26,7 +26,7 @@ func main() {
|
|||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Name: "gowebbuild",
|
Name: "gowebbuild",
|
||||||
Usage: "All in one tool to build web frontend projects.",
|
Usage: "All in one tool to build web frontend projects.",
|
||||||
Version: "7.3.0",
|
Version: "7.5.0",
|
||||||
Authors: []*cli.Author{{
|
Authors: []*cli.Author{{
|
||||||
Name: "trading-peter (https://github.com/trading-peter)",
|
Name: "trading-peter (https://github.com/trading-peter)",
|
||||||
}},
|
}},
|
||||||
@@ -86,7 +86,10 @@ $ gowebbuild replace *.go foo bar
|
|||||||
cfgParam,
|
cfgParam,
|
||||||
&cli.UintFlag{
|
&cli.UintFlag{
|
||||||
Name: "lr-port",
|
Name: "lr-port",
|
||||||
Value: uint(lrserver.DefaultPort),
|
Value: (func() uint {
|
||||||
|
port := findFreePort(int(lrserver.DefaultPort), int(lrserver.DefaultPort)+100)
|
||||||
|
return uint(port)
|
||||||
|
}()),
|
||||||
Usage: "port for the live reload server",
|
Usage: "port for the live reload server",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
9
watch.go
9
watch.go
@@ -13,11 +13,13 @@ import (
|
|||||||
|
|
||||||
func watchAction(ctx *cli.Context) error {
|
func watchAction(ctx *cli.Context) error {
|
||||||
cfgPath, err := filepath.Abs(ctx.String("c"))
|
cfgPath, err := filepath.Abs(ctx.String("c"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lrport := ctx.Uint("lr-port")
|
||||||
|
fmt.Printf("Live reload is running on port %d\n", lrport)
|
||||||
|
|
||||||
os.Chdir(filepath.Dir(cfgPath))
|
os.Chdir(filepath.Dir(cfgPath))
|
||||||
optsSetups := readCfg(cfgPath)
|
optsSetups := readCfg(cfgPath)
|
||||||
|
|
||||||
@@ -25,7 +27,7 @@ func watchAction(ctx *cli.Context) error {
|
|||||||
purge(opts)
|
purge(opts)
|
||||||
cp(opts)
|
cp(opts)
|
||||||
build(opts)
|
build(opts)
|
||||||
injectLR(opts)
|
injectLR(lrport, opts)
|
||||||
replace(opts)
|
replace(opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,8 +108,7 @@ func watchAction(ctx *cli.Context) error {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
fmt.Println("Starting live reload server.")
|
fmt.Println("Starting live reload server.")
|
||||||
port := ctx.Uint("lr-port")
|
lr := lrserver.New(lrserver.DefaultName, uint16(lrport))
|
||||||
lr := lrserver.New(lrserver.DefaultName, uint16(port))
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
Reference in New Issue
Block a user