82 lines
2 KiB
Go
82 lines
2 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
|
"github.com/yaoapp/gou/runtime/transform"
|
|
"github.com/yaoapp/kun/log"
|
|
)
|
|
|
|
// Compile the page
|
|
func (page *Page) Compile(option *BuildOption) (string, error) {
|
|
|
|
doc, warnings, err := page.Build(option)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if warnings != nil && len(warnings) > 0 {
|
|
for _, warning := range warnings {
|
|
log.Warn("Compile page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, warning)
|
|
}
|
|
}
|
|
|
|
if page.Codes.DATA.Code != "" {
|
|
doc.Find("body").AppendHtml(`<script name="data" type="json">` +
|
|
fmt.Sprintf("\n%s\n", page.Codes.DATA.Code) +
|
|
"</script>\n",
|
|
)
|
|
}
|
|
|
|
html, err := doc.Html()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// @todo: Minify the html
|
|
return html, nil
|
|
}
|
|
|
|
// CompileJS compile the javascript
|
|
func (page *Page) CompileJS(source []byte, minify bool) ([]byte, error) {
|
|
jsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
|
|
if minify {
|
|
minified, err := transform.MinifyJS(jsCode)
|
|
return []byte(minified), err
|
|
}
|
|
return []byte(jsCode), nil
|
|
}
|
|
|
|
// CompileTS compile the typescript
|
|
func (page *Page) CompileTS(source []byte, minify bool) ([]byte, error) {
|
|
tsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
|
|
if minify {
|
|
jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{
|
|
Target: api.ESNext,
|
|
MinifyWhitespace: true,
|
|
MinifyIdentifiers: true,
|
|
MinifySyntax: true,
|
|
})
|
|
|
|
return []byte(jsCode), err
|
|
}
|
|
|
|
jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{Target: api.ESNext})
|
|
return []byte(jsCode), err
|
|
}
|
|
|
|
// CompileCSS compile the css
|
|
func (page *Page) CompileCSS(source []byte, minify bool) ([]byte, error) {
|
|
if minify {
|
|
cssCode, err := transform.MinifyCSS(string(source))
|
|
return []byte(cssCode), err
|
|
}
|
|
return source, nil
|
|
}
|
|
|
|
// CompileHTML compile the html
|
|
func (page *Page) CompileHTML(source []byte, minify bool) ([]byte, error) {
|
|
return source, nil
|
|
}
|