-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine_code.go
More file actions
92 lines (86 loc) · 3.36 KB
/
engine_code.go
File metadata and controls
92 lines (86 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package engine
import (
"fmt"
"github.com/ZenLiuCN/fn"
"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"math/rand"
"os"
"path/filepath"
"strings"
)
type Code struct {
Path string
*goja.Program
}
// CompileFile entry for whether source is as script or library
func CompileFile(path string, entry bool) *Code {
if len(path) > 3 && (strings.EqualFold(path[len(path)-3:], ".js") || strings.EqualFold(path[len(path)-3:], ".cjs") || strings.EqualFold(path[len(path)-3:], ".mjs")) {
data := string(fn.Panic1(os.ReadFile(path)))
return &Code{Path: path, Program: fn.Panic1(goja.Compile(path, CompileJs(data, entry), true))}
}
if len(path) > 3 && strings.EqualFold(path[len(path)-3:], ".ts") {
data := string(fn.Panic1(os.ReadFile(path)))
return &Code{Path: path, Program: fn.Panic1(goja.Compile(path, CompileTs(data, entry), true))}
}
panic(fmt.Errorf(`unsupported file %s`, path))
}
// CompileSource compile script source, ts for source is typescript or javascript. entry for whether source is as script or library
func CompileSource(source string, ts, entry bool) *Code {
if !ts {
return &Code{Path: "", Program: fn.Panic1(goja.Compile("", CompileJs(source, entry), false))}
} else {
return &Code{Path: "", Program: fn.Panic1(goja.Compile("", CompileTs(source, entry), false))}
}
}
func CompileFileSource(path, source string, ts, entry bool) *Code {
if !ts {
return &Code{Path: path, Program: fn.Panic1(goja.Compile("", CompileJs(source, entry), false))}
} else {
return &Code{Path: path, Program: fn.Panic1(goja.Compile("", CompileTs(source, entry), false))}
}
}
func CompileFileWithMapping(path string, entry bool) (*Code, SourceMapping) {
if len(path) > 3 && (strings.EqualFold(path[len(path)-3:], ".js") || strings.EqualFold(path[len(path)-3:], ".cjs") || strings.EqualFold(path[len(path)-3:], ".mjs")) {
data := string(fn.Panic1(os.ReadFile(path)))
var base = filepath.Base(path)
s, m, b := CompileJsWithMapping(base, data, entry)
return &Code{Path: path, Program: compile(base, s, b)}, m
}
if len(path) > 3 && strings.EqualFold(path[len(path)-3:], ".ts") {
data := string(fn.Panic1(os.ReadFile(path)))
var base = filepath.Base(path)
s, m, b := CompileTsWithMapping(base, data, entry)
return &Code{Path: path, Program: compile(base, s, b)}, m
}
panic(fmt.Errorf(`unsupported file %s`, path))
}
func CompileSourceWithMapping(name, source string, ts, entry bool) (*Code, SourceMapping) {
if !ts {
s, m, b := CompileJsWithMapping(name, source, entry)
return &Code{Path: "", Program: compile(name, s, b)}, m
} else {
s, m, b := CompileTsWithMapping(name, source, entry)
return &Code{Path: "", Program: compile(name, s, b)}, m
}
}
func CompileFileSourceWithMapping(path, source string, ts, entry bool) (*Code, SourceMapping) {
var base = filepath.Base(path)
if !ts {
s, m, b := CompileJsWithMapping(base, source, entry)
return &Code{Path: path, Program: compile(base, s, b)}, m
} else {
s, m, b := CompileTsWithMapping(base, source, entry)
return &Code{Path: path, Program: compile(base, s, b)}, m
}
}
func compile(name, source string, m []byte) *goja.Program {
rnd := fmt.Sprintf("%s_%d.map", name, rand.Int())
p := fn.Panic1(goja.Parse(name, source+"\n//# sourceMappingURL="+rnd, parser.WithSourceMapLoader(func(n string) ([]byte, error) {
if n == rnd {
return m, nil
}
return nil, nil
})))
return fn.Panic1(goja.CompileAST(p, false))
}