Merge pull request #932 from trheyi/main
feat: Enhance global build context and component handling
This commit is contained in:
commit
fd84be9f84
8 changed files with 31 additions and 8 deletions
|
|
@ -164,7 +164,7 @@ func TemplateRender(process *process.Process) interface{} {
|
||||||
source := process.ArgsString(2)
|
source := process.ArgsString(2)
|
||||||
page := tmpl.CreatePage(source)
|
page := tmpl.CreatePage(source)
|
||||||
route := page.Get().Route
|
route := page.Get().Route
|
||||||
globalCtx := core.NewGlobalBuildContext()
|
globalCtx := core.NewGlobalBuildContext(tmpl)
|
||||||
suicode, _, err := page.Get().CompileAsComponent(core.NewBuildContext(globalCtx), &core.BuildOption{
|
suicode, _, err := page.Get().CompileAsComponent(core.NewBuildContext(globalCtx), &core.BuildOption{
|
||||||
PublicRoot: root,
|
PublicRoot: root,
|
||||||
AssetRoot: assetRoot,
|
AssetRoot: assetRoot,
|
||||||
|
|
|
||||||
|
|
@ -89,8 +89,27 @@ func (page *Page) Compile(ctx *BuildContext, option *BuildOption) (string, strin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Page Components
|
// Page Components
|
||||||
if ctx != nil && ctx.components != nil && len(ctx.components) > 0 {
|
if ctx != nil {
|
||||||
rawComponents, _ := jsoniter.MarshalToString(ctx.components)
|
components := map[string]string{}
|
||||||
|
for _, component := range ctx.GetComponents() {
|
||||||
|
components[component] = component
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Jit Components
|
||||||
|
if ctx.global != nil && ctx.global.tmpl != nil {
|
||||||
|
patterns := ctx.GetJitComponents()
|
||||||
|
|
||||||
|
// Get all pages
|
||||||
|
routes, err := ctx.global.tmpl.GlobRoutes(patterns, true)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", warnings, fmt.Errorf("Get jit components error: %s", err.Error())
|
||||||
|
}
|
||||||
|
for _, route := range routes {
|
||||||
|
components[route] = route
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rawComponents, _ := jsoniter.MarshalToString(components)
|
||||||
body.AppendHtml("\n\n" + `<script name="imports" type="json">` + "\n" + rawComponents + "\n</script>\n\n")
|
body.AppendHtml("\n\n" + `<script name="imports" type="json">` + "\n" + rawComponents + "\n</script>\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,10 @@ func NewTranslateContext() *TranslateContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGlobalBuildContext create a new global build context
|
// NewGlobalBuildContext create a new global build context
|
||||||
func NewGlobalBuildContext() *GlobalBuildContext {
|
func NewGlobalBuildContext(tmpl ITemplate) *GlobalBuildContext {
|
||||||
return &GlobalBuildContext{
|
return &GlobalBuildContext{
|
||||||
jitComponents: map[string]bool{},
|
jitComponents: map[string]bool{},
|
||||||
|
tmpl: tmpl,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,7 @@ func (data Data) ReplaceUse(tokens Tokens, value string) (string, []StringValue)
|
||||||
values = append(values, v)
|
values = append(values, v)
|
||||||
return v.Value
|
return v.Value
|
||||||
})
|
})
|
||||||
|
|
||||||
return res, values
|
return res, values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,8 @@ type ITemplate interface {
|
||||||
ExecAfterBuildScripts() []TemplateScirptResult
|
ExecAfterBuildScripts() []TemplateScirptResult
|
||||||
|
|
||||||
Trans(option *BuildOption) ([]string, error)
|
Trans(option *BuildOption) ([]string, error)
|
||||||
|
|
||||||
|
GlobRoutes(patterns []string, unique ...bool) ([]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPage is the interface for the page
|
// IPage is the interface for the page
|
||||||
|
|
|
||||||
|
|
@ -292,7 +292,7 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
|
||||||
func (parser *TemplateParser) parseElementComponent(sel *goquery.Selection) {
|
func (parser *TemplateParser) parseElementComponent(sel *goquery.Selection) {
|
||||||
|
|
||||||
parser.parsed(sel)
|
parser.parsed(sel)
|
||||||
com := sel.AttrOr("s:cn", "")
|
com := sel.AttrOr("s:route", "")
|
||||||
props := map[string]interface{}{}
|
props := map[string]interface{}{}
|
||||||
for _, attr := range sel.Nodes[0].Attr {
|
for _, attr := range sel.Nodes[0].Attr {
|
||||||
if strings.HasPrefix(attr.Key, "prop:") {
|
if strings.HasPrefix(attr.Key, "prop:") {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
var propTokens = Tokens{
|
var propTokens = Tokens{
|
||||||
{start: "{%", end: "%}"}, // {% xxx %}
|
{start: "{%", end: "%}"}, // {% xxx %}
|
||||||
{start: "[{", end: "}]"}, // [{ xxx }]
|
// {start: "[{", end: "}]"}, // [{ xxx }] Deprecated
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataTokens = Tokens{
|
var dataTokens = Tokens{
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ func (tmpl *Template) Build(option *core.BuildOption) ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build all pages
|
// Build all pages
|
||||||
ctx := core.NewGlobalBuildContext()
|
ctx := core.NewGlobalBuildContext(tmpl)
|
||||||
pages, err := tmpl.Pages()
|
pages, err := tmpl.Pages()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return warnings, err
|
return warnings, err
|
||||||
|
|
@ -149,7 +149,7 @@ func (tmpl *Template) Trans(option *core.BuildOption) ([]string, error) {
|
||||||
var err error
|
var err error
|
||||||
warnings := []string{}
|
warnings := []string{}
|
||||||
|
|
||||||
ctx := core.NewGlobalBuildContext()
|
ctx := core.NewGlobalBuildContext(tmpl)
|
||||||
pages, err := tmpl.Pages()
|
pages, err := tmpl.Pages()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return warnings, err
|
return warnings, err
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue