From 80d28ab8c520a4f5b122434cb40fd7f87995178d Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 28 Jun 2024 20:17:06 +0800 Subject: [PATCH] refactor: Minify script and style code during SUI page build --- sui/core/build.go | 15 ++++++++------- sui/core/compile.go | 6 ++++-- sui/core/types.go | 2 ++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 438f3273e..f7fcfdfb9 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -54,14 +54,14 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume } // Add Style - _, err = page.BuildStyle(ctx, option) + style, err := page.BuildStyle(ctx, option) if err != nil { warnings = append(warnings, err.Error()) } - doc.Selection.Find("head").AppendHtml(fmt.Sprintf("\n"+``+"\n", strings.Join(ctx.styles, "\n"))) + doc.Selection.Find("head").AppendHtml(fmt.Sprintf("\n"+``+"\n%s\n", strings.Join(ctx.styles, "\n"), style)) // Add Script - _, scripts, err := page.BuildScript(ctx, option, option.Namespace) + code, scripts, err := page.BuildScript(ctx, option, option.Namespace) if err != nil { warnings = append(warnings, err.Error()) } @@ -70,8 +70,7 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume doc.Selection.Find("body").AppendHtml("\n" + `` + "\n") } } - doc.Selection.Find("body").AppendHtml(fmt.Sprintf("\n"+``+"\n", strings.Join(ctx.scripts, "\n"))) - + doc.Selection.Find("body").AppendHtml(fmt.Sprintf("\n"+``+"\n%s\n", strings.Join(ctx.scripts, "\n"), code)) return doc, warnings, nil } @@ -249,6 +248,8 @@ func (page *Page) parse(ctx *BuildContext, doc *goquery.Document, option *BuildO IgnoreDocument: true, Namespace: namespace, ComponentName: componentName, + ScriptMinify: true, + StyleMinify: true, }, slots, attrs) // append translations @@ -365,7 +366,7 @@ func (page *Page) BuildScript(ctx *BuildContext, option *BuildOption, namespace // TypeScript if page.Codes.TS.Code != "" { - code, scripts, err := page.CompileTS([]byte(page.Codes.TS.Code), false) + code, scripts, err := page.CompileTS([]byte(page.Codes.TS.Code), option.ScriptMinify) if err != nil { return "", nil, err } @@ -398,7 +399,7 @@ func (page *Page) BuildScript(ctx *BuildContext, option *BuildOption, namespace } // JavaScript - code, scripts, err := page.CompileJS([]byte(page.Codes.JS.Code), true) + code, scripts, err := page.CompileJS([]byte(page.Codes.JS.Code), option.ScriptMinify) if err != nil { return "", nil, err } diff --git a/sui/core/compile.go b/sui/core/compile.go index 977bb570d..06483bdad 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -82,7 +82,9 @@ func (page *Page) CompileJS(source []byte, minify bool) ([]byte, []string, error minified, err := transform.MinifyJS(jsCode, api.ES2015) return []byte(minified), scripts, err } - return []byte(jsCode), scripts, nil + + jsCode, err := transform.JavaScript(string(jsCode), api.TransformOptions{Target: api.ES2015}) + return []byte(jsCode), scripts, err } // CompileTS compile the typescript @@ -108,7 +110,7 @@ func (page *Page) CompileTS(source []byte, minify bool) ([]byte, []string, error return []byte(jsCode), scripts, err } - jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{Target: api.ESNext}) + jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{Target: api.ES2015}) return []byte(jsCode), scripts, err } diff --git a/sui/core/types.go b/sui/core/types.go index 125e213b7..37e00a06d 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -208,6 +208,8 @@ type BuildOption struct { Namespace string `json:"namespace,omitempty"` Data map[string]interface{} `json:"data,omitempty"` ComponentName string `json:"component_name,omitempty"` + ScriptMinify bool `json:"scriptminify,omitempty"` + StyleMinify bool `json:"styleminify,omitempty"` } // Request is the struct for the request