monaco-editor/docs/integrate-esm.md

230 lines
6.5 KiB
Markdown
Raw Normal View History

2018-03-14 21:44:58 +08:00
## Integrating the ESM version of the Monaco Editor
- [Webpack](#using-webpack)
2022-07-23 11:55:53 +08:00
- [Option 1: Using the Monaco Editor WebPack Plugin](#option-1-using-the-monaco-editor-webpack-plugin)
2019-03-02 08:22:06 +08:00
- [Option 2: Using plain webpack](#option-2-using-plain-webpack)
- [Parcel](#using-parcel)
- [Vite](#using-vite)
2018-03-14 21:44:58 +08:00
### Using webpack
Here is the most basic script that imports the editor using ESM with webpack.
2021-11-17 06:18:23 +08:00
More self-contained samples are available in the [samples folder](../samples/).
2018-03-14 21:44:58 +08:00
---
2021-11-17 06:01:55 +08:00
### Option 1: Using the Monaco Editor WebPack Plugin
2021-11-17 06:18:23 +08:00
This is the easiest method, and it allows for options to be passed into the plugin in order to select only a subset of editor features or editor languages. Read more about the [Monaco Editor WebPack Plugin](../webpack-plugin/), which is a community authored plugin.
2021-11-06 07:15:13 +08:00
- `index.js`
```js
import * as monaco from 'monaco-editor';
monaco.editor.create(document.getElementById('container'), {
2021-11-06 07:15:13 +08:00
value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
language: 'javascript'
});
```
2021-11-06 07:15:13 +08:00
- `webpack.config.js`
```js
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const path = require('path');
module.exports = {
2021-11-06 07:15:13 +08:00
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js'
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.ttf$/,
use: ['file-loader']
}
]
},
plugins: [new MonacoWebpackPlugin()]
};
```
---
### Option 2: Using plain webpack
2021-11-17 06:18:23 +08:00
Full working samples are available at https://github.com/microsoft/monaco-editor/tree/main/samples/browser-esm-webpack or https://github.com/microsoft/monaco-editor/tree/main/samples/browser-esm-webpack-small
2019-03-02 08:21:41 +08:00
2021-11-06 07:15:13 +08:00
- `index.js`
2018-03-14 21:44:58 +08:00
```js
import * as monaco from 'monaco-editor';
// Since packaging is done by you, you need
// to instruct the editor how you named the
// bundles that contain the web workers.
self.MonacoEnvironment = {
2021-11-06 07:15:13 +08:00
getWorkerUrl: function (moduleId, label) {
if (label === 'json') {
return './json.worker.bundle.js';
}
if (label === 'css' || label === 'scss' || label === 'less') {
return './css.worker.bundle.js';
}
if (label === 'html' || label === 'handlebars' || label === 'razor') {
return './html.worker.bundle.js';
}
if (label === 'typescript' || label === 'javascript') {
return './ts.worker.bundle.js';
}
return './editor.worker.bundle.js';
}
};
2018-03-14 21:44:58 +08:00
monaco.editor.create(document.getElementById('container'), {
2021-11-06 07:15:13 +08:00
value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
language: 'javascript'
2018-03-14 21:44:58 +08:00
});
```
2021-11-06 07:15:13 +08:00
- `webpack.config.js`:
2018-03-14 21:44:58 +08:00
```js
const path = require('path');
module.exports = {
2021-11-06 07:15:13 +08:00
entry: {
app: './index.js',
// Package each language's worker and give these filenames in `getWorkerUrl`
'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
'json.worker': 'monaco-editor/esm/vs/language/json/json.worker',
'css.worker': 'monaco-editor/esm/vs/language/css/css.worker',
'html.worker': 'monaco-editor/esm/vs/language/html/html.worker',
'ts.worker': 'monaco-editor/esm/vs/language/typescript/ts.worker'
},
output: {
globalObject: 'self',
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.ttf$/,
use: ['file-loader']
}
]
}
2018-03-14 21:44:58 +08:00
};
2018-03-14 23:36:14 +08:00
```
---
### Using parcel
2021-11-17 06:18:23 +08:00
A full working sample is available at https://github.com/microsoft/monaco-editor/tree/main/samples/browser-esm-parcel
2019-03-02 08:21:41 +08:00
When using parcel, we need to use the `getWorkerUrl` function and build the workers seperately from our main source. To simplify things, we can write a tiny bash script to build the workers for us.
2021-11-06 07:15:13 +08:00
- `index.js`
```js
import * as monaco from 'monaco-editor';
self.MonacoEnvironment = {
2021-11-06 07:15:13 +08:00
getWorkerUrl: function (moduleId, label) {
if (label === 'json') {
return './json.worker.js';
}
if (label === 'css' || label === 'scss' || label === 'less') {
return './css.worker.js';
}
if (label === 'html' || label === 'handlebars' || label === 'razor') {
return './html.worker.js';
}
if (label === 'typescript' || label === 'javascript') {
return './ts.worker.js';
}
return './editor.worker.js';
}
};
monaco.editor.create(document.getElementById('container'), {
2021-11-06 07:15:13 +08:00
value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
language: 'javascript'
});
```
2021-11-06 07:15:13 +08:00
- `build_workers.sh`
```sh
ROOT=$PWD/node_modules/monaco-editor/esm/vs
OPTS="--no-source-maps --log-level 1" # Parcel options - See: https://parceljs.org/cli.html
parcel build $ROOT/language/json/json.worker.js $OPTS
parcel build $ROOT/language/css/css.worker.js $OPTS
parcel build $ROOT/language/html/html.worker.js $OPTS
parcel build $ROOT/language/typescript/ts.worker.js $OPTS
parcel build $ROOT/editor/editor.worker.js $OPTS
```
Then, simply run `sh ./build_workers.sh && parcel index.html`. This builds the workers into the same directory as your main bundle (usually `./dist`). If you want to change the `--out-dir` of the workers, you must change the paths in `index.js` to reflect their new location.
2021-11-06 07:15:13 +08:00
_note - the `getWorkerUrl` paths are relative to the build directory of your src bundle_
---
### Using Vite
Adding monaco editor to [Vite](https://vitejs.dev/) is simple since it has built-in support for web workers. You only need to implement the `getWorker` function (NOT the `getWorkerUrl`) to use Vite's output ([Source](https://github.com/vitejs/vite/discussions/1791#discussioncomment-321046)):
```js
2021-11-06 07:15:13 +08:00
import * as monaco from 'monaco-editor';
self.MonacoEnvironment = {
2022-02-25 17:42:38 +08:00
getWorker: function (workerId, label) {
const getWorkerModule = (moduleUrl, label) => {
return new Worker(self.MonacoEnvironment.getWorkerUrl(moduleUrl), {
name: label,
type: 'module'
});
};
switch (label) {
case 'json':
return getWorkerModule('/monaco-editor/esm/vs/language/json/json.worker?worker', label);
case 'css':
case 'scss':
case 'less':
return getWorkerModule('/monaco-editor/esm/vs/language/css/css.worker?worker', label);
case 'html':
case 'handlebars':
case 'razor':
return getWorkerModule('/monaco-editor/esm/vs/language/html/html.worker?worker', label);
case 'typescript':
case 'javascript':
return getWorkerModule('/monaco-editor/esm/vs/language/typescript/ts.worker?worker', label);
default:
return getWorkerModule('/monaco-editor/esm/vs/editor/editor.worker?worker', label);
}
}
2021-11-06 07:15:13 +08:00
};
monaco.editor.create(document.getElementById('container'), {
2021-11-06 07:15:13 +08:00
value: "function hello() {\n\talert('Hello world!');\n}",
language: 'javascript'
});
```