-
Notifications
You must be signed in to change notification settings - Fork 1
/
transpiler_internal.go
191 lines (163 loc) · 5.03 KB
/
transpiler_internal.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gomini
import (
"github.com/apex/log"
"github.com/go-errors/errors"
"path/filepath"
"github.com/spf13/afero"
"os"
"fmt"
"encoding/json"
)
type transpilerCache struct {
TranspilerVersion string `json:"transpiler_version"`
Modules []transpiledModule `json:"modules"`
}
type transpiledModule struct {
OriginalFile string `json:"original_file"`
CacheFile string `json:"cache_file"`
Checksum string `json:"checksum"`
BundleId string `json:"bundle_id"`
}
func (t *transpiler) __initialize() {
if t.sandbox == nil {
log.Info("Transpiler: Setting up TypeScript transpiler...")
t.sandbox = t.kernel.kernelConfig.NewSandbox(t.kernel)
t.sandbox.Global().DefineFunction("tsVersion", "tsVersion", func(call FunctionCall) Value {
version := call.Argument(0).String()
log.Infof("Transpiler: Using bundled TypeScript v%s", version)
t.transpilerVersion = version
return t.sandbox.UndefinedValue()
})
builder := t.sandbox.NewObjectCreator("console")
builder.DefineGoFunction("log", "log", func(msg interface{}) {
stackFrames := t.sandbox.CaptureCallStack(2)
frame := stackFrames[1]
pos := frame.Position()
log.Infof("%s[%d:%d]: %s", frame.SrcName(), pos.Line, pos.Col, msg)
})
builder.BuildInto("console", t.sandbox.Global())
if _, err := t.__loadScript(t.kernel, "/js/typescript", ""); err != nil {
panic(err)
}
if _, err := t.__loadScript(t.kernel, "embedded://tsc.js", tscSource); err != nil {
panic(err)
}
}
}
func (t *transpiler) __transpileSource(source string) (*string, error) {
// Make sure the underlying runtime is initialized
t.__initialize()
// Retrieve the transpiler function from the runtime
jsTranspiler := t.sandbox.Global().Get("transpiler")
if jsTranspiler == nil || jsTranspiler == t.sandbox.NullValue() {
panic(errors.New("transpiler function not available"))
}
var transpiler Callable
if err := t.sandbox.Export(jsTranspiler, &transpiler); err != nil {
return nil, err
}
// Transpile
if val, err := transpiler(jsTranspiler, t.sandbox.ToValue(source)); err != nil {
return nil, err
} else {
source := val.String()
return &source, nil
}
}
func (t *transpiler) __loadScript(bundle Bundle, filename string, source string) (Value, error) {
if source == "" {
scriptFile := t.kernel.resolveScriptPath(t.kernel, filename)
filename = fmt.Sprintf("%s:/%s", scriptFile.loader.Name(), scriptFile.path)
s, err := t.kernel.loadContent(bundle, scriptFile.loader.Filesystem(), scriptFile.path)
if err != nil {
return nil, err
}
source = string(s)
}
script, _, err := t.sandbox.Compile(filename, source)
if err != nil {
return nil, err
}
return t.sandbox.Execute(script)
}
func (t *transpiler) __findTranspiledModule(filename string) *transpiledModule {
for _, module := range t.transpilerCache.Modules {
if module.OriginalFile == filename {
return &module
}
}
return nil
}
func (t *transpiler) __removeTranspiledModule(module *transpiledModule) error {
if module == nil {
return nil
}
for i, temp := range t.transpilerCache.Modules {
if temp.OriginalFile == module.OriginalFile {
t.transpilerCache.Modules = append(t.transpilerCache.Modules[:i], t.transpilerCache.Modules[i+1:]...)
return nil
}
}
// Store transpiled module information
if err := t.__storeModuleCacheInformation(); err != nil {
return err
}
return nil
}
func (t *transpiler) __addTranspiledModule(path, cacheFile, code string, bundle Bundle) error {
module := transpiledModule{path, cacheFile, hash(code), bundle.ID()}
t.transpilerCache.Modules = append(t.transpilerCache.Modules, module)
// Store transpiled module information
if err := t.__storeModuleCacheInformation(); err != nil {
return err
}
return nil
}
func (t *transpiler) __storeModuleCacheInformation() error {
file := filepath.Join(KernelVfsCachePath, cacheJsonFile)
t.kernel.filesystem.Remove(file)
if data, err := json.Marshal(t.transpilerCache); err != nil {
return err
} else {
if err := afero.WriteFile(t.kernel.filesystem, file, data, os.ModePerm); err != nil {
return err
}
}
return nil
}
const tscSource = `
tsVersion(ts.version);
function transpiler(source) {
var result = ts.transpileModule(source, {
compilerOptions: {
moduleResolution: "node",
module: "System",
target: "es5",
isolatedModules: true,
importHelpers: true,
tsconfig: false,
noImplicitAny: false,
alwaysStrict: true,
inlineSourceMap: true,
diagnostics: true,
strictPropertyInitialization: true,
allowJs: false,
downlevelIteration: true,
noLib: true,
declaration: true,
typeRoots: [
"scripts/types"
],
lib: [
"lib/libbase.d.ts"
]
},
reportDiagnostics: true,
transformers: []
});
for (var i = 0; i < result.diagnostics.length; i++) {
ts.sys.write(result.diagnostics[i]);
}
return result.outputText;
}
`