From 0d0a52e249b18fe9363283bec3a6cab720fa8fb4 Mon Sep 17 00:00:00 2001 From: Austin Morton Date: Wed, 13 Nov 2019 23:09:11 -0500 Subject: [PATCH] "Fix" cross origin monaco web workers This is a workaround to make cross origin monaco web workers function in spite of the monaco webpack plugin hijacking the MonacoEnvironment global. see https://github.com/microsoft/monaco-editor-webpack-plugin/issues/42 This workaround wouldn't be so bad, if it didn't _also_ rely on *another* bug to actually work. The webpack plugin incorrectly uses window.__webpack_public_path__ when it should use __webpack_public_path__ see https://github.com/microsoft/monaco-editor-webpack-plugin/pull/63 We can leave __webpack_public_path__ with the correct value, which lets runtime chunk loading continue to function correctly. We can then set window.__webpack_public_path__ to the below handler, which lets us fabricate a worker on the fly. This is bad and I feel bad. --- app.js | 30 ++++++++++++++++++++++++++++++ static/options.js | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 600463da12b..687123e6973 100755 --- a/app.js +++ b/app.js @@ -502,6 +502,36 @@ aws.initConfig(awsProps) const shortenerLib = require(`./lib/shortener-${clientOptionsHandler.options.urlShortenService}`); const shortener = shortenerLib({storageHandler}); + /* + * This is a workaround to make cross origin monaco web workers function + * in spite of the monaco webpack plugin hijacking the MonacoEnvironment global. + * + * see https://github.com/microsoft/monaco-editor-webpack-plugin/issues/42 + * + * This workaround wouldn't be so bad, if it didn't _also_ rely on *another* bug to + * actually work. + * + * The webpack plugin incorrectly uses + * window.__webpack_public_path__ + * when it should use + * __webpack_public_path__ + * + * see https://github.com/microsoft/monaco-editor-webpack-plugin/pull/63 + * + * We can leave __webpack_public_path__ with the correct value, which lets runtime chunk + * loading continue to function correctly. + * + * We can then set window.__webpack_public_path__ to the below handler, which lets us + * fabricate a worker on the fly. + * + * This is bad and I feel bad. + */ + router.get('/workers/:worker', (req, res) => { + staticHeaders(res); + res.set('Content-Type', 'application/javascript'); + res.end(`importScripts('${urljoin(staticRoot, req.params.worker)}');`); + }); + router .use(Sentry.Handlers.requestHandler()) .use(morgan(morganFormat, { diff --git a/static/options.js b/static/options.js index 3ba2dada119..78e2e235c0a 100644 --- a/static/options.js +++ b/static/options.js @@ -35,6 +35,8 @@ for (var k in extraOptions) { } __webpack_public_path__ = window.staticRoot; -window.__webpack_public_path__ = __webpack_public_path__; + +// really gross monaco web worker cross origin workaround +window.__webpack_public_path__ = window.httpRoot + 'workers/'; module.exports = window.compilerExplorerOptions;