mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 19:17:10 +00:00
refactor(bundler): use encoding/json for JS string literals
This commit is contained in:
1 parent
b7bae73aaa
commit
9987cc95d4
1 file changed
+6
-24
@@ -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()
|
||||
}
|
||||
Reference in new issue
Block a user