[add] sui page Compile

This commit is contained in:
Max 2023-11-22 11:37:08 +08:00
parent f06bbb2447
commit ef05990480
5 changed files with 74 additions and 17 deletions

View file

@ -1,14 +1,43 @@
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() {}
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) {

20
sui/core/compile_test.go Normal file
View file

@ -0,0 +1,20 @@
package core
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompile(t *testing.T) {
prepare(t)
defer clean()
page := testPage(t)
html, err := page.Compile(&BuildOption{})
if err != nil {
t.Fatalf("Compile error: %v", err)
}
assert.Contains(t, html, "input.data")
}

View file

@ -45,6 +45,24 @@ func testPage(t *testing.T) *Page {
page := &Page{
Name: "test",
Route: "test",
Document: []byte(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="/assets/css/tailwind.css" />
<link rel="stylesheet" href="/assets/css/app.css" />
</head>
<body>
<div id="app">
{{ __page }}
</div>
</body>
</html>
`),
Codes: SourceCodes{
HTML: Source{
File: "test.html",

View file

@ -9,6 +9,8 @@ import (
"github.com/yaoapp/kun/maps"
)
var varRe = regexp.MustCompile(`{{\s*([^{}]+)\s*}}`)
// Setting the struct for the DSL
func (sui *DSL) Setting() (*Setting, error) {
return &Setting{
@ -42,13 +44,12 @@ func (sui *DSL) PublicRoot() (string, error) {
var root = sui.Public.Root
dot := maps.Of(vars).Dot()
re := regexp.MustCompile(`{{\s*([^{}]+)\s*}}`)
output := re.ReplaceAllStringFunc(root, func(matched string) string {
output := varRe.ReplaceAllStringFunc(root, func(matched string) string {
varName := strings.TrimSpace(matched[2 : len(matched)-2])
if value, ok := dot[varName]; ok {
return fmt.Sprint(value)
}
return matched
return "__undefined"
})
sui.publicRoot = output

View file

@ -84,24 +84,13 @@ func (page *Page) Build(option *core.BuildOption) error {
option.AssetRoot = filepath.Join(page.tmpl.local.DSL.Public.Root, "assets")
}
doc, _, err := page.Page.Build(option)
if err != nil {
return err
}
html, err := doc.Html()
html, err := page.Page.Compile(option)
if err != nil {
return err
}
// Save the html
err = page.writeHTML([]byte(html))
if err != nil {
return err
}
// Save the data
return page.writeData()
return page.writeHTML([]byte(html))
}
func (page *Page) publicFile() string {