Skip to content

Commit

Permalink
Merge pull request #217 from trheyi/main
Browse files Browse the repository at this point in the history
refactor: Update script.go to improve TypeScript import handling and …
  • Loading branch information
trheyi authored Jul 31, 2024
2 parents 9c61499 + a17693a commit e62a7a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
6 changes: 5 additions & 1 deletion runtime/v8/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,17 @@ func replaceImportCode(file string, source []byte) (string, []Import, error) {

func getImportPath(file string, path string) (string, error) {

var tsfile string
var fromTsConfig bool = false
if runtimeOption.TSConfig != nil {
var err error
path, fromTsConfig, err = runtimeOption.TSConfig.GetFileName(path)
tsfile, fromTsConfig, err = runtimeOption.TSConfig.GetFileName(path)
if err != nil {
return "", err
}
if fromTsConfig {
file = tsfile
}
}

if !fromTsConfig {
Expand Down
45 changes: 27 additions & 18 deletions runtime/v8/tsconfig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v8

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -19,36 +20,44 @@ func (tsconfg *TSConfig) GetFileName(path string) (string, bool, error) {
}

for pattern, paths := range tsconfg.CompilerOptions.Paths {

match, err := filepath.Match(pattern, path)
if err != nil {
return path, false, nil
}

if match {
if tsconfg.Match(pattern, path) {
f := tsconfg.ReplacePattern(path, pattern)
for _, p := range paths {
matches, err := application.App.Glob(p)
if err != nil {
return path, false, err
}
for _, file := range matches {
if strings.HasSuffix(file, f) {
return file, true, nil
dir := filepath.Clean(filepath.Dir(p))
f = filepath.Join(dir, f)
err := application.App.Walk(dir, func(root, filename string, isdir bool) error {
if isdir {
return nil
}
if filename == f {
return fmt.Errorf("Found")
}
return nil
}, "*.ts")

if err == nil {
return path, false, nil
}

if err.Error() == "Found" {
return f, true, nil
}
}
}

}

return path, false, nil
}

// Match match the pattern
func (tsconfg *TSConfig) Match(pattern, path string) bool {
prefix := strings.Split(pattern, "/*")[0] + string(os.PathSeparator)
return strings.HasPrefix(path, prefix)
}

// ReplacePattern replace the pattern
func (tsconfg *TSConfig) ReplacePattern(path, pattern string) string {
dir := filepath.Clean(filepath.Dir(path)) + string(os.PathSeparator)
file := strings.TrimLeft(path, dir)
prefix := strings.Split(pattern, "/*")[0]
file := strings.TrimPrefix(path, prefix)
if strings.HasSuffix(file, ".ts") {
return file
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/v8/tsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func TestTSConfigGetFileName(t *testing.T) {
t.Fatal("not match")
}

assert.Equal(t, "/scripts/runtime/ts/lib/foo.ts", file)
assert.Equal(t, "scripts/runtime/ts/lib/foo.ts", file)

}

0 comments on commit e62a7a5

Please sign in to comment.