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

[Feature Request] ESM build that can be loaded from CDNs #4628

Open
2 tasks done
hatemhosny opened this issue Jul 31, 2024 · 1 comment
Open
2 tasks done

[Feature Request] ESM build that can be loaded from CDNs #4628

hatemhosny opened this issue Jul 31, 2024 · 1 comment
Labels
feature-request Request for new features or functionality

Comments

@hatemhosny
Copy link

Context

  • This issue is not a bug report. (please use a different template for reporting a bug)
  • This issue is not a duplicate of an existing issue. (please use the search to find existing issues)

Description

This is a suggestion to provide an ESM build that can be loaded from CDN.

Currently, the recommended way is to build the editor with a bundler and host it with the page that needs to load it. In addition it requires the user to configure the MonacoEnvironment.getWorkerUrl

This is a bit cumbersome and exposes implementation details that the end-user does not need to be concerned with.

The other alternative is to use the loader, that loads an AMD version, which I think should no longer be the recommended way, with ESM being used across the whole eco-system. In addition, it is prone to conflicts with other libraries and getting such errors:
Error: Can only have one anonymous define call per script file

My suggestion is something like this:

<div id="container" style="height: 200px;"></div>

<script type="module">
  import { monaco } from "https://cdn.jsdelivr.net/npm/monaco-editor-esm-cdn/monaco.js";

  monaco.editor.create(document.getElementById("container"), {
    value: `function x() {\n\tconsole.log("Hello world!");\n}`,
    language: "javascript",
  });
</script>

Try in LiveCodes playground

This works by using a simple workaround that allows using web workers from a CDN.

I believe this can make using Monaco editor much simpler.
If you think this way is acceptable, I can start a PR to either provide such build that can just be published with the npm package or at least add docs for using something like this.

Please see the example repo:
https://github.com/hatemhosny/monaco-editor-esm-cdn

Monaco Editor Playground Link

No response

Monaco Editor Playground Code

No response

@hatemhosny hatemhosny added the feature-request Request for new features or functionality label Jul 31, 2024
@kachurun
Copy link

This is not a big deal to run it through esm.sh

import * as Monaco from 'https://esm.sh/[email protected]?bundle';
import EditorWorker from 'https://esm.sh/[email protected]/esm/vs/editor/editor.worker?worker';
import JsonWorker from 'https://esm.sh/[email protected]/esm/vs/language/json/json.worker?worker';
import CssWorker from 'https://esm.sh/[email protected]/esm/vs/language/css/css.worker?worker';
import HtmlWorker from 'https://esm.sh/[email protected]/esm/vs/language/html/html.worker?worker';
import TSWorker from 'https://esm.sh/[email protected]/esm/vs/language/typescript/ts.worker?worker';

async function loadCSS() {
    const css = await fetch(`https://esm.sh/[email protected]?css`).then(response => response.text());
    const style = document.createElement('style');

    style.id = 'monaco-css';
    style.innerHTML = css;

    document.getElementById('monaco-css')?.remove();
    document.head.appendChild(style);
}

self.monaco = monaco;
self.MonacoEnvironment = {
    getWorker(_, label) {
        if (label === 'json') {
            return new JsonWorker();
        }

        if (label === 'css' || label === 'scss' || label === 'less') {
            return new CssWorker();
        }

        if (label === 'html') {
            return new HtmlWorker();
        }

        if (label === 'typescript' || label === 'javascript') {
            return TSWorker();
        }

        return new EditorWorker();
    }
};

loadCSS();

monaco.editor.create(document.getElementById("container"), {
  value: `function x() {\n\tconsole.log("Hello world!");\n}`,
  language: "javascript",
});

But yes, it's really strange that Microsoft recommends using that silly require loader instead of ESM.

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

No branches or pull requests

2 participants