yao/sui/core/context.go
2024-07-07 01:19:15 +08:00

65 lines
1.7 KiB
Go

package core
// NewBuildContext create a new build context
func NewBuildContext(global *GlobalBuildContext) *BuildContext {
return &BuildContext{
components: map[string]string{},
sequence: 1,
scripts: []ScriptNode{},
scriptUnique: map[string]bool{},
styles: []StyleNode{},
styleUnique: map[string]bool{},
jitComponents: map[string]bool{},
global: global,
warnings: []string{},
visited: map[string]int{},
stack: []string{},
}
}
// NewGlobalBuildContext create a new global build context
func NewGlobalBuildContext() *GlobalBuildContext {
return &GlobalBuildContext{
jitComponents: map[string]bool{},
}
}
// GetJitComponents get the just in time components
func (ctx *BuildContext) GetJitComponents() []string {
if ctx.jitComponents == nil {
return []string{}
}
jitComponents := []string{}
for name := range ctx.jitComponents {
jitComponents = append(jitComponents, name)
}
return jitComponents
}
// GetJitComponents get the just in time components
func (globalCtx *GlobalBuildContext) GetJitComponents() []string {
if globalCtx.jitComponents == nil {
return []string{}
}
jitComponents := []string{}
for name := range globalCtx.jitComponents {
jitComponents = append(jitComponents, name)
}
return jitComponents
}
func (ctx *BuildContext) addJitComponent(name string) {
name = stmtRe.ReplaceAllString(name, "*")
name = propRe.ReplaceAllString(name, "*")
ctx.jitComponents[name] = true
if ctx.global != nil {
ctx.global.jitComponents[name] = true
}
}
func (ctx *BuildContext) isJitComponent(name string) bool {
hasStmt := stmtRe.MatchString(name)
hasProp := propRe.MatchString(name)
return hasStmt || hasProp
}