Merge pull request #635 from trheyi/main
refactor: Minify script and style code during SUI page build
This commit is contained in:
commit
f15f5fa342
3 changed files with 14 additions and 9 deletions
|
|
@ -54,14 +54,14 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add Style
|
// Add Style
|
||||||
_, err = page.BuildStyle(ctx, option)
|
style, err := page.BuildStyle(ctx, option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnings = append(warnings, err.Error())
|
warnings = append(warnings, err.Error())
|
||||||
}
|
}
|
||||||
doc.Selection.Find("head").AppendHtml(fmt.Sprintf("\n"+`<style type="text/css">`+"\n%s\n"+`</style>`+"\n", strings.Join(ctx.styles, "\n")))
|
doc.Selection.Find("head").AppendHtml(fmt.Sprintf("\n"+`<style type="text/css">`+"\n%s\n"+`</style>`+"\n%s\n", strings.Join(ctx.styles, "\n"), style))
|
||||||
|
|
||||||
// Add Script
|
// Add Script
|
||||||
_, scripts, err := page.BuildScript(ctx, option, option.Namespace)
|
code, scripts, err := page.BuildScript(ctx, option, option.Namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnings = append(warnings, err.Error())
|
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" + `<script src="` + script + `"></script>` + "\n")
|
doc.Selection.Find("body").AppendHtml("\n" + `<script src="` + script + `"></script>` + "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doc.Selection.Find("body").AppendHtml(fmt.Sprintf("\n"+`<script type="text/javascript">`+"\n%s\n"+`</script>`+"\n", strings.Join(ctx.scripts, "\n")))
|
doc.Selection.Find("body").AppendHtml(fmt.Sprintf("\n"+`<script type="text/javascript">`+"\n%s\n"+`</script>`+"\n%s\n", strings.Join(ctx.scripts, "\n"), code))
|
||||||
|
|
||||||
return doc, warnings, nil
|
return doc, warnings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -249,6 +248,8 @@ func (page *Page) parse(ctx *BuildContext, doc *goquery.Document, option *BuildO
|
||||||
IgnoreDocument: true,
|
IgnoreDocument: true,
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
ComponentName: componentName,
|
ComponentName: componentName,
|
||||||
|
ScriptMinify: true,
|
||||||
|
StyleMinify: true,
|
||||||
}, slots, attrs)
|
}, slots, attrs)
|
||||||
|
|
||||||
// append translations
|
// append translations
|
||||||
|
|
@ -365,7 +366,7 @@ func (page *Page) BuildScript(ctx *BuildContext, option *BuildOption, namespace
|
||||||
|
|
||||||
// TypeScript
|
// TypeScript
|
||||||
if page.Codes.TS.Code != "" {
|
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 {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -398,7 +399,7 @@ func (page *Page) BuildScript(ctx *BuildContext, option *BuildOption, namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
// JavaScript
|
// 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 {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,9 @@ func (page *Page) CompileJS(source []byte, minify bool) ([]byte, []string, error
|
||||||
minified, err := transform.MinifyJS(jsCode, api.ES2015)
|
minified, err := transform.MinifyJS(jsCode, api.ES2015)
|
||||||
return []byte(minified), scripts, err
|
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
|
// CompileTS compile the typescript
|
||||||
|
|
@ -108,7 +110,7 @@ func (page *Page) CompileTS(source []byte, minify bool) ([]byte, []string, error
|
||||||
return []byte(jsCode), scripts, err
|
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
|
return []byte(jsCode), scripts, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -208,6 +208,8 @@ type BuildOption struct {
|
||||||
Namespace string `json:"namespace,omitempty"`
|
Namespace string `json:"namespace,omitempty"`
|
||||||
Data map[string]interface{} `json:"data,omitempty"`
|
Data map[string]interface{} `json:"data,omitempty"`
|
||||||
ComponentName string `json:"component_name,omitempty"`
|
ComponentName string `json:"component_name,omitempty"`
|
||||||
|
ScriptMinify bool `json:"scriptminify,omitempty"`
|
||||||
|
StyleMinify bool `json:"styleminify,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request is the struct for the request
|
// Request is the struct for the request
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue