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

chore: Refactor script.go to remove comments and improve TypeScript i… #202

Merged
merged 1 commit into from
Jun 15, 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
30 changes: 28 additions & 2 deletions runtime/v8/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func CLearModules() {
// TransformTS transform the typescript
func TransformTS(file string, source []byte) ([]byte, error) {

tsCode, err := tsImports(file, source)
tsCode, err := tsImports(file, removeCommentsAndKeepLines(source))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,6 +165,30 @@ type entry struct {
file string
}

func removeCommentsAndKeepLines(code []byte) []byte {
lines := strings.Split(string(code), "\n")
for i, line := range lines {
// Start with /*
if strings.HasPrefix(strings.TrimSpace(line), "/*") {
lines[i] = ""
for {
if strings.Contains(line, "*/") {
break
}
i++
line = lines[i]
lines[i] = ""
}
}

// Start with //
if strings.HasPrefix(strings.TrimSpace(line), "//") {
lines[i] = ""
}
}
return []byte(strings.Join(lines, "\n"))
}

func getEntryPoints(file string, tsCode string, loaded map[string]bool) (string, []entry, error) {
entryPoints := []entry{}
root := application.App.Root()
Expand Down Expand Up @@ -224,7 +248,8 @@ func loadModule(file string, tsCode string) error {
codes[entry.absfile] = entry.source
}

dir := filepath.Dir(absFile)
paths := strings.Split(file, string(os.PathSeparator))
dir := filepath.Join(root, paths[0]) // <app_root>/scripts, <app_root>/services, etc..
outdir := filepath.Join(string(os.PathSeparator), "outdir")

result := api.Build(api.BuildOptions{
Expand All @@ -237,6 +262,7 @@ func loadModule(file string, tsCode string) error {
".ts": api.LoaderTS,
},
Sourcemap: api.SourceMapExternal,
Outbase: dir,
Outdir: outdir,
Plugins: []api.Plugin{
{
Expand Down
Loading