A script to build monaco-editor workers using parcel
This package will
-
- Find
monaco-editor
in your project
- Find
-
- Build all files ending with
.worker.js
using Parcel
- Build all files ending with
-
- Save them in your
public
folder
- Save them in your
-
- Generate a
monacoWorkerManifest.json
file in yoursrc
folder
- Generate a
- Install
monaco-editor
yarn add monaco-editor
# or
npm install monaco-editor
- Install
monaco-editor-builder
yarn add monaco-editor-builder
# or
npm install monaco-editor-builder
- Add a script to build workers
{
"scripts": {
"build:monaco": "monaco-editor-builder"
}
}
- Don't forget to run this scripts in production
{
"scripts": {
"build": "npm run build:monaco && react-scripts build",
"build:monaco": "monaco-editor-builder"
}
}
- Setup monaco-editor
// src/index.js
import * as monaco from "monaco-editor";
import monacoWorkers from "./monacoWorkerManifest.json";
// Since packaging is done by you, you need
// to instruct the editor how you named the
// bundles that contain the web workers.
self.MonacoEnvironment = {
getWorkerUrl: function(moduleId, label) {
if (label === "json") {
return monacoWorkers["language/json/json.worker.js"];
}
if (label === "css") {
return monacoWorkers["language/css/css.worker.js"];
}
if (label === "html") {
return monacoWorkers["language/html/html.worker.js"];
}
if (label === "typescript" || label === "javascript") {
return monacoWorkers["language/typescript/ts.worker.js"];
}
return monacoWorkers["editor/editor.worker.js"];
}
};
monaco.editor.create(document.getElementById("container"), {
value: ["function x() {", '\tconsole.log("Hello world!");', "}"].join("\n"),
language: "javascript"
});