Skip to content

Commit

Permalink
hacked up webworker support for monaco.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhollen committed May 2, 2024
1 parent caeabf3 commit d8fd029
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
15 changes: 13 additions & 2 deletions enterprise/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
load("//rules/typescript:index.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@npm//@bazel/esbuild:index.bzl", "esbuild", "esbuild_config")
load("//rules/sha:index.bzl", "sha")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//rules/typescript:index.bzl", "ts_library")

# gazelle:default_visibility //enterprise:__subpackages__
package(default_visibility = ["//enterprise:__subpackages__"])
Expand All @@ -27,6 +28,7 @@ esbuild(
define = {"process.env.NODE_ENV": '"production"'},
entry_points = [
"app.tsx",
"monaco_worker.js",
],
metafile = False,
minify = select({
Expand All @@ -36,6 +38,7 @@ esbuild(
visibility = ["//visibility:public"],
deps = [
":app",
":monaco_worker",
],
)

Expand Down Expand Up @@ -105,6 +108,14 @@ ts_library(
],
)

js_library(
name = "monaco_worker",
srcs = ["monaco_worker.js"],
deps = [
"@npm//monaco-editor",
]
)

genrule(
name = "empty_for_embedsrcs",
outs = ["empty"],
Expand Down
5 changes: 5 additions & 0 deletions enterprise/app/monaco_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as worker from "monaco-editor/esm/vs/editor/editor.worker";

export function initialize() {
worker.initialize();
}
12 changes: 8 additions & 4 deletions server/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
const (
indexTemplateFilename = "index.html"
stylePathTemplate = "/app/style.css?hash={APP_BUNDLE_HASH}"
monacoWorkerPathTemplate = "/app/app_bundle/monaco_worker.js?hash={APP_BUNDLE_HASH}"
)

var (
Expand Down Expand Up @@ -94,12 +95,13 @@ func NewStaticFileServer(env environment.Env, fs fs.FS, rootPaths []string, appB

jsPath := strings.ReplaceAll(*jsEntryPointPath, "{APP_BUNDLE_HASH}", appBundleHash)
stylePath := strings.ReplaceAll(stylePathTemplate, "{APP_BUNDLE_HASH}", appBundleHash)
workerPath := strings.ReplaceAll(monacoWorkerPathTemplate, "{APP_BUNDLE_HASH}", appBundleHash)

if strings.HasPrefix(jsPath, "http://") || strings.HasPrefix(jsPath, "https://") {
env.GetHealthChecker().AddHealthCheck("app_static_file_server", &healthChecker{jsPath: jsPath})
}

handler = handleRootPaths(env, rootPaths, template, version.AppVersion(), jsPath, stylePath, handler)
handler = handleRootPaths(env, rootPaths, template, version.AppVersion(), jsPath, stylePath, workerPath, handler)
}
return &StaticFileServer{
handler: setCacheHeaders(handler),
Expand All @@ -111,7 +113,7 @@ func (s *StaticFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handler.ServeHTTP(w, r)
}

func handleRootPaths(env environment.Env, rootPaths []string, template *template.Template, version, jsPath, stylePath string, h http.Handler) http.Handler {
func handleRootPaths(env environment.Env, rootPaths []string, template *template.Template, version, jsPath, stylePath string, workerPath string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, rootPath := range rootPaths {
if strings.HasPrefix(r.URL.Path, rootPath) {
Expand All @@ -120,7 +122,7 @@ func handleRootPaths(env environment.Env, rootPaths []string, template *template
}

if r.URL.Path == "/" {
serveIndexTemplate(r.Context(), env, template, version, jsPath, stylePath, w)
serveIndexTemplate(r.Context(), env, template, version, jsPath, stylePath, workerPath, w)
return
}

Expand All @@ -141,6 +143,7 @@ func setCacheHeaders(h http.Handler) http.Handler {
type FrontendTemplateData struct {
// StylePath is the path to the main styles for the app.
StylePath string
WorkerPath string
// JsEntryPointPath is the path to the main script that bootstraps the app.
JsEntryPointPath string
// GaEnabled decides whether to render the Google Analytics script.
Expand All @@ -149,7 +152,7 @@ type FrontendTemplateData struct {
Config template.JS
}

func serveIndexTemplate(ctx context.Context, env environment.Env, tpl *template.Template, version, jsPath, stylePath string, w http.ResponseWriter) {
func serveIndexTemplate(ctx context.Context, env environment.Env, tpl *template.Template, version, jsPath, stylePath string, workerPath string, w http.ResponseWriter) {
config := cfgpb.FrontendConfig{
Version: version,
ConfiguredIssuers: env.GetAuthenticator().PublicIssuers(),
Expand Down Expand Up @@ -206,6 +209,7 @@ func serveIndexTemplate(ctx context.Context, env environment.Env, tpl *template.
}
err = tpl.ExecuteTemplate(w, indexTemplateFilename, &FrontendTemplateData{
StylePath: stylePath,
WorkerPath: workerPath,
JsEntryPointPath: jsPath,
GaEnabled: !*disableGA,
Config: template.JS(configJSON),
Expand Down
5 changes: 5 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
window.buildbuddyConfig = {{.Config}};
{{/* Monaco loads some libraries using require(), ignore these for now. */}}
window.require = () => { };
self.MonacoEnvironment = {
getWorkerUrl: function (moduleId, label) {
return "{{.WorkerPath}}";
},
};
</script>

{{if .GaEnabled}}
Expand Down

0 comments on commit d8fd029

Please sign in to comment.