diff --git a/share/types.go b/share/types.go index 85d811dc..e9d897a2 100644 --- a/share/types.go +++ b/share/types.go @@ -97,7 +97,8 @@ type Moapi struct { // Static setting 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 应用存储 diff --git a/sui/api/request.go b/sui/api/request.go index c84159a4..567a693e 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -270,7 +270,7 @@ func (r *Request) MakeCache() (*core.Cache, int, error) { } // Backend script - script, err := core.LoadScript(r.File) + script, err := core.LoadScript(r.File, true) if err != nil { return nil, 500, fmt.Errorf("script error, please re-complie the page %s", err.Error()) } diff --git a/sui/core/script.go b/sui/core/script.go index 779c5c60..51a89c9b 100644 --- a/sui/core/script.go +++ b/sui/core/script.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "path/filepath" "strings" "time" @@ -10,6 +11,7 @@ import ( "github.com/yaoapp/gou/application" v8 "github.com/yaoapp/gou/runtime/v8" "github.com/yaoapp/gou/runtime/v8/bridge" + "github.com/yaoapp/yao/share" ) // 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 { - file = base + ".js" + file = base + ".backend.js" } if exist, _ := application.App.Exists(file); !exist { @@ -81,6 +83,7 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) { return nil, err } + v8script.SourceRoots = getSourceRootReplaceFunc() script := &Script{Script: v8script} chScript <- &scriptData{base, script, saveScript} return script, nil @@ -217,3 +220,22 @@ func (script *Script) Helpers() ([]string, error) { 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 + } +} diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index f9800dc9..4a428c2e 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -692,7 +692,7 @@ func (page *Page) writeBackendScript(data map[string]interface{}) error { } 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) dir := filepath.Dir(scriptFileAbs) if exist, _ := os.Stat(dir); exist == nil {