-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload-monaco.js
50 lines (43 loc) · 1.72 KB
/
load-monaco.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export const loadMonaco = async ({
baseUrl = "",
injectStyles = true,
} = {}) => {
const editorBaseUrl =
baseUrl || import.meta.url.split("/").slice(0, -1).join("/") + "/dist/";
const tailwindcssWorkerUrl =
"https://cdn.jsdelivr.net/npm/@live-codes/browser-compilers/dist/tailwindcss/monaco-tailwindcss.worker.js";
// allow using web workers from CDN
const toDataUrl = (content, type = "text/javascript") =>
`data:${type};charset=UTF-8;base64,` + btoa(content);
const getWorkerDataURL = (url) => toDataUrl(`importScripts("${url}");`);
// configure monaco to use the web workers
globalThis.MonacoEnvironment = {
getWorkerUrl: function (_moduleId, label) {
if (label === "json") {
return getWorkerDataURL(editorBaseUrl + "json.worker.js");
}
if (label === "css" || label === "scss" || label === "less") {
return getWorkerDataURL(editorBaseUrl + "css.worker.js");
}
if (label === "html" || label === "handlebars" || label === "razor") {
return getWorkerDataURL(editorBaseUrl + "html.worker.js");
}
if (label === "typescript" || label === "javascript") {
return getWorkerDataURL(editorBaseUrl + "ts.worker.js");
}
if (label === "tailwindcss") {
return getWorkerDataURL(tailwindcssWorkerUrl);
}
return getWorkerDataURL(editorBaseUrl + "editor.worker.js");
},
};
if (injectStyles) {
const stylesheet = document.createElement("link");
stylesheet.crossOrigin = "anonymous";
stylesheet.href = editorBaseUrl + "monaco-editor.css";
stylesheet.rel = "stylesheet";
document.head.appendChild(stylesheet);
}
const monaco = await import(editorBaseUrl + "monaco-editor.js");
return monaco;
};