feat: Add support for directory mapping from tsconfig in TypeScript import

This commit is contained in:
Max 2024-07-31 11:11:22 +08:00
parent f451eb12a8
commit 42b42e1134
3 changed files with 23 additions and 5 deletions

View file

@ -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.
}

View file

@ -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

View file

@ -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"}