From 94d84f51f1a29cf66962b2ebd38284b273754528 Mon Sep 17 00:00:00 2001 From: nvms Date: Mon, 14 Oct 2024 09:04:20 -0400 Subject: [PATCH] runner fixes --- internal/esr/init.go | 3 +- internal/esr/mode.go | 107 ++++++++++++++++++++++++++--------------- internal/esr/runner.go | 26 ++++++++-- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/internal/esr/init.go b/internal/esr/init.go index 0fee375..66911d4 100644 --- a/internal/esr/init.go +++ b/internal/esr/init.go @@ -120,8 +120,7 @@ func InitProject() { "compilerOptions": { "module": "ES2022", "target": "ESNext", - "experimentalDecorators": true, - "emitDecoratorMetadata": true + "allowSyntheticDefaultImports": true } }`), 0644); err != nil { Die("failed to write tsconfig.json: %v", err) diff --git a/internal/esr/mode.go b/internal/esr/mode.go index cb9963b..77f202f 100644 --- a/internal/esr/mode.go +++ b/internal/esr/mode.go @@ -29,7 +29,7 @@ func ModeOptions(config *config.Config, mode ModeType, entryPoint, tempFilePath } return api.SourceMapNone } - + switch mode { case ModeBuilder: buildOptions.Bundle = config.Build.Bundle @@ -96,46 +96,73 @@ func ModeOptions(config *config.Config, mode ModeType, entryPoint, tempFilePath fmt.Printf("Unknown JSX: %s, using default\n", config.JSX) } - // Apply loader - buildOptions.Loader = make(map[string]api.Loader) - for k, v := range config.Loader { - switch v { - case "file": - buildOptions.Loader[k] = api.LoaderFile - case "dataurl": - buildOptions.Loader[k] = api.LoaderDataURL - case "binary": - buildOptions.Loader[k] = api.LoaderBinary - case "base64": - buildOptions.Loader[k] = api.LoaderBase64 - case "copy": - buildOptions.Loader[k] = api.LoaderCopy - case "text": - buildOptions.Loader[k] = api.LoaderText - case "js": - buildOptions.Loader[k] = api.LoaderJS - case "jsx": - buildOptions.Loader[k] = api.LoaderJSX - case "tsx": - buildOptions.Loader[k] = api.LoaderTSX - case "ts": - buildOptions.Loader[k] = api.LoaderTS - case "json": - buildOptions.Loader[k] = api.LoaderJSON - case "css": - buildOptions.Loader[k] = api.LoaderCSS - case "globalcss": - buildOptions.Loader[k] = api.LoaderGlobalCSS - case "localcss": - buildOptions.Loader[k] = api.LoaderLocalCSS - case "empty": - buildOptions.Loader[k] = api.LoaderEmpty - case "default": - buildOptions.Loader[k] = api.LoaderDefault - default: - buildOptions.Loader[k] = api.LoaderNone - } + var loaders = map[string]api.Loader{ + ".js": api.LoaderJS, + ".mjs": api.LoaderJS, + ".cjs": api.LoaderJS, + ".jsx": api.LoaderJSX, + ".ts": api.LoaderTS, + ".tsx": api.LoaderTSX, + ".mts": api.LoaderTS, + ".css": api.LoaderCSS, + ".json": api.LoaderJSON, + ".txt": api.LoaderText, + ".html": api.LoaderText, + ".md": api.LoaderText, + ".svg": api.LoaderDataURL, + ".png": api.LoaderDataURL, + ".webp": api.LoaderDataURL, + ".gif": api.LoaderDataURL, + ".ttf": api.LoaderDataURL, + ".eot": api.LoaderDataURL, + ".woff": api.LoaderDataURL, + ".woff2": api.LoaderDataURL, } + buildOptions.Loader = loaders + + // Apply loader + // buildOptions.Loader = make(map[string]api.Loader) + // for k, v := range config.Loader { + // switch v { + // case ".woff2": + // buildOptions.Loader[k] = api.LoaderDataURL + // case "file": + // buildOptions.Loader[k] = api.LoaderFile + // case "dataurl": + // buildOptions.Loader[k] = api.LoaderDataURL + // case "binary": + // buildOptions.Loader[k] = api.LoaderBinary + // case "base64": + // buildOptions.Loader[k] = api.LoaderBase64 + // case "copy": + // buildOptions.Loader[k] = api.LoaderCopy + // case "text": + // buildOptions.Loader[k] = api.LoaderText + // case "js": + // buildOptions.Loader[k] = api.LoaderJS + // case "jsx": + // buildOptions.Loader[k] = api.LoaderJSX + // case "tsx": + // buildOptions.Loader[k] = api.LoaderTSX + // case "ts": + // buildOptions.Loader[k] = api.LoaderTS + // case "json": + // buildOptions.Loader[k] = api.LoaderJSON + // case "css": + // buildOptions.Loader[k] = api.LoaderCSS + // case "globalcss": + // buildOptions.Loader[k] = api.LoaderGlobalCSS + // case "localcss": + // buildOptions.Loader[k] = api.LoaderLocalCSS + // case "empty": + // buildOptions.Loader[k] = api.LoaderEmpty + // case "default": + // buildOptions.Loader[k] = api.LoaderDefault + // default: + // buildOptions.Loader[k] = api.LoaderNone + // } + // } + return &buildOptions } diff --git a/internal/esr/runner.go b/internal/esr/runner.go index 4e1b5e5..9dc6253 100644 --- a/internal/esr/runner.go +++ b/internal/esr/runner.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "sync" "syscall" "time" ) @@ -22,14 +23,18 @@ func NewRunner(c *config.Config, b *Builder, e string) *Runner { Config: c, EntryPoint: e, Builder: b, - TempFilePath: newTempFilePath(), + TempFilePath: newTempFilePath(e), } } -func newTempFilePath() string { +func newTempFilePath(entry string) string { timestamp := time.Now().UnixNano() - tempDir := os.TempDir() - return filepath.Join(tempDir, fmt.Sprintf("esr-%d.mjs", timestamp)) + // tempDir := os.TempDir() + // return filepath.Join(tempDir, fmt.Sprintf("esr-%d.mjs", timestamp)) + return filepath.Join( + filepath.Dir(entry), + fmt.Sprintf("esr-%d.mjs", timestamp), + ) } func (r *Runner) Run(entryPoint string) { @@ -44,18 +49,31 @@ func (r *Runner) Run(entryPoint string) { cmd := exec.Command(r.Config.Run.Runtime, r.TempFilePath) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout + if err := cmd.Start(); err != nil { fmt.Println("Error starting process:", err) os.Exit(1) } + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() if err := cmd.Wait(); err != nil { fmt.Printf("Process finished with error: %v\n", err) } else { fmt.Println("Process finished successfully") } }() + + wg.Wait() + + // Finally, remove the temp file we created + + if err := os.Remove(r.TempFilePath); err != nil { + fmt.Printf("Failed to remove temp file: %v\n", err) + } } func (r *Runner) Stop(cmd *exec.Cmd) error {