Giter Club home page Giter Club logo

vite-plugin-monaco-editor's Introduction

Vite Plugin Monaco Editor

A plugin to simplify loading the Monaco Editor with vite.

  • It uses Vite specific plugin hooks: configResolved, configureServer, transformIndexHtml.
  • It uses esbuild to bundle worker in the node_modules/.monaco directory, via the server.middlewares proxy http server for the bundle worker.

Installing

// make sure you have it installed monaco-editor.

yarn add vite-plugin-monaco-editor -D

// or
npm install --save-dev vite-plugin-monaco-editor

Using

  • vite.config.ts:
import { defineConfig } from 'vite';
import monacoEditorPlugin from 'vite-plugin-monaco-editor';

export default defineConfig({
  plugins: [monacoEditorPlugin()],
});

Import all monaco functions

  • index.ts:
import * as monaco from 'monaco-editor';

monaco.editor.create(document.getElementById('container'), {
  value: 'console.log("Hello, world")',
  language: 'javascript',
});

Import part of monaco functions

The import * as monaco from 'monaco-editor' is import all features and languages of the Monaco Editor. Assume you only need part of the features and languages:

  • 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 };

The Complete list of imports: customMonaco.ts

  • index.ts
import { monaco } from './customMonaco.ts';
monaco.editor.create(document.getElementById('container'), {
  value: 'console.log("Hello, world")',
  language: 'javascript',
});

Options

  • languageWorkers (string[]) - include only a subset of the languageWorkers supported.

    • default value: ['editorWorkerService', 'css', 'html', 'json', 'typescript'].
    • Assuming only use css worker(editorWorkerService is must include base worker), you can set ['editorWorkerService', 'css']
  • customWorkers (IWorkerDefinition[]) - include your custom worker.

    • default value: []
    • example value: [{label: "graphql", entry: "monaco-graphql/esm/graphql.worker"}], The entry is relative path in the node_modules Or you can set absolute path.
  • publicPath (string) - custom public path for worker scripts, overrides the public path from which files generated by this plugin will be served. Or you can set CDN e.g https://unpkg.com/[email protected]/cdn

    • default value: monacoeditorwork
  • globalAPI (boolean) - specifies whether the editor API should be exposed through a global monaco object or not. This option is applicable to 0.22.0 and newer version of monaco-editor. Since 0.22.0, the ESM version of the monaco editor does no longer define a global monaco object unless global.MonacoEnvironment = { globalAPI: true } is set (change log).

    • default value: false.
  • customDistPath ((root: string, buildOutDir: string, base: string) => string) - Custom callback returns the worker path

  • forceBuildCDN (boolean) - If you use CDN, the build is skipped by default. Set to true if you want to generate woker

    • default value: false

Some languages share the same web worker. If one of the following languages is included, you must also include the language responsible for instantiating their shared worker:

Language Instantiator
javascript typescript
handlebars html
scss, less css

vite-plugin-monaco-editor's People

Contributors

china-bin avatar akarachen avatar kotofeyskaya avatar ethan-arrowood avatar zlyyyy avatar martinbuessemeyer avatar mattcroat avatar tanvesh01 avatar tarnishablec avatar windsonr avatar

Stargazers

Ng Bin Chong  avatar

Forkers

aoxiang78

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.