monaco-languageclient: Runtime warning about "Could not create web worker(s)..."

Full warnings I get are

Image

Could not create web worker(s). Falling back to loading web worker code in main thread, which might cause UI freezes. Please see https://github.com/microsoft/monaco-editor#faq

I don’t think I’m getting the UI freezes it mentions, but the warning appeared when I changed from using the buildWorkerDefinition() method from the monaco-editor-workers package to using the new useWorkerFactory method of building/initializing workers. Or it could be from updating the monaco-languageclient packages, I’m not sure, but I don’t think I can use monaco-editor-workers with the newer monaco-languageclient packages. I’m using the monaco-editor-wrapper, but the warnings also appear in a branch I have where I’m not using the wrapper, so it doesn’t appear to be related to that.

It seems to be only about the simpleWorker, and it happens even if I don’t include the 'TextEditorWorker' and 'TextMateWorker' functions under workerLoaders for the useWorkerFactory() call like it is here

I’m using Angular and Webpack, and these versions of monaco packages: monaco-editor: 0.52.2, monaco-editor-wrapper: 6.2.5 monaco-languageclient: 9.2.5

A bit more from the console log in case it’s relevant: Image

My code where I’m using monaco editor can be seen here

I’ll reiterate that I’m not seeing errors or anything breaking that I’m aware of, but I’m guessing it’s not intended.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 35 (10 by maintainers)

Most upvoted comments

@jhk-mjolner I added your solution to the README here. This is not yet on main, but will be soon.

  1. Npm install webpack-cli (or webpack will do it for you when you try running this later).
  2. Create a bundle-monaco-workers.js file with this content:
// solve: __dirname is not defined in ES module scope
import {fileURLToPath} from 'url';
import {dirname, resolve} from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default {
  entry: {
    editor: './node_modules/@codingame/monaco-vscode-editor-api/esm/vs/editor/editor.worker.js',
    textmate: './node_modules/@codingame/monaco-vscode-textmate-service-override/worker.js',
  },
  output: {
    filename: '[name].js',
    path: resolve(__dirname, './src/assets/monaco-workers'),
    chunkLoading: false, // if this is true (default), webpack will produce code trying to access global `document` variable for the textmate worker, which will fail at runtime due to being a worker
  },
  mode: 'production',
  performance: {
    hints: false,
  },
};
  1. Add this line to your packages.json scripts section: "bundle monaco workers": "webpack --config bundle-monaco-workers.js"
  2. Run the script npm run 'bundle monaco workers'
  3. Configure the workerLoaders parameter for useWorkerFactory to point to the pre-bundled workers:
'TextEditorWorker': () => new Worker('/assets/monaco-workers/editor.js', {type: 'module'}),
'TextMateWorker': () => new Worker('/assets/monaco-workers/textmate.js', {type: 'module'}),
  1. Enable editor.experimental.asyncTokenization in the monaco-wrapper config, if you want to use the textmate worker.

It should work after that!

@jhk-mjolner you can disable the Textmate Worker by setting editor.experimental.asyncTokenization to false: https://github.com/TypeFox/monaco-languageclient/blob/main/packages/examples/src/bare/client.ts#L24

Building worker bundles yourself is pretty straight-forward, see: https://github.com/TypeFox/monaco-languageclient/blob/main/packages/wrapper/build/vite.cssWorker.ts

It is invoked like this: https://github.com/TypeFox/monaco-languageclient/blob/main/packages/wrapper/package.json#L77

I don’t want to expand the worker pre-bundling. My plan was actually to remove it. It could make sense to move the bundling instructions to an new extra package everyone can consume and perform the bundling if needed.

Since I got it working with copying the editorWorker-es.js file, I guess we can close this issue.

maybe you can do something in webpack as we do in esbuild

await esbuild.build({
    ....
    plugins: [
      copy({
        resolveFrom: "cwd",
        assets: [
          ....
          // Monaco editor workers
          {
            from: ["../../node_modules/monaco-editor-wrapper/dist/workers/*"],
            to: ["./static/libs/monaco-editor-workers"],
          },
        ],
        watch: true,
      }),
    ],

@jhk-mjolner yes, I will bring back the pre-bundled workers with #863