From 42b42e1134afde80472a987f888c44618afda204 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Jul 2024 11:11:22 +0800 Subject: [PATCH] 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"}