From fc1f80e0f5daaf3d4494d703674ccf98b73c54c9 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 1 Aug 2024 18:16:59 +0800 Subject: [PATCH 1/3] feat: Add sourceRoots to Static struct in types.go --- share/types.go | 3 ++- sui/api/request.go | 2 +- sui/core/script.go | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) 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..bc1293e0 100644 --- a/sui/core/script.go +++ b/sui/core/script.go @@ -10,6 +10,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 @@ -81,6 +82,8 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) { return nil, err } + v8script.SourceRoots = share.App.Static.SourceRoots + script := &Script{Script: v8script} chScript <- &scriptData{base, script, saveScript} return script, nil From 44eb60377df706785910701808ecf111dbb3ef4d Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 1 Aug 2024 18:43:15 +0800 Subject: [PATCH 2/3] Update file extensions for backend scripts --- sui/core/script.go | 6 ++++-- sui/storages/local/build.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sui/core/script.go b/sui/core/script.go index bc1293e0..3db7c949 100644 --- a/sui/core/script.go +++ b/sui/core/script.go @@ -63,9 +63,11 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) { } } - file = base + ".ts" + file = base + ".backend.ts" + fmt.Println(file) + if exist, _ := application.App.Exists(file); !exist { - file = base + ".js" + file = base + ".backend.js" } if exist, _ := application.App.Exists(file); !exist { 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 { From 83e1081c9b170d9787d90db89a4029edb45f2946 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 1 Aug 2024 19:11:27 +0800 Subject: [PATCH 3/3] feat: Update script loading to handle source root mapping from tsconfig.json --- sui/core/script.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/sui/core/script.go b/sui/core/script.go index 3db7c949..51a89c9b 100644 --- a/sui/core/script.go +++ b/sui/core/script.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "path/filepath" "strings" "time" @@ -64,8 +65,6 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) { } file = base + ".backend.ts" - fmt.Println(file) - if exist, _ := application.App.Exists(file); !exist { file = base + ".backend.js" } @@ -84,8 +83,7 @@ func LoadScript(file string, disableCache ...bool) (*Script, error) { return nil, err } - v8script.SourceRoots = share.App.Static.SourceRoots - + v8script.SourceRoots = getSourceRootReplaceFunc() script := &Script{Script: v8script} chScript <- &scriptData{base, script, saveScript} return script, nil @@ -222,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 + } +}