-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.go
More file actions
197 lines (175 loc) · 5.43 KB
/
compiler.go
File metadata and controls
197 lines (175 loc) · 5.43 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
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
192
193
194
195
196
197
package goact
import (
"bytes"
"fmt"
"github.com/buke/quickjs-go"
esbuild "github.com/evanw/esbuild/pkg/api"
html2 "github.com/sevenreup/goact/html"
"html/template"
"log"
"strings"
textTemplate "text/template"
)
type BuildFiles struct {
Js string
Css esbuild.OutputFile
Html string
}
type GoactCompiler struct {
outputDir string
workingDir string
isDebug bool
injectCss bool
}
type Params struct {
Title string
Meta map[string]string
JS template.JS
CSS template.CSS
Body template.HTML
Head template.HTML
}
var textEncoderPolyfill = `function TextEncoder(){}TextEncoder.prototype.encode=function(string){var octets=[];var length=string.length;var i=0;while(i<length){var codePoint=string.codePointAt(i);var c=0;var bits=0;if(codePoint<=0x0000007F){c=0;bits=0x00}else if(codePoint<=0x000007FF){c=6;bits=0xC0}else if(codePoint<=0x0000FFFF){c=12;bits=0xE0}else if(codePoint<=0x001FFFFF){c=18;bits=0xF0}octets.push(bits|(codePoint>>c));c-=6;while(c>=0){octets.push(0x80|((codePoint>>c)&0x3F));c-=6}i+=codePoint>=0x10000?2:1}return octets};function TextDecoder(){}TextDecoder.prototype.decode=function(octets){var string="";var i=0;while(i<octets.length){var octet=octets[i];var bytesNeeded=0;var codePoint=0;if(octet<=0x7F){bytesNeeded=0;codePoint=octet&0xFF}else if(octet<=0xDF){bytesNeeded=1;codePoint=octet&0x1F}else if(octet<=0xEF){bytesNeeded=2;codePoint=octet&0x0F}else if(octet<=0xF4){bytesNeeded=3;codePoint=octet&0x07}if(octets.length-i-bytesNeeded>0){var k=0;while(k<bytesNeeded){octet=octets[i+k+1];codePoint=(codePoint<<6)|(octet&0x3F);k+=1}}else{codePoint=0xFFFD;bytesNeeded=octets.length-i}string+=String.fromCodePoint(codePoint);i+=bytesNeeded+1}return string};`
var consolePolyfill = `var console = {log: function(){}};`
var shims = "\nvar process = { env: new Proxy({}, { get: () => '', }) }"
var loaders = map[string]esbuild.Loader{
".png": esbuild.LoaderFile,
".svg": esbuild.LoaderFile,
".jpg": esbuild.LoaderFile,
".jpeg": esbuild.LoaderFile,
".gif": esbuild.LoaderFile,
".bmp": esbuild.LoaderFile,
".woff2": esbuild.LoaderFile,
".woff": esbuild.LoaderFile,
".ttf": esbuild.LoaderFile,
".eot": esbuild.LoaderFile,
}
func NewGoactCompiler(outDir string, workingDir string, injectCss bool, isDebug bool) *GoactCompiler {
compiler := GoactCompiler{
outputDir: outDir,
workingDir: workingDir,
isDebug: isDebug,
injectCss: injectCss,
}
return &compiler
}
func (g *GoactCompiler) Compile(content string, layout string, props string, baseHtmlLayout string, renderData RenderData, defaultRenderData RenderData) (string, error) {
build, err := g.compileReactToHtml(content, layout, props)
if err != nil {
return "", err
}
tmpl, err := template.New(baseHtmlLayout).Parse(baseHtmlLayout)
if err != nil {
return "", err
}
var title string
if len(renderData.Title) > 0 {
title = renderData.Title
} else {
title = defaultRenderData.Title
}
css := template.CSS("")
if g.injectCss {
css = template.CSS(build.Css.Contents)
}
var head template.HTML
if len(renderData.Head) > 0 {
head = renderData.Head
} else {
head = defaultRenderData.Head
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, Params{
Body: template.HTML(build.Html),
Title: title,
CSS: css,
JS: "",
Meta: renderData.MetaTags,
Head: head,
})
if err != nil {
return "", err
}
return buf.String(), nil
}
func (g *GoactCompiler) compileReactToHtml(content string, layout string, props string) (*BuildFiles, error) {
var tmpl *textTemplate.Template
if len(layout) > 0 {
temp, err := textTemplate.New(html2.BaseReactRenderWithLayout).Parse(html2.BaseReactRenderWithLayout)
if err != nil {
return nil, err
}
tmpl = temp
} else {
temp, err := textTemplate.New(html2.BaseReactRenderNoLayout).Parse(html2.BaseReactRenderNoLayout)
if err != nil {
return nil, err
}
tmpl = temp
}
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string]string{
"FilePath": content,
"Content": "",
"LayoutPath": layout,
"Props": fmt.Sprintf("const props = %s;", props),
})
if err != nil {
return nil, err
}
fmt.Println(buf.String())
std := esbuild.StdinOptions{
Contents: buf.String(),
Loader: esbuild.LoaderTSX,
ResolveDir: g.workingDir,
}
opts := esbuild.BuildOptions{
Outdir: g.outputDir,
Platform: esbuild.PlatformNode,
Metafile: false,
Bundle: true,
MinifyWhitespace: !g.isDebug,
MinifyIdentifiers: !g.isDebug,
MinifySyntax: !g.isDebug,
Write: true,
Stdin: &std,
JSXDev: true,
JSX: esbuild.JSXAutomatic,
Loader: loaders,
Banner: map[string]string{
"js": textEncoderPolyfill + consolePolyfill + shims,
},
}
result := esbuild.Build(opts)
if len(result.Errors) != 0 {
fmt.Println(result.Errors)
}
build := BuildFiles{}
var jsContent string
for _, file := range result.OutputFiles {
if strings.HasSuffix(file.Path, ".js") {
build.Js = file.Path
jsContent = string(file.Contents)
} else if strings.HasSuffix(file.Path, ".css") {
build.Css = file
}
}
html, err := renderReactToHTMLNewQuick(jsContent)
if err != nil {
log.Print(err)
return nil, err
}
build.Html = html
return &build, nil
}
func renderReactToHTMLNewQuick(js string) (string, error) {
rt := quickjs.NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
res, err := ctx.Eval(js)
if err != nil {
return "", err
}
return res.String(), nil
}