As it turned out, the code snippet of the original post (and the official documentation) causes subtle bugs Firefox (e.g. the highlighting in monaco's diff editor did not work).
I think this happens because Firefox needs an explicit type: module for registering the worker, otherwise an error happens: import declarations may only appear at top level of a module
Here is the version that works in Chrome + Firefox. Maybe you want to update your post accordingly, @lawrencecchen ?
<script lang="ts">importloaderfrom'@monaco-editor/loader';import{onDestroy,onMount}from'svelte';importtype*asMonacofrom'monaco-editor/esm/vs/editor/editor.api';leteditor:Monaco.editor.IStandaloneCodeEditor;letmonaco:typeofMonaco;leteditorContainer:HTMLElement;onMount(async()=>{// Remove this line to load the monaco editor from a CDN// see https://www.npmjs.com/package/@monaco-editor/loader#configloader.config({paths:{vs:'/node_modules/monaco-editor/min/vs'}});monaco=awaitloader.init();// Sampleconsteditor=monaco.editor.create(editorContainer);constmodel=monaco.editor.createModel("console.log('Hello from Monaco! (the editor, not the city...)')",undefined,// Give monaco a hint which syntax highlighting to usemonaco.Uri.file('sample.js'));editor.setModel(model);});onDestroy(()=>{monaco?.editor.getModels().forEach((model)=>model.dispose());});</script>
You're welcome, but please be aware that this still loads the editor from a CDN if I remember correctly. (Meaning the comment is wrong). I've been fighting with this for a while, maybe tomorrow the post is finally ready.
If you are fine with a CDN, the loader library is a good solution.
I'd be fine with a CDN, but I get a 404 after building and deploying my app:
GET - 404 - https://my-domain.com/node_modules/monaco-editor/min/vs/loader.js
As it turned out, the code snippet of the original post (and the official documentation) causes subtle bugs Firefox (e.g. the highlighting in monaco's diff editor did not work).
I think this happens because Firefox needs an explicit
type: module
for registering the worker, otherwise an error happens:import declarations may only appear at top level of a module
Here is the version that works in Chrome + Firefox. Maybe you want to update your post accordingly, @lawrencecchen ?
In addition, setting
let Monaco: typeof monaco;
will help TypeScript to figure stuff out ;-)Thanks for pointing that out @dan1ve
I get the following warning in firefox:
self.MonacoEnvironment.getWorkerUrl is not a function
Do you know the solution to this?
In the meantime, I used the monaco-loader library, which does the heavy lifting for us, and works best.
Blog post coming tomorrow (I'll link it here)
No blog post yet, but here is the gist:
Works perfectly for both Firefox and Chrome @dan1ve
Thank you very much! Looking forward to read your blog article!
You're welcome, but please be aware that this still loads the editor from a CDN if I remember correctly. (Meaning the comment is wrong). I've been fighting with this for a while, maybe tomorrow the post is finally ready.
If you are fine with a CDN, the loader library is a good solution.
I'd be fine with a CDN, but I get a 404 after building and deploying my app:
GET - 404 -
https://my-domain.com/node_modules/monaco-editor/min/vs/loader.js
Seems like it doesn't use a CDN.
Did you remove the loader.config() line? (You should :)
A blog post with a full how-to is here: codelantis.com/blog/sveltekit-mona...