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

Example of using monaco editor in typescript code

Open brentshermana opened this issue 7 years ago • 8 comments

Monaco provides typescript definitions, so it should be able to be used in typescript. Unfortunately, all the examples of using monaco are just in embedded HTML, so it's tough to get get started, even with a minimal example.

Right now, I'm working off of the following. It doesn't get any simpler than this, folks:

// src/index.ts
import {
    editor
} from 'monaco-editor';

let div = document.createElement('div');
editor.create(div);

I get the following errors in the browser (chrome):

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

You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker

To address the first error, I checked out the faq and yes, I am loading the webpage via http://. No clue about the second, though.

Importing a library should not be this difficult. Has anyone figured out how to do this in Typescript?

brentshermana avatar Jan 28 '19 21:01 brentshermana

Hi , you need exactly this

AMD Loader

yln99517 avatar Jan 29 '19 06:01 yln99517

I looked through the javascript examples, but they aren't very helpful for typescript. Specifically, the type checking totally falls apart when I try to add the loader.

brentshermana avatar Jan 29 '19 14:01 brentshermana

+1 for this

Example of typescript with webpack esm would be awesome.

..or typescript amd.

arniebradfo avatar Mar 25 '19 19:03 arniebradfo

+1

Deepakanandrao avatar Apr 18 '19 20:04 Deepakanandrao

+1

thomas3577 avatar Jul 18 '19 06:07 thomas3577

+1

timnlupo avatar Jul 26 '19 20:07 timnlupo

Have you read this thread? https://github.com/Microsoft/monaco-editor/issues/1290#issuecomment-459075707

It's all about config.

tsconfig.json

{
    "compilerOptions": {
        // ...
        "target": "es6",
        "module": "commonjs",
        // ...
    }
}

webpack.config.js

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
// ...
module.exports = {
    // ...
    module: {
        rules: [
            // ...
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.js', '...']
    },
    plugins: [
        // ...
        new MonacoWebpackPlugin()
    ],
};

package.json

{
  // ...
  "devDependencies": {
    // ...
    "css-loader": "^3.2.0",
    "monaco-editor-webpack-plugin": "^1.7.0",
    "style-loader": "^1.0.0",
    "ts-loader": "^6.0.4",
    "typescript": "^3.5.3",
    "webpack": "^4.39.2",
    "webpack-cli": "^3.3.6",
    "webpack-dev-server": "^3.8.0",
  },
  "dependencies": {
    "monaco-editor": "^0.17.1"
  }
}

index.ts

import * as monaco from "monaco-editor";

document.addEventListener('DOMContentLoaded', main, false);

function main() {
    var monacoEditorContainer = document.getElementById("monaco-editor-container");
    if (monacoEditorContainer) {
        monaco.editor.create(monacoEditorContainer, {
            value: 'console.log("Hello, world")',
            language: 'javascript'
        });
    }
}

If you still have problems let me know and I will create a sample repo.

Enc-EE avatar Aug 23 '19 15:08 Enc-EE

There is a sample in the meantime -- https://github.com/microsoft/monaco-editor-samples/tree/master/browser-esm-webpack-typescript

alexdima avatar Dec 12 '19 21:12 alexdima