Skip to content

Commit

Permalink
cli: improve loader cold start
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Nov 13, 2024
1 parent 8d7bd42 commit bf479b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/internal/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async function unocss(config, content, id) {
const generatorKey = config?.filename ?? ".";
let uno = unoGenerators.get(generatorKey);
if (!uno || uno.configCSS !== config?.css) {
uno = import("npm:@esm.sh/[email protected].1").then(({ init }) => init(config?.css));
uno = import("npm:@esm.sh/[email protected].2").then(({ init }) => init(config?.css));
uno.configCSS = config?.css;
unoGenerators.set(generatorKey, uno);
}
Expand Down
29 changes: 17 additions & 12 deletions cli/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type LoaderWorker struct {
outReader *bufio.Reader
}

func (lw *LoaderWorker) Start(loaderjs []byte) (err error) {
func (l *LoaderWorker) Start(loaderjs []byte) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
Expand All @@ -41,35 +41,40 @@ func (lw *LoaderWorker) Start(loaderjs []byte) (err error) {
return
}
}

denoPath, err := getDenoPath()
if err != nil {
err = errors.New("deno not found, please install deno first")
return
}

cmd := exec.Command(denoPath, "run", "--no-lock", "-A", jsPath)
cmd.Stdin, lw.stdin = io.Pipe()
lw.stdout, cmd.Stdout = io.Pipe()
cmd.Stdin, l.stdin = io.Pipe()
l.stdout, cmd.Stdout = io.Pipe()
err = cmd.Start()
if err != nil {
lw.stdin = nil
lw.stdout = nil
l.stdin = nil
l.stdout = nil
} else {
lw.outReader = bufio.NewReader(lw.stdout)
l.outReader = bufio.NewReader(l.stdout)
if os.Getenv("DEBUG") == "1" {
denoVersion, _ := exec.Command(denoPath, "-v").Output()
fmt.Println(term.Dim(fmt.Sprintf("[debug] loader process started (runtime: %s)", strings.TrimSpace(string(denoVersion)))))
}
}

// pre-install npm deps
cmd = exec.Command(denoPath, "cache", "npm:@esm.sh/[email protected]", "npm:@esm.sh/[email protected]", "npm:@esm.sh/[email protected]")
cmd.Start()
return
}

func (lw *LoaderWorker) Load(loaderType string, args []any) (lang string, code string, err error) {
func (l *LoaderWorker) Load(loaderType string, args []any) (lang string, code string, err error) {
// only one load can be invoked at a time
lw.lock.Lock()
defer lw.lock.Unlock()
l.lock.Lock()
defer l.lock.Unlock()

if lw.outReader == nil {
if l.outReader == nil {
err = errors.New("loader not started")
return
}
Expand All @@ -88,13 +93,13 @@ func (lw *LoaderWorker) Load(loaderType string, args []any) (lang string, code s
loaderArgs := make([]any, len(args)+1)
loaderArgs[0] = loaderType
copy(loaderArgs[1:], args)
err = json.NewEncoder(lw.stdin).Encode(loaderArgs)
err = json.NewEncoder(l.stdin).Encode(loaderArgs)
if err != nil {
return
}
for {
var line []byte
line, err = lw.outReader.ReadBytes('\n')
line, err = l.outReader.ReadBytes('\n')
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func transformSvelte(npmrc *NpmRC, importMap common.ImportMap, args []string) (o
}

func generateUnoCSS(npmrc *NpmRC, args []string) (output *LoaderOutput, err error) {
return runLoader(npmrc, "unocss", args, PackageId{"@esm.sh/unocss", "0.2.1"}, "@iconify/[email protected].269")
return runLoader(npmrc, "unocss", args, PackageId{"@esm.sh/unocss", "0.2.2"}, "@iconify/[email protected].271")
}

0 comments on commit bf479b8

Please sign in to comment.