Skip to content

Commit

Permalink
refactor: Update script.go to improve TypeScript import handling and …
Browse files Browse the repository at this point in the history
…add internal keep modules
  • Loading branch information
trheyi committed Jul 11, 2024
1 parent 1c3dc13 commit 68b914a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions runtime/v8/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 41,8 @@ var RootScripts = map[string]*Script{}
var importRe = regexp.MustCompile(`import\s \t*\n*(\*\s as\s \w |\{[^}] \}|\w )\s from\s ["']([^"'] )["'];?`)
var exportRe = regexp.MustCompile(`export\s (default|function|class|const|var|let)\s `)

var internalKeepModules = []string{"/yao.ts", "/yao", "/gou", "/gou.ts"}
var internalKeepModuleSuffixes = []string{"/yao.ts", "/yao", "/gou", "/gou.ts"}
var internalKeepModules = []string{"@yao", "@yaoapps", "@yaoapp", "@gou"}

// NewScript create a new script
func NewScript(file string, id string, timeout ...time.Duration) *Script {
Expand Down Expand Up @@ -339,7 340,7 @@ func replaceImportCode(file string, source []byte) (string, []Import, error) {
importClause, importPath := matches[1], matches[2]

// Filter the internal keep modules
for _, keep := range internalKeepModules {
for _, keep := range internalKeepModuleSuffixes {
if strings.HasSuffix(importPath, keep) {
lines := strings.Split(m, "\n")
for i, line := range lines {
Expand All @@ -349,6 350,15 @@ func replaceImportCode(file string, source []byte) (string, []Import, error) {
return strings.Join(lines, "\n")
}
}
for _, keep := range internalKeepModules {
if strings.HasPrefix(importPath, keep) {
lines := strings.Split(m, "\n")
for i, line := range lines {
lines[i] = "// " line
}
return strings.Join(lines, "\n")
}
}

relImportPath, err := getImportPath(file, importPath)
if err != nil {
Expand Down

0 comments on commit 68b914a

Please sign in to comment.