-
Notifications
You must be signed in to change notification settings - Fork 6
/
babel.go
132 lines (115 loc) · 2.8 KB
/
babel.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package babel
import (
_ "embed"
"fmt"
"io"
"io/ioutil"
"strings"
"sync"
"github.com/dop251/goja"
)
const DefaultPoolSize = 1
type babelTransformer struct {
Runtime *goja.Runtime
Transform func(string, map[string]interface{}) (goja.Value, error)
}
func (t *babelTransformer) Done() {
globalpool <- t
}
var once = &sync.Once{}
var globalpool chan *babelTransformer
var babelProg *goja.Program
func Init(poolSize int) (err error) {
defer (func() {
if err != nil {
once = &sync.Once{}
}
})()
once.Do(func() {
if e := compileBabel(); e != nil {
err = e
return
}
globalpool = make(chan *babelTransformer, poolSize)
for i := 0; i < poolSize; i++ {
vm := goja.New()
// define console.{log|error|warn} so loading babel doesn't error
logFunc := func(goja.FunctionCall) goja.Value { return nil }
vm.Set("console", map[string]func(goja.FunctionCall) goja.Value{
"log": logFunc,
"error": logFunc,
"warn": logFunc,
})
transformFn, e := loadBabel(vm)
if e != nil {
err = e
return
}
globalpool <- &babelTransformer{Runtime: vm, Transform: transformFn}
}
})
return err
}
func Transform(src io.Reader, opts map[string]interface{}) (io.Reader, error) {
data, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
res, err := TransformString(string(data), opts)
if err != nil {
return nil, err
}
return strings.NewReader(res), nil
}
func TransformString(src string, opts map[string]interface{}) (string, error) {
if opts == nil {
opts = map[string]interface{}{}
}
t, err := getTransformer()
if err != nil {
return "", err
}
defer func() { t.Done() }() // Make transformer available again when we're done
v, err := t.Transform(src, opts)
if err != nil {
return "", err
}
vm := t.Runtime
return v.ToObject(vm).Get("code").String(), nil
}
func getTransformer() (*babelTransformer, error) {
// Make sure we have a pool created
if len(globalpool) == 0 {
if err := Init(DefaultPoolSize); err != nil {
return nil, err
}
}
for {
t := <-globalpool
return t, nil
}
}
//go:embed babel.js
var babelData string
func compileBabel() error {
var err error
babelProg, err = goja.Compile("babel.js", babelData, false)
if err != nil {
return err
}
return nil
}
func loadBabel(vm *goja.Runtime) (func(string, map[string]interface{}) (goja.Value, error), error) {
_, err := vm.RunProgram(babelProg)
if err != nil {
return nil, fmt.Errorf("unable to load babel.js: %s", err)
}
var transform goja.Callable
babel := vm.Get("Babel")
if err := vm.ExportTo(babel.ToObject(vm).Get("transform"), &transform); err != nil {
return nil, fmt.Errorf("unable to export transform fn: %s", err)
}
return func(src string, opts map[string]interface{}) (goja.Value, error) {
return transform(babel, vm.ToValue(src), vm.ToValue(opts))
}, nil
}