From 42b42e1134afde80472a987f888c44618afda204 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Jul 2024 11:11:22 +0800 Subject: [PATCH 1/3] feat: Add support for directory mapping from tsconfig in TypeScript import --- config/types.go | 5 +---- runtime/runtime.go | 21 +++++++++++++++++++++ script/script.go | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/config/types.go b/config/types.go index 58df739d..6a57a152 100644 --- a/config/types.go +++ b/config/types.go @@ -65,8 +65,5 @@ type Runtime struct { HeapSizeRelease uint64 `json:"heapSizeRelease,omitempty" env:"YAO_RUNTIME_HEAP_RELEASE" envDefault:"52428800"` // the isolate will be re-created when reaching this value, and the default value is 52428800 (50M) HeapAvailableSize uint64 `json:"heapAvailableSize,omitempty" env:"YAO_RUNTIME_HEAP_AVAILABLE" envDefault:"524288000"` // the isolate will be re-created when the available size is smaller than this value, and the default value is 524288000 (500M) Precompile bool `json:"precompile,omitempty" env:"YAO_RUNTIME_PRECOMPILE" envDefault:"false"` // if true compile scripts when the VM is created. this will increase the load time, but the script will run faster. the default value is false - - // The following options are experimental features and not stable. - // They may be removed once the features become stable. Please do not use them in a production environment. - Import bool `json:"import,omitempty" env:"YAO_RUNTIME_IMPORT" envDefault:"false"` // If true, TypeScript import will be enabled. Default value is false. + Import bool `json:"import,omitempty" env:"YAO_RUNTIME_IMPORT" envDefault:"true"` // If false the import statement will be disabled, the default value is true. } diff --git a/runtime/runtime.go b/runtime/runtime.go index 6986ade4..eb320ca8 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1,6 +1,10 @@ package runtime import ( + "fmt" + + jsoniter "github.com/json-iterator/go" + "github.com/yaoapp/gou/application" v8 "github.com/yaoapp/gou/runtime/v8" "github.com/yaoapp/yao/config" ) @@ -22,6 +26,23 @@ func Start(cfg config.Config) error { Import: cfg.Runtime.Import, } + // Read the tsconfig.json + if cfg.Runtime.Import { + if exist, _ := application.App.Exists("tsconfig.json"); exist { + var tsconfig v8.TSConfig + raw, err := application.App.Read("tsconfig.json") + if err != nil { + return fmt.Errorf("tsconfig.json is not a valid json file %s", err) + } + + err = jsoniter.Unmarshal(raw, &tsconfig) + if err != nil { + return fmt.Errorf("tsconfig.json is not a valid json file %s", err) + } + option.TSConfig = &tsconfig + } + } + err := v8.Start(option) if err != nil { return err diff --git a/script/script.go b/script/script.go index 68cfd3dd..f983e843 100644 --- a/script/script.go +++ b/script/script.go @@ -9,7 +9,7 @@ import ( "github.com/yaoapp/yao/share" ) -// Load 加载共享库 +// Load load all scripts and services func Load(cfg config.Config) error { v8.CLearModules() exts := []string{"*.js", "*.ts"} From a0729bbde08d67a57ed2e9ddd597f959a86d4023 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Jul 2024 11:20:04 +0800 Subject: [PATCH 2/3] feat: Improve tsconfig.json import handling in runtime.Start function --- runtime/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index eb320ca8..d6013b9f 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -27,7 +27,7 @@ func Start(cfg config.Config) error { } // Read the tsconfig.json - if cfg.Runtime.Import { + if cfg.Runtime.Import && application.App != nil { if exist, _ := application.App.Exists("tsconfig.json"); exist { var tsconfig v8.TSConfig raw, err := application.App.Read("tsconfig.json") From 4a0a403e7e73151939e7a9988ba2e0221900e7a9 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Jul 2024 11:26:03 +0800 Subject: [PATCH 3/3] feat: Add testPrepare function for initializing test environment --- runtime/runtime_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index 3adf0664..17465030 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -1,15 +1,36 @@ package runtime import ( + "os" "testing" + "github.com/yaoapp/gou/application" "github.com/yaoapp/yao/config" ) func TestStart(t *testing.T) { + testPrepare(t) defer Stop() err := Start(config.Conf) if err != nil { t.Fatal(err) } } + +func testPrepare(t *testing.T, rootEnv ...string) { + + appRootEnv := "YAO_TEST_APPLICATION" + if len(rootEnv) > 0 { + appRootEnv = rootEnv[0] + } + + root := os.Getenv(appRootEnv) + var app application.Application + var err error + + app, err = application.OpenFromDisk(root) // Load app from Disk + if err != nil { + t.Fatal(err) + } + application.Load(app) +}