Merge pull request #725 from trheyi/main

feat: Add sourceRoots configuration to app.yao, improve SUI backend script development experience
This commit is contained in:
Max 2024-08-01 19:22:47 +08:00 committed by GitHub
commit 990ccce28c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 28 additions and 5 deletions

View file

@ -97,7 +97,8 @@ type Moapi struct {
// Static setting // Static setting
type Static struct { type Static struct {
Rewrite []map[string]string `json:"rewrite,omitempty"` Rewrite []map[string]string `json:"rewrite,omitempty"`
SourceRoots map[string]string `json:"sourceRoots,omitempty"`
} }
// AppStorage 应用存储 // AppStorage 应用存储

View file

@ -270,7 +270,7 @@ func (r *Request) MakeCache() (*core.Cache, int, error) {
} }
// Backend script // Backend script
script, err := core.LoadScript(r.File) script, err := core.LoadScript(r.File, true)
if err != nil { if err != nil {
return nil, 500, fmt.Errorf("script error, please re-complie the page %s", err.Error()) return nil, 500, fmt.Errorf("script error, please re-complie the page %s", err.Error())
} }

View file

@ -2,6 +2,7 @@ package core
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"time" "time"
@ -10,6 +11,7 @@ import (
"github.com/yaoapp/gou/application" "github.com/yaoapp/gou/application"
v8 "github.com/yaoapp/gou/runtime/v8" v8 "github.com/yaoapp/gou/runtime/v8"
"github.com/yaoapp/gou/runtime/v8/bridge" "github.com/yaoapp/gou/runtime/v8/bridge"
"github.com/yaoapp/yao/share"
) )
// Scripts loaded scripts // Scripts loaded scripts
@ -62,9 +64,9 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) {
} }
} }
file = base + ".ts" file = base + ".backend.ts"
if exist, _ := application.App.Exists(file); !exist { if exist, _ := application.App.Exists(file); !exist {
file = base + ".js" file = base + ".backend.js"
} }
if exist, _ := application.App.Exists(file); !exist { if exist, _ := application.App.Exists(file); !exist {
@ -81,6 +83,7 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) {
return nil, err return nil, err
} }
v8script.SourceRoots = getSourceRootReplaceFunc()
script := &Script{Script: v8script} script := &Script{Script: v8script}
chScript <- &scriptData{base, script, saveScript} chScript <- &scriptData{base, script, saveScript}
return script, nil return script, nil
@ -217,3 +220,22 @@ func (script *Script) Helpers() ([]string, error) {
return nil, fmt.Errorf("helpers is %v should be []string", goValues) return nil, fmt.Errorf("helpers is %v should be []string", goValues)
} }
func getSourceRootReplaceFunc() interface{} {
if share.App.Static.SourceRoots == nil {
return nil
}
roots := share.App.Static.SourceRoots
return func(file string) string {
for name, mapping := range roots {
if strings.HasPrefix(file, name) {
path := mapping + strings.TrimPrefix(file, name)
base := filepath.Base(path)
name := strings.TrimSuffix(base, ".backend.ts")
dir := filepath.Dir(path)
return filepath.Join(dir, name, base)
}
}
return file
}
}

View file

@ -692,7 +692,7 @@ func (page *Page) writeBackendScript(data map[string]interface{}) error {
} }
ext := filepath.Ext(file) ext := filepath.Ext(file)
scriptFile := fmt.Sprintf("%s%s", page.publicFile(data), ext) scriptFile := fmt.Sprintf("%s.backend%s", page.publicFile(data), ext)
scriptFileAbs := filepath.Join(application.App.Root(), scriptFile) scriptFileAbs := filepath.Join(application.App.Root(), scriptFile)
dir := filepath.Dir(scriptFileAbs) dir := filepath.Dir(scriptFileAbs)
if exist, _ := os.Stat(dir); exist == nil { if exist, _ := os.Stat(dir); exist == nil {