Skip to content

Issue with monaco workers when bundling monaco-languageclient and the monaco editor in a npm package #1013

@vemonet

Description

@vemonet

Hi, I am trying to create a npm package that bundles the monaco-editor and a custom wasm compiled language server (qlue-ls)

It aims to be modular, and is 2 levels deep:

  • Yasqe contains the query editor (monaco-languageclient + qlue-ls wasm language client)
  • Yasgui is a configurable UI containing the editor (Yasqe) + additional stuff around
  • The Yasgui package is then deployed to a HTML page compiled with vite

The 2 can be deployed simply with a call to a JS class given an HTML element and a JSON config:

import Yasgui from "@sib-swiss/yasgui";

const yasgui = new Yasgui(document.getElementById("yasgui"), {/* config */})

I managed to make this work in dev, but I am facing monaco workers import issues when building and deploying it in a HTML vite-compiled app.

For the vite config, I took inspiration from your own root config, and a config from an app that was working for bundling monaco + the language client directly, but the behavior seems different when building a lib. When building a lib it seems like the workers are compiled as base64 instead of having .wasm files (when building apps with similar vite config) bundled in dist/assets

I am using the configureDefaultWorkerFactory, cf. editor setup here, and import importMetaUrlPlugin from "@codingame/esbuild-import-meta-url-plugin"; in our vite configs.

You can see check the demo with the described issue deployed here: https://vemonet.github.io/Yasgui

At initial load the monaco editor is shown, but the syntax highlighting is not triggered, we are getting this error in the console:

TypeError: Error resolving module specifier “./main-BaMT9ISm.js”. data:dmFyIENoYXJDb2RlOwooZnVuY3Rpb24gKENoYXJDb2RlKSB7CiAgICBDaGFyQ29kZVtDaGFyQ29kZVsiTnVsbCJdID0gMF0
Uncaught (in promise) TypeError: Error resolving module specifier “./main-BaMT9ISm.js”. extension.api-DoXXMoHF-DDgp3BwH-B4ANaycD.js:27887:24
TypeError: Error resolving module specifier “./main-BaMT9ISm.js”.

The error in extension.api.###.js points to code in the class WebWorkerProtocol in vsCode workers related code

As well as a warning related to sourceMap URLs of workers:

Source map error: Error: URL constructor:  is not a valid URL.
Stack in the worker:resolveSourceMapURL@resource://devtools/client/shared/source-map-loader/utils/fetchSourceMap.js:56:22
getOriginalURLs@resource://devtools/client/shared/source-map-loader/source-map.js:74:24
workerHandler/</<@resource://devtools/client/shared/worker-utils.js:115:52
workerHandler/<@resource://devtools/client/shared/worker-utils.js:113:13

Resource URL: wasm:http://localhost:4173/Yasgui/assets/main-Dx4Qdq1s-Qpe4Nb0D-C6BcJyJa.js%20line%2031%20%3E%20WebAssembly.instantiate
Source Map URL: null

As soon as we enter a character in the editor then:

  • proper syntax highlighting kicks in (maybe some monaco editor service fallback?)
  • the qlue-ls language server autocomplete properly works

This seems to point at the fact that the workers are actually bundled in the libs and app, they just have issues with monaco workers imports at init

Click here to see how to test it locally
git clone https://github.com/vemonet/Yasgui
cd Yasgui
npm i

Dev works:

npm run dev

But build and deploying the demo gives the described error:

npm run build && npm run demo:preview

My questions are:

  • Do you have recommendation on how to define vite.config.ts to enable proper bundling of the monaco-languageclient in a reusable lib? Or comments on some bad config I might have set? I have tried to apply everything I could find in your docs, but could not find much about lib bundling.
  • Any pointers to code of npm packages that managed to properly bundle monaco and the monaco-languageclient and simplify its distribution?
    • monaco-languageclient seems to have managed to do it, but I could not find any vite.config.ts for the libs in the packages folder, there is just a vite.config.ts at the root, but it is not clear how you managed to properly packaged the monaco editor in libs
  • Is this even a good idea to try to bundle monaco and the monaco-languageclient? Or am I trying to do something for which the current ESM import system and vite is not yet ready for? (bundling monaco workers in ES modules)

This issue is a follow up to #950 after migrating from monaco-editor-wrapper to the latest monaco-languageclient version

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions