Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native approach to load web workers #2605

Open
remcohaszing opened this issue Aug 6, 2021 · 3 comments
Open

Use native approach to load web workers #2605

remcohaszing opened this issue Aug 6, 2021 · 3 comments
Labels
ESM ECMAScript modules feature-request Request for new features or functionality

Comments

@remcohaszing
Copy link
Contributor

Currently to use Monaco with module bundlers such as Webpack 5, Parcel, Snowpack, or natively in ESM in a browser, one needs to use the following:

window.MonacoEnvironment = {
  getWorker(moduleId, label) {
    switch (label) {
      case 'css':
      case 'less':
      case 'scss':
        return new Worker(new URL('monaco-editor/esm/vs/language/css/css.worker', import.meta.url));
      case 'editorWorkerService':
        return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url));
      case 'handlebars':
      case 'html':
      case 'razor':
        return new Worker(new URL('monaco-editor/esm/vs/language/html/html.worker', import.meta.url));
      case 'json':
        return new Worker(new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url));
      case 'javascript':
      case 'typescript':
        return new Worker(new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url));
      case 'yaml':
        return new Worker(new URL('monaco-yaml/lib/esm/yaml.worker', import.meta.url));
      default:
        throw new Error(`Unknown label ${label}`);
    }
  },
};

All official documentation and examples on this subject are outdated.

It appears the issue of having to resolve a worker is going away. Instead of putting this burden on the user if this library, it may be better to move this to the plugins instead. I.e., a plugin could call something like:

import { registerWorker } from 'monaco-editor';

registerWorker('json', new Worker(new URL('./json.worker', import.meta.url)));

This way users don’t have to deal with workers explicitly anymore.

@jimmywarting
Copy link

I think that web workers should use new Worker(url, { type: module }) variant

@Kuinox
Copy link

Kuinox commented Nov 6, 2022

I think that web workers should use new Worker(url, { type: module }) variant

Firefox doesn't support yet modules in web workers.

@jimmywarting
Copy link

Undici (aka fetch) now wants to create blobs backed up by filesystems as well when parsing multipart FormData.
New discussion is taking place here to bring life into some kind of fs.getFileBlob('some-file-path.txt') so just more reason to wanting to have this in the feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ESM ECMAScript modules feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants