monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

ESM with Vanilla JS

Open sbuchok opened this issue 5 years ago • 10 comments

Please create a sample for ESM that doesn't use WebPack or Parcel. Just plain Vanilla JS.

sbuchok avatar Feb 03 '21 17:02 sbuchok

Hi @sbuchok Check out the AMD version (linked from the main readme):

https://github.com/microsoft/monaco-editor/blob/master/docs/integrate-amd.md

Below is a version of it that runs from CDN (jsdelivr).

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>

<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script>
<script>
    require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs' }});
    require(['vs/editor/editor.main'], function() {
        var editor = monaco.editor.create(document.getElementById('container'), {
            value: [
                'function x() {',
                '\tconsole.log("Hello world!");',
                '}'
            ].join('\n'),
            language: 'javascript'
        });
    });
</script>
</body>
</html>

ConradSollitt avatar Feb 03 '21 23:02 ConradSollitt

I was already using the AMD version before. I want a straight vanilla version that has 0 dependencies on other tech.

sbuchok avatar Feb 04 '21 00:02 sbuchok

@sbuchok What is your goal of a vanilla version? For example - all code in a single JS file? I'm not a maintainer/developer of Monaco (rather an end-user/developer) so I can't provide future design divisions, etc but Monaco uses Web Workers and dynamic loading so a single file would probably not be possible.

Anyways, using the AMD version only code related to the current repository is loaded and no other files so it has no dependancies other than this repository or npm package. Personally I find that it works well for 0 dependancies as I can simply copy all files or just use a CDN/ link.

ConradSollitt avatar Feb 12 '21 12:02 ConradSollitt

@ConradSollitt

Thanks for the reply.

What I would like to be able to do is:

import { monaco } from './somefolder/monaco.js';
...setupcode, labuage, theme, that sort of stuff...

monaco.editor.create(...);

Using native ES Modules. The ESM version makes it sound like you can do this, but that is not the case.

I want to be able to use Monaco from within a js module without the need for WebPack or anything else. As I mentioned, I already have the AMD version working. I also got the AMD version working ... hackily, within a js module. But it is not straight forward.

Here is what I did, please remember, very hacky.

(async () => {
    window.require = { paths: { 'vs': 'scripts/monaco/vs' } };

    await import('./monaco/vs/loader.js');
    await import('./monaco/vs/editor/editor.main.nls.js');

    require(["vs/editor/editor.main"], () => {
        monaco.languages.register(...);
        monaco.languages.setMonacrchTokenProvider(...);
        monaco.editor.defineTheme(...);
...
    });
})();

sbuchok avatar Feb 12 '21 18:02 sbuchok

Related to https://github.com/microsoft/monaco-editor/issues/949

What's the status on this issue? We're in mid 2021 and monaco-editor still can't be loaded directly as an ESM module in the browser without AMD, Webpack or any other stuff?

It can be loaded with skypack.dev (https://www.skypack.dev/view/monaco-editor) when it works (but it is often broken).

We need a working solution right out of the box.

kurtcodemander avatar Jul 16 '21 11:07 kurtcodemander

It can be loaded with skypack.dev (https://www.skypack.dev/view/monaco-editor) when it works (but it is often broken).

Can you elaborate on how you got it to work with SkyPack or Esm.run? I haven't been able to get it to run with them.

jamesgibson14 avatar Aug 26 '21 04:08 jamesgibson14

For anyone who needs - here, is a utility that helps you to overcome the webpack configuration.

Please note that it doesn't solve the issue we have - we still waiting for the "pure" ESM version of the monaco-editor

suren-atoyan avatar Oct 15 '21 21:10 suren-atoyan

try esm.sh

import * as monaco from 'https://esm.sh/monaco-editor';
import editorWorker from 'https://esm.sh/monaco-editor/esm/vs/editor/editor.worker?worker';
import jsonWorker from 'https://esm.sh/monaco-editor/esm/vs/language/json/json.worker?worker';
import cssWorker from 'https://esm.sh/monaco-editor/esm/vs/language/css/css.worker?worker';
import htmlWorker from 'https://esm.sh/monaco-editor/esm/vs/language/html/html.worker?worker';
import tsWorker from 'https://esm.sh/monaco-editor/esm/vs/language/typescript/ts.worker?worker';

self.MonacoEnvironment = {
  getWorker(_, label) {
    if (label === 'json') {
      return new jsonWorker();
    }
    if (label === 'css' || label === 'scss' || label === 'less') {
      return new cssWorker();
    }
    if (label === 'html' || label === 'handlebars' || label === 'razor') {
      return new htmlWorker();
    }
    if (label === 'typescript' || label === 'javascript') {
      return new tsWorker();
    }
    return new editorWorker();
  }
};

monaco.editor.create(document.getElementById('container'), {
  value: "function hello() {\n\talert('Hello world!');\n}",
  language: 'javascript'
});

ije avatar Dec 02 '21 05:12 ije

@ije have you got it working with esm.sh? I tested it and it does load correctly but then there is an Error: Unexpected usage, and also a CORS error: "Failed to construct 'Worker': Script at 'https://esm.sh/v58/[email protected]/es2021/esm/vs/editor/editor.worker.js' cannot be accessed from origin 'https://cdpn.io'." Any idea how to fix these?

jamesgibson14 avatar Dec 08 '21 10:12 jamesgibson14

Here is an code pen of it working but with errors: https://codepen.io/jamesgibson14/pen/ZEXOWEz

jamesgibson14 avatar Dec 08 '21 18:12 jamesgibson14