vite-plugin-monaco-editor

repository·master·Indexed 42 days ago

https://github.com/vdesjs/vite-plugin-monaco-editor

A Vite plugin for the Monaco Editor that simplifies integration by automatically managing, bundling, and serving language workers. Version 1.1.0 supports custom worker definitions, CDN public paths, and selective language worker inclusion for optimized bundle sizes.

Tokens
1.7K
Snippets
9
Records
10
Agent score
54%

What's inside vite-plugin-monaco-editor

  1. Install vite-plugin-monaco-editor

    master

    Install the plugin as a development dependency using yarn or npm. Ensure you have monaco-editor already installed in your project.

    yarn add vite-plugin-monaco-editor -D
    
    # or
    
    npm install --save-dev vite-plugin-monaco-editor
    npm install --save-dev vite-plugin-monaco-editor
  2. Configure monacoEditorPlugin options

    master

    The monacoEditorPlugin() accepts several options to control worker bundling and serving:

    OptionTypeDefaultDescription
    languageWorkersstring[]['editorWorkerService', 'css', 'html', 'json', 'typescript']Include only a subset of supported language workers. Note: editorWorkerService must always be included.
    customWorkersIWorkerDefinition[][]Include custom workers. Example: [{label: "graphql", entry: "monaco-graphql/esm/graphql.worker"}]. entry can be a relative path in node_modules or an absolute path.
    publicPathstring'monacoeditorwork'Custom public path for worker scripts. Can be a CDN URL (e.g., https://unpkg.com/...).
    globalAPIbooleanfalseExposes the editor API via a global monaco object. Required for monaco-editor versions 0.22.0 and newer if using ESM.
    customDistPath(root: string, buildOutDir: string, base: string) => stringN/ACallback that returns a custom worker path.
    forceBuildCDNbooleanfalseIf using a CDN, the build is skipped by default. Set to true to force worker generation.

    Important: Shared Workers Some languages share workers. If you include a language, you must also include its instantiator:

    • javascript requires typescript
    • handlebars requires html
    • scss or less requires css
  3. Install and use vite-plugin-monaco-editor

    master

    To use Monaco Editor in a Vite project, import and initialize the monacoEditorPlugin in your vite.config.ts. This plugin handles the bundling of language workers and configures the MonacoEnvironment so the editor can correctly locate and load them.

    By default, the plugin includes all supported language workers. You can customize which workers are included, change the public path for serving files, or configure the global API behavior.

    import { defineConfig } from 'vite';
    import monacoEditorPlugin from 'vite-plugin-monaco-editor';
    
    export default defineConfig({
      plugins: [
        monacoEditorPlugin({
          // Configuration options go here
        })
      ]
    });
  4. Import partial Monaco Editor features to reduce bundle size

    master

    To avoid importing all features and languages, you can manually import specific ESM modules and the editor API. This is useful for optimizing your bundle.

    1. Create a custom entry point (e.g., customMonaco.ts):
    import 'monaco-editor/esm/vs/editor/editor.all.js';
    import 'monaco-editor/esm/vs/editor/standalone/browser/accessibilityHelp/accessibilityHelp.js';
    
    import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
    
    export { monaco };
    1. Use the custom entry point in your application:
    import { monaco } from './customMonaco.ts';
    
    monaco.editor.create(document.getElementById('container'), {
      value: 'console.log("Hello, world")',
      language: 'javascript',
    });
  5. Import all Monaco Editor features

    master

    To include all features and languages of the Monaco Editor, use a wildcard import from monaco-editor.

    import * as monaco from 'monaco-editor';
    
    monaco.editor.create(document.getElementById('container'), {
      value: 'console.log("Hello, world")',
      language: 'javascript',
    });
  6. Resolve a Monaco file path with resolveMonacoPath

    master

    The resolveMonacoPath function returns a resolved absolute path for a given Monaco file, attempting to locate it within the local node_modules directory first before falling back to standard resolution.

    import { resolveMonacoPath } from 'vite-plugin-monaco-editor';
    
    const filePath = resolveMonacoPath('monaco-editor/esm/vs/editor/editor.worker.js');
  7. Access language worker definitions via languageWorksByLabel

    master

    The languageWorksByLabel object allows you to look up the IWorkerDefinition (containing the label and the ESM entry path) using the language label as a key. This is useful when programmatically configuring which workers the plugin should bundle.

    // Example lookup
    const worker = languageWorksByLabel['typescript'];
    // Returns: { label: 'typescript', entry: 'monaco-editor/esm/vs/language/typescript/ts.worker' }
  8. Check if a path is a CDN with isCDN

    master

    The isCDN function determines if a given publicPath is a CDN URL. It returns true if the path starts with http:, https:, file:, or //.

    import { isCDN } from 'vite-plugin-monaco-editor';
    
    const isRemote = isCDN('https://cdn.example.com/monaco/'); // returns true
    const isLocal = isCDN('/assets/monaco/'); // returns false
  9. Available Editor Language Workers

    master

    The plugin provides a predefined list of language workers that can be used for Monaco Editor functionality. These workers are mapped via labels to their specific ESM entry points within the monaco-editor package.

    Supported EditorLanguageWorks labels include:

    • editorWorkerService (Core editor service)
    • css
    • html
    • json
    • typescript
    // Available labels for EditorLanguageWorks:
    // 'css' | 'html' | 'json' | 'typescript' | 'editorWorkerService'