diff --git a/sui/core/compile.go b/sui/core/compile.go index 46457075b2..a6e9a9499d 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -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(`\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) { diff --git a/sui/core/compile_test.go b/sui/core/compile_test.go new file mode 100644 index 0000000000..d87e3d14f7 --- /dev/null +++ b/sui/core/compile_test.go @@ -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") +} diff --git a/sui/core/page_test.go b/sui/core/page_test.go index f032334b9f..45f18ac0db 100644 --- a/sui/core/page_test.go +++ b/sui/core/page_test.go @@ -45,6 +45,24 @@ func testPage(t *testing.T) *Page { page := &Page{ Name: "test", Route: "test", + Document: []byte(` + + +
+ + + +