@akrc/vite-plugin-monaco-editor

中文文档
3.0.0发布于 2024/05/06MITTypeScriptES Module近 30 天下载 5739

A vite plugin for the Monaco Editor

原始使用文档
文档目录5 节
  1. Installing
  2. Using
  3. Import all monaco functions
  4. Import part of monaco functions
  5. Options

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:

LanguageInstantiator
javascripttypescript
handlebarshtml
scss, lesscss

版本与发布记录

@akrc/vite-plugin-monaco-editor 共发布 3 个版本,首次发布于 2023/12/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2024/05/062 年前
2.0.12023/12/062 年前
2.0.02023/12/062 年前

关于 @akrc/vite-plugin-monaco-editor 的常见问题

@akrc/vite-plugin-monaco-editor 是什么?

A vite plugin for the Monaco Editor

如何安装 @akrc/vite-plugin-monaco-editor?

在项目目录运行 npm install @akrc/vite-plugin-monaco-editor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:monaco-editor(>=0.33.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@akrc/vite-plugin-monaco-editor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@akrc/vite-plugin-monaco-editor 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@akrc/vite-plugin-monaco-editor 可以商用吗?

@akrc/vite-plugin-monaco-editor 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@akrc/vite-plugin-monaco-editor 的下载量有多少?

根据 npm 官方下载统计,@akrc/vite-plugin-monaco-editor 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,739 次,日均约 191 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@akrc/vite-plugin-monaco-editor 还在维护吗?

最新版本 3.0.0 发布于 2024/05/06(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。