Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Update script.go to improve TypeScript import handling and … #210

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading