From 6c140758da2b49ff9eff204e27b370276cbb15e5 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 28 Sep 2023 18:05:36 +0700 Subject: internal: replace esbuild api.Build with api.BuildContext Using api.BuildContext [1] is more efficient than building again because some of the data from the previous build is cached and can be reused if the original files haven't changed since the previous build. While at it, we update the github.com/evanw/esbuild module to latest release. [1] https://esbuild.github.io/api/#rebuild --- go.mod | 2 +- go.sum | 4 +-- internal/internal.go | 79 +++++++++++++++++++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index 0d1a174..026fa16 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ go 1.20 require ( git.sr.ht/~shulhan/ciigo v0.10.0 - github.com/evanw/esbuild v0.19.3 + github.com/evanw/esbuild v0.19.4 github.com/shuLhan/share v0.49.2-0.20230927031716-fa2510a8d458 ) diff --git a/go.sum b/go.sum index 3b9152c..98858fd 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ git.sr.ht/~shulhan/asciidoctor-go v0.5.0 h1:TfcAjv+7EwBZ83ef8OhX9vfQ4vRFcaJh0P1X git.sr.ht/~shulhan/asciidoctor-go v0.5.0/go.mod h1:RHAfgO1CU1It8Gsz6eqDBi8HhT4wxhCS1dKO8U9vIgM= git.sr.ht/~shulhan/ciigo v0.10.0 h1:s1SJ3/NzBcbOLmEZ4z1Cx9Vf7ZdDIvm45b7KMCZKzEY= git.sr.ht/~shulhan/ciigo v0.10.0/go.mod h1:cG6av+ywJZZp96F43kmLB2QWjm2hYiahbsbeTX/vlgk= -github.com/evanw/esbuild v0.19.3 h1:foPr0xwQM3lBWKBtscauTN9FrmJzRDVI2+EGOs82H/I= -github.com/evanw/esbuild v0.19.3/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk= +github.com/evanw/esbuild v0.19.4 h1:Etk+6ZCjtNxZZLEgMKSqpO0/oM0k1WYKJabaPMJ39iQ= +github.com/evanw/esbuild v0.19.4/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk= github.com/shuLhan/share v0.49.2-0.20230927031716-fa2510a8d458 h1:TqZptxsESZGZJbSWDVVUwIrGzvF4ZdgSz5WLOZH+Yrk= github.com/shuLhan/share v0.49.2-0.20230927031716-fa2510a8d458/go.mod h1:YEwLUwwBpDqPn5yxfdb0B8eFP04SKQxv5gMQwB4A+c4= github.com/yuin/goldmark v1.5.6 h1:COmQAWTCcGetChm3Ig7G/t8AFAN00t+o8Mt4cf7JpwA= diff --git a/internal/internal.go b/internal/internal.go index f14029d..38da779 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -10,6 +10,7 @@ import ( "git.sr.ht/~shulhan/ciigo" "github.com/evanw/esbuild/pkg/api" + esapi "github.com/evanw/esbuild/pkg/api" "github.com/shuLhan/share/lib/memfs" "github.com/shuLhan/share/lib/mlog" ) @@ -25,9 +26,20 @@ var MemfsWww *memfs.MemFS // Build compile all TypeScript files inside _www into JavaScript and embed // them into memfs_www.go. func Build() (err error) { - var logp = `Build` + var ( + logp = `Build` + + esctx esapi.BuildContext + ) - err = buildTypeScript(nil) + esctx, err = newEsbuild() + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + defer esctx.Dispose() + + err = buildTypeScript(esctx) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } @@ -72,6 +84,15 @@ func Watch() { log.Fatalf(`%s: %s`, logp, err) } + var esctx esapi.BuildContext + + esctx, err = newEsbuild() + if err != nil { + log.Fatalf(`%s: %s`, logp, err) + } + + defer esctx.Dispose() + if MemfsWww == nil { err = initMemfsWww() if err != nil { @@ -93,20 +114,13 @@ func Watch() { } var ( - buildTicker = time.NewTicker(3 * time.Second) - esBuildOptions = api.BuildOptions{ - EntryPoints: []string{`_www/main.ts`}, - Platform: api.PlatformBrowser, - Outfile: `_www/main.js`, - GlobalName: `awwan`, - Bundle: true, - Write: true, - } + buildTicker = time.NewTicker(3 * time.Second) ns memfs.NodeState tsCount int embedCount int ) + for { select { case ns = <-dw.C: @@ -141,7 +155,7 @@ func Watch() { case <-buildTicker.C: if tsCount > 0 { - err = buildTypeScript(&esBuildOptions) + err = buildTypeScript(esctx) if err != nil { mlog.Errf(`%s`, err) } else { @@ -161,27 +175,16 @@ func Watch() { } } -func buildTypeScript(esBuildOptions *api.BuildOptions) (err error) { +func buildTypeScript(esctx esapi.BuildContext) (err error) { var logp = `buildTypeScript` - if esBuildOptions == nil { - esBuildOptions = &api.BuildOptions{ - EntryPoints: []string{`_www/main.ts`}, - Platform: api.PlatformBrowser, - Outfile: `_www/main.js`, - GlobalName: `app`, - Bundle: true, - Write: true, - } - } - - var buildResult = api.Build(*esBuildOptions) + var buildResult = esctx.Rebuild() if len(buildResult.Errors) == 0 { return nil } var ( - msg api.Message + msg esapi.Message x int ) for x, msg = range buildResult.Errors { @@ -234,3 +237,27 @@ func initMemfsWww() (err error) { } return nil } + +// newEsbuild create new [esapi.BuildContext] for transpiling TypeScript with +// esbuild. +func newEsbuild() (esctx api.BuildContext, err error) { + var ( + esBuildOptions = esapi.BuildOptions{ + EntryPoints: []string{`_www/main.ts`}, + Platform: esapi.PlatformBrowser, + Outfile: `_www/main.js`, + GlobalName: `awwan`, + Bundle: true, + Write: true, + } + + errctx *esapi.ContextError + ) + + esctx, errctx = esapi.Context(esBuildOptions) + if errctx != nil { + return nil, fmt.Errorf(`newEsbuild: %w`, errctx) + } + + return esctx, nil +} -- cgit v1.3