Example of using monaco editor in typescript code
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?
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.
+1 for this
Example of typescript with webpack esm would be awesome.
..or typescript amd.
+1
+1
+1
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.
There is a sample in the meantime -- https://github.com/microsoft/monaco-editor-samples/tree/master/browser-esm-webpack-typescript