monaco-vscode-api: Cannot get the startup guide to work
I did follow the Getting started guide. Precisely, I started at the section New Begining:
Up until the section Adding language highlighting, everything worked fine except some warnings and errors in the browser console:
But in this section the website stopped working.
I did not use bun, so I used npm commands instead.
The Exact steps I took
create vite project:
$ npm create vite my-monaco-editor
(options: Vanilla, TypeScript)
$ cd my-monaco-editor
$ npm install
$ npm add vscode@npm:@codingame/monaco-vscode-api
$ npm add monaco-editor@npm:@codingame/monaco-vscode-editor-api
$ npm add -D @types/vscode
import code
copy index.html from guide
copy style.css from guide
copy main.ts from guide
improve adding workers
copy main.ts from guide
vite fix
copy vite.config.ts
$ npm install @codingame/esbuild-import-meta-url-plugin
[this is the last working state]
adding language highlighting
$ npm install @codingame/monaco-vscode-textmate-service-override
$ npm install @codingame/monaco-vscode-theme-service-override
$ npm install @codingame/monaco-vscode-languages-service-override
$ npm install @codingame/monaco-vscode-python-default-extension
$ npm install @codingame/monaco-vscode-theme-defaults-default-extension
copy main.ts from guide
// adding worker
export type WorkerLoader = () => Worker;
const workerLoaders: Partial<Record<string, WorkerLoader>> = {
TextEditorWorker: () => new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker.js', import.meta.url), { type: 'module' })
}
window.MonacoEnvironment = {
getWorker: function (_workerId, label) {
const workerFactory = workerLoaders[label]
if (workerFactory != null) {
return workerFactory()
}
throw new Error(`Worker ${label} not found`)
}
}
// adding services
await initialize({
...getTextMateServiceOverride(),
...getThemeServiceOverride(),
...getLanguagesServiceOverride(),
});
monaco.editor.create(document.getElementById('editor')!, {
value: "print('Hello world!')",
language: "python"
});
[now there is no monaco-editor visible on the website, only background]
Also, I needed to remove some brackets from the example code:
const workerLoaders: Partial<Record<string, WorkerLoader>> = {
TextEditorWorker: () => new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker.js', import.meta.url), { type: 'module' })
}) //<-- removed theese since they were never opened
}
If there is additional information to provide, let me know.
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 25 (13 by maintainers)
For the onig error, you need to optimize
vscode-onigurumaas wellFor the other errors, it seems there are stacktraces, I’ll let you investigate a bit
The first error message is pretty self-explanatory
regarding the vscodetextmate issue, it’s because the lib is published as commonjs, you need to force vite to optimize it
Ok the guide was outdated, I’ve updated it!
@monaco-vscode-apishouldn’t be installed asvscodeanymore, and theinitializemethod should be imported from@codingame/monaco-vscode-api