Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions packages/monaco-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,27 +515,25 @@ then, in your application:

```ts
// Vite query suffixes https://vite.dev/guide/features.html#import-with-query-suffixes
// eslint-disable-next-line import-x/default
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import GraphQLWorker from './my-graphql.worker?worker';

globalThis.MonacoEnvironment = {
getWorker(_workerId: string, label: string) {
return label === 'graphql' ? new GraphQLWorker() : new EditorWorker();
},
};
```

or, if you have webpack configured for it:

```ts
globalThis.MonacoEnvironment = {
getWorkerUrl(_workerId: string, label: string) {
return label === 'graphql' ? 'my-graphql.worker.js' : 'editor.worker.js';
if (label === 'graphql') {
return new GraphQLWorker();
}
if (label === 'json') {
return new JsonWorker();
}
return new EditorWorker();
},
};
```

with vite you just need:
with vite, an alternative way is using a plugin like this:

```ts
import { defineConfig } from 'vite';
Expand All @@ -547,6 +545,8 @@ export default defineConfig({
customWorker: [
{
label: 'graphql',
// adjust if needed to be the path from node_modules
// i.e. ../src/my-graphql.worker.js
entry: 'my-graphql.worker.js',
},
],
Expand All @@ -555,6 +555,53 @@ export default defineConfig({
});
```

or, if you have webpack configured for it:

```ts
globalThis.MonacoEnvironment = {
getWorkerUrl(_workerId: string, label: string) {
if (label === 'graphql') {
return 'my-graphql.worker.js';
}
if (label === 'json') {
return 'json.worker.js';
}
return 'editor.worker.js';
},
};
```

or, if you have esbuild configured for it:

```ts
// add entry points to the esbuild script
const entryPoints = {
'my-graphql.worker': '../src/my-graphql.worker.ts',
'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
'json.worker': 'monaco-editor/esm/vs/language/json/json.worker.js',
};

// add to editor file
globalThis.MonacoEnvironment = {
getWorker(_workerId: string, label: string) {
const options: WorkerOptions = {
type: 'module',
name: label,
};
if (label === 'graphql') {
const url = new URL('./my-graphql.worker.js', import.meta.url);
return worker;
}
if (label === 'json') {
const url = new URL('./json.worker.js', import.meta.url);
return new Worker(url, options);
}
const url = new URL('./editor.worker.js', import.meta.url);
return new Worker(url, options);
},
};
```

## Monaco Editor Tips

If you are familiar with Codemirror/Atom-era terminology and features, here's
Expand Down