Skip to content

Commit 64eed3a

Browse files
ttys026Copilot
andcommitted
feat: add worker factory option for Monaco Editor >= 0.55
Monaco Editor 0.55 changed the createWebWorker API to require a Worker instance via opts.worker. Additionally, createData is no longer sent through the worker message protocol. This adds a `worker` option to `configureMonacoYaml()` that allows consumers to provide their own Worker factory function. When provided, the worker manager intercepts the postMessage sequence to inject createData before the RPC INITIALIZE message, preserving compatibility with the existing yaml worker protocol. For Monaco < 0.55, omit the worker option and use MonacoEnvironment as before. Updated README with usage examples for both Vite and Webpack. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e5a3014 commit 64eed3a

File tree

4 files changed

+270
-13
lines changed

4 files changed

+270
-13
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ offers to configure the YAML language support.
3131
- [API](#api)
3232
- [`configureMonacoYaml(monaco, options?)`](#configuremonacoyamlmonaco-options)
3333
- [FAQ](#faq)
34+
- [Using with Monaco Editor >= 0.55](#using-with-monaco-editor--055)
3435
- [Does this work with the Monaco UMD bundle?](#does-this-work-with-the-monaco-umd-bundle)
3536
- [Does this work with Monaco Editor from a CDN?](#does-this-work-with-monaco-editor-from-a-cdn)
3637
- [Does this work with `@monaco-editor/loader` or `@monaco-editor/react`?](#does-this-work-with-monaco-editorloader-or-monaco-editorreact)
@@ -191,6 +192,9 @@ Configure `monaco-yaml`.
191192
- `schemas` (`object[]`): A list of known schemas and/or associations of schemas to file names.
192193
(Default: `[]`)
193194
- `validate` (`boolean`): based validation. (Default: `true`)
195+
- `worker` (`() => Worker`): A factory function that creates a new `Worker` for the YAML language
196+
service. Required for Monaco Editor >= 0.55. See
197+
[Using with Monaco Editor >= 0.55](#using-with-monaco-editor--055).
194198
- `yamlVersion` (`'1.1' | '1.2'`): The YAML version to use for parsing. (Default: `1,2`)
195199
196200
#### Returns
@@ -199,6 +203,66 @@ An object that can be used to dispose or update `monaco-yaml`.
199203
200204
## FAQ
201205
206+
### Using with Monaco Editor >= 0.55
207+
208+
Monaco Editor **0.55** introduced a breaking change to the `createWebWorker` API: the `Worker`
209+
instance must now be provided directly by the caller. Previous versions created the worker internally
210+
via `MonacoEnvironment.getWorker` / `MonacoEnvironment.getWorkerUrl`.
211+
212+
To support Monaco Editor >= 0.55, pass the `worker` option — a factory function that creates a new
213+
`Worker` — to `configureMonacoYaml`. You no longer need to set up `MonacoEnvironment.getWorker` for
214+
the `yaml` label.
215+
216+
**Using Vite:**
217+
218+
Create a local worker wrapper (required because Vite's dependency pre-bundling can break the worker
219+
protocol):
220+
221+
```js
222+
// yaml.worker.js
223+
import 'monaco-yaml/yaml.worker.js'
224+
```
225+
226+
Then pass the worker factory:
227+
228+
```typescript
229+
import * as monaco from 'monaco-editor'
230+
import { configureMonacoYaml } from 'monaco-yaml'
231+
import YamlWorker from './yaml.worker?worker'
232+
233+
configureMonacoYaml(monaco, {
234+
worker: () => new YamlWorker(),
235+
enableSchemaRequest: true,
236+
schemas: [
237+
{
238+
fileMatch: ['**/.prettierrc.*'],
239+
uri: 'https://json.schemastore.org/prettierrc.json'
240+
}
241+
]
242+
})
243+
```
244+
245+
**Using Webpack 5:**
246+
247+
```typescript
248+
import * as monaco from 'monaco-editor'
249+
import { configureMonacoYaml } from 'monaco-yaml'
250+
251+
configureMonacoYaml(monaco, {
252+
worker: () => new Worker(new URL('monaco-yaml/yaml.worker', import.meta.url)),
253+
enableSchemaRequest: true,
254+
schemas: [
255+
{
256+
fileMatch: ['**/.prettierrc.*'],
257+
uri: 'https://json.schemastore.org/prettierrc.json'
258+
}
259+
]
260+
})
261+
```
262+
263+
For Monaco Editor < 0.55, omit the `worker` option and configure `MonacoEnvironment.getWorker` as
264+
described in the [Usage](#usage) section above.
265+
202266
### Does this work with the Monaco UMD bundle?
203267
204268
Yes, starting from version 5.0.0

package-lock.json

Lines changed: 55 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"@vitest/browser-playwright": "^4.0.0",
6060
"@vitest/coverage-v8": "^4.0.0",
6161
"esbuild": "^0.27.0",
62-
"monaco-editor": "^0.52.0",
62+
"monaco-editor": "^0.55.1",
6363
"playwright": "^1.0.0",
6464
"remark-cli": "^12.0.0",
6565
"remark-preset-remcohaszing": "^3.0.0",

0 commit comments

Comments
 (0)