Skip to content

Commit

Permalink
cmd/frontend: move typescript compile watching into main
Browse files Browse the repository at this point in the history
To remove the dependency from cmd/pkgsite on to
github.com/evanw/esbuild, this change moves the code that starts the
watcher for recompiling typescript files into cmd/frontend. This means
the dev mode functionality in cmd/pkgsite for watching and recompiling
typescript files is gone.

For golang/go#61399

Change-Id: If1325461de30dfd2f71b63d810cab8e9b4cdeb06
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/522735
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Michael Matloob <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
kokoro-CI: kokoro <[email protected]>
  • Loading branch information
matloob committed Aug 25, 2023
1 parent c1de6c5 commit ecfc750
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
18 changes: 17 additions & 1 deletion cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"golang.org/x/pkgsite/internal/queue"
"golang.org/x/pkgsite/internal/queue/gcpqueue"
"golang.org/x/pkgsite/internal/source"
"golang.org/x/pkgsite/internal/static"
"golang.org/x/pkgsite/internal/vuln"
)

Expand Down Expand Up @@ -115,6 +116,22 @@ func main() {
log.Fatalf(ctx, "vuln.NewClient: %v", err)
}
staticSource := template.TrustedSourceFromFlag(flag.Lookup("static").Value)
if *devMode {
// In dev mode compile TypeScript files into minified JavaScript files
// and rebuild them on file changes.
if *staticFlag == "" {
panic("staticPath is empty in dev mode; cannot rebuild static files")
}
ctx := context.Background()
if err := static.Build(static.Config{
EntryPoint: *staticFlag + "/frontend",
Watch: true,
Bundle: true,
}); err != nil {
log.Error(ctx, err)
}
}

// TODO: Can we use a separate queue for the fetchServer and for the Server?
// It would help differentiate ownership.
fetchServer := &fetchserver.FetchServer{
Expand All @@ -128,7 +145,6 @@ func main() {
Queue: fetchQueue,
TemplateFS: template.TrustedFSFromTrustedSource(staticSource),
StaticFS: os.DirFS(*staticFlag),
StaticPath: *staticFlag,
ThirdPartyFS: os.DirFS(*thirdPartyPath),
DevMode: *devMode,
LocalMode: *localMode,
Expand Down
1 change: 0 additions & 1 deletion cmd/pkgsite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule
DevMode: *devMode,
LocalMode: true,
LocalModules: localModules,
StaticPath: *staticFlag,
ThirdPartyFS: thirdparty.FS,
})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/frontend/fetchserver/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func newTestServerWithFetch(t *testing.T, proxyModules []*proxytest.Module, cach
// Integration tests will use the actual directories.
StaticFS: static.FS,
ThirdPartyFS: thirdparty.FS,
StaticPath: "../../static",
})
if err != nil {
t.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion internal/frontend/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func newTestServer(t *testing.T, cacher Cacher) (*Server, http.Handler) {
// Integration tests will use the actual directories.
StaticFS: static.FS,
ThirdPartyFS: thirdparty.FS,
StaticPath: "../../static",
})
if err != nil {
t.Fatal(err)
Expand Down
19 changes: 0 additions & 19 deletions internal/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"golang.org/x/pkgsite/internal/memory"
"golang.org/x/pkgsite/internal/middleware/stats"
"golang.org/x/pkgsite/internal/queue"
"golang.org/x/pkgsite/internal/static"
"golang.org/x/pkgsite/internal/version"
"golang.org/x/pkgsite/internal/vuln"
"golang.org/x/text/cases"
Expand All @@ -54,7 +53,6 @@ type Server struct {
devMode bool
localMode bool // running locally (i.e. ./cmd/pkgsite)
localModules []LocalModule // locally hosted modules; empty in production
staticPath string // used only for dynamic loading in dev mode
errorPage []byte
appVersionLabel string
googleTagManagerID string
Expand Down Expand Up @@ -95,7 +93,6 @@ type ServerConfig struct {
DevMode bool
LocalMode bool
LocalModules []LocalModule
StaticPath string // used only for dynamic loading in dev mode
Reporter derrors.Reporter
VulndbClient *vuln.Client
}
Expand All @@ -118,7 +115,6 @@ func NewServer(scfg ServerConfig) (_ *Server, err error) {
devMode: scfg.DevMode,
localMode: scfg.LocalMode,
localModules: scfg.LocalModules,
staticPath: scfg.StaticPath,
templates: ts,
reporter: scfg.Reporter,
fileMux: http.NewServeMux(),
Expand Down Expand Up @@ -738,21 +734,6 @@ func parsePageTemplates(fsys template.TrustedFS) (map[string]*template.Template,
}

func (s *Server) staticHandler() http.Handler {
// In dev mode compile TypeScript files into minified JavaScript files
// and rebuild them on file changes.
if s.devMode {
if s.staticPath == "" {
panic("staticPath is empty in dev mode; cannot rebuild static files")
}
ctx := context.Background()
if err := static.Build(static.Config{
EntryPoint: s.staticPath + "/frontend",
Watch: true,
Bundle: true,
}); err != nil {
log.Error(ctx, err)
}
}
return http.StripPrefix("/static/", http.FileServer(http.FS(s.staticFS)))
}

Expand Down

0 comments on commit ecfc750

Please sign in to comment.