Add swap content option

This commit is contained in:
2025-07-16 22:01:26 +02:00
parent 84bdc73900
commit d5319e18e2
7 changed files with 179 additions and 43 deletions

View File

@@ -162,15 +162,17 @@ func injectLR(opts options) {
htmlContent := string(contents)
// First modify CSP
cspModified, err := updateContentPolicyTag(htmlContent)
if err != nil {
fmt.Println("Error modifying CSP:", err)
return
if !opts.Watch.SkipCSPInject {
// First modify CSP
htmlContent, err = updateContentPolicyTag(htmlContent)
if err != nil {
fmt.Println("Error modifying CSP:", err)
return
}
}
// Then inject script
finalHTML, err := injectLiveReloadScript(cspModified)
finalHTML, err := injectLiveReloadScript(htmlContent)
if err != nil {
fmt.Println("Error injecting script:", err)
return
@@ -277,6 +279,9 @@ func updateContentPolicyTag(html string) (string, error) {
func build(opts options) {
esBuildOpts := cfgToESBuildCfg(opts)
esBuildOpts.Plugins = append(esBuildOpts.Plugins, contentSwapPlugin(opts))
result := api.Build(esBuildOpts)
if len(result.Errors) == 0 {
@@ -284,6 +289,33 @@ func build(opts options) {
}
}
func contentSwapPlugin(opts options) api.Plugin {
return api.Plugin{
Name: "content-swap",
Setup: func(build api.PluginBuild) {
build.OnLoad(api.OnLoadOptions{Filter: `.*`},
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
for _, swap := range opts.ContentSwap {
if strings.HasSuffix(args.Path, swap.File) {
fmt.Printf("Swapping content of %s with %s\n", args.Path, swap.ReplaceWith)
text, err := os.ReadFile(swap.ReplaceWith)
if err != nil {
return api.OnLoadResult{}, err
}
contents := string(text)
return api.OnLoadResult{
Contents: &contents,
Loader: api.LoaderJS,
}, nil
}
}
return api.OnLoadResult{}, nil
})
},
}
}
func getGoModuleName(root string) (string, error) {
modFile := filepath.Join(root, "go.mod")