refactor(bundler): use encoding/json for JS string literals

This commit is contained in:
pj committed 2026-06-01 22:43:32 +05:30
1 parent b7bae73aaa
commit 9987cc95d4
1 file changed
+6 -24
+6 -24
View File
@@ -3,6 +3,7 @@ package bundler
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"path/filepath"
@@ -47,7 +48,11 @@ func BundleWeb(options WebOptions) (Result, error) {
defines := map[string]string{}
for key, value := range options.Defines {
defines["process.env."+key] = quoteJSString(value)
encoded, err := json.Marshal(value)
if err != nil {
return Result{}, fmt.Errorf("encode define %q: %w", key, err)
}
defines["process.env."+key] = string(encoded)
}
stdinContents := fmt.Sprintf("import %q;\n%s", runtimeAbs, registrationEntry(specAbs))
@@ -86,26 +91,3 @@ func BundleWeb(options WebOptions) (Result, error) {
SHA256: hex.EncodeToString(sum[:]),
}, nil
}
func quoteJSString(value string) string {
var builder strings.Builder
builder.WriteByte('"')
for index := 0; index < len(value); index++ {
c := value[index]
switch c {
case '"', '\\':
builder.WriteByte('\\')
builder.WriteByte(c)
case '\n':
builder.WriteString("\\n")
case '\r':
builder.WriteString("\\r")
case '\t':
builder.WriteString("\\t")
default:
builder.WriteByte(c)
}
}
builder.WriteByte('"')
return builder.String()
}