# Monaco 编辑器

**Monaco 编辑器**是来自 [Visual Studio Code](https://www.edgepedia.org/zh/visual-studio-code) 的功能齐全的代码编辑器，直接由 VS Code 的源代码生成，并在代码所需的服务周围加上垫片，以便在 VS Code 之外的网页浏览器中运行<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。它作为一款 MIT 许可的库而非产品分发：开箱即支持语法与语义校验、内容辅助、语法着色、参数提示和悬停提示，但不承载 VS Code 扩展<sup>[1](https://github.com/microsoft/monaco-editor/)</sup><sup> • </sup><sup>[2](https://www.npmjs.com/package/monaco-editor)</sup><sup> • </sup><sup>[3](https://www.typefox.io/blog/how-to-embed-a-monaco-editor-in-a-browser-as-a-part-of-my-first-task-at-typefox/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 它是什么 | VS Code 编辑器核心重新打包为浏览器库，并在 VS Code 之外所需的服务周围加上垫片<sup>[1](https://github.com/microsoft/monaco-editor/)</sup> |
| 许可与软件包 | MIT，微软公司的 npm 包 `monaco-editor`，版本 0.57.0，每周约 9,958,337 次下载，2,847 个依赖者<sup>[2](https://www.npmjs.com/package/monaco-editor)</sup> |
| 版本管理 | Monaco 版本与 VS Code 发布版本没有关系；该库直接反映源代码<sup>[1](https://github.com/microsoft/monaco-editor/)</sup> |
| 语言智能 | 语言服务创建 Web Worker，在 UI 线程之外执行繁重的计算；部分功能依赖文件 URI，例如 TypeScript 导入解析和 JSON 模式应用<sup>[1](https://github.com/microsoft/monaco-editor/)</sup> |
| 包体下限 | 一位维护者估计，在 gzip 压缩前，压缩后的最小体积约为 1.5 MB，因为小地图等组件无法通过功能标志移除<sup>[12](https://github.com/microsoft/monaco-editor/issues/2730)</sup> |
| 移动端支持 | 不支持移动浏览器或移动 Web 应用框架<sup>[1](https://github.com/microsoft/monaco-editor/)</sup> |
| 打包 | 在 `/esm` 中提供 ESM 版本，外加一个 `monaco.d.ts` API 文件；AMD 构建已弃用，将在未来版本中移除<sup>[1](https://github.com/microsoft/monaco-editor/)</sup> |

## 与 Visual Studio Code 的关系

Monaco 和 VS Code 共享同一个编辑器核心，但它们是不同的产物。VS Code 是作为一款多语言、跨平台的轻量级代码编辑器，构建在现代 Web 技术栈之上，其部分开发受到了开发 Eclipse 平台经验的影响<sup>[4](https://emisa-journal.org/emisa/article/download/320/210)</sup>。一篇 2024 年 IEEE 会议论文将 Monaco 描述为驱动 Visual Studio Code 的核心组件<sup>[5](https://doi.org/10.1109/icosse62619.2024.00016)</sup>。

对集成者而言，实际差异在于扩展模型。VS Code 扩展无法在浏览器中的 Monaco 里运行；仓库的 FAQ 对此给出了干脆的否定回答，但附加了一个限定条件：完全基于[语言服务器协议](https://www.edgepedia.org/zh/language-server-protocol)、使用 [JavaScript](https://www.edgepedia.org/zh/javascript) 语言服务器的扩展可以工作<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。底层设计原因出现在 VS Code 编辑器设计文档中：扩展在自己的宿主进程中运行，被禁止直接访问 View 和 ViewModel，而是接收 Model——这个与 UI 无关的对象持有文件内容、元数据、词元、标记和装饰<sup>[6](https://github.com/microsoft/vscode/wiki/%5BWIP%5D-Code-Editor-Design-Doc)</sup>。

npm 包创建于 2016-05-31，GitHub 仓库创建于 2016-06-07；仓库拥有约 46,847 个星标、4,154 个复刻和 873 个未关闭议题<sup>[2](https://www.npmjs.com/package/monaco-editor)</sup><sup> • </sup><sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。

## 架构与语言智能

Monaco 的架构分为三层。Model 是数据和业务逻辑，完全独立于 UI，持有文件的内容和元数据、词元和标记。ViewModel 和 View 负责呈现，渲染进程仅在必要时渲染视图 DOM 元素，监听 ViewModel 事件并在真实视图变化时重新渲染<sup>[6](https://github.com/microsoft/vscode/wiki/%5BWIP%5D-Code-Editor-Design-Doc)</sup>。Monaco 还已经内置了一套用于常见场景（如 IntelliSense）的 UI 组件，位于 `/vs/editor/contrib/` 下<sup>[6](https://github.com/microsoft/vscode/wiki/%5BWIP%5D-Code-Editor-Design-Doc)</sup>。

**繁重工作由 Worker 承担。** 语言服务创建 Web Worker，在 UI 线程之外执行繁重的计算<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。部分智能功能依赖模型的文件 URI，例如 [TypeScript](https://www.edgepedia.org/zh/typescript) 导入解析和 JSON 模式的应用，这就是为什么跨文件智能需要注册多个具有正确 URI 的模型<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。对于自定义语言逻辑，Monaco 可以通过 [WebSocket](https://www.edgepedia.org/zh/websocket) 连接到语言服务器；在一篇 TypeFox 集成文章中，语言服务器协议当时不支持语法着色，因此着色必须在客户端 JavaScript 中单独配置<sup>[3](https://www.typefox.io/blog/how-to-embed-a-monaco-editor-in-a-browser-as-a-part-of-my-first-task-at-typefox/)</sup>。

**提供程序与 Monarch。** 补全和悬停信息等智能编辑器功能通过提供程序 API 提供，这些 API 与语言服务器协议功能并不相同，但通常映射到它们<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。新语言的[语法高亮](https://www.edgepedia.org/zh/syntax-highlighting)用 Monarch 定义，这是一种声明式词法规范，写成一个 JSON 值；该规范表达力足够强，可以定义具有复杂状态转换、动态括号匹配、自动补全和嵌入语言的高级高亮器<sup>[7](https://microsoft.github.io/monaco-editor/monarch-static.html)</sup>。

## 集成与定制

**ESM 集成。** 自 0.22.0 版本起，除非设置 `global.MonacoEnvironment = { globalAPI: true }`，ESM 构建不再定义全局 `monaco` 对象<sup>[8](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md)</sup>。`Environment` 接口暴露可选的 `baseUrl`、`globalAPI`、`createTrustedTypesPolicy`、`getWorker` 和 `getWorkerUrl` 属性，用于配置 Worker 加载和受信类型<sup>[9](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor_editor_api.Environment.html)</sup>。

Worker 接线因打包器而异。使用 webpack 时，每种语言的 Worker 从 `monaco-editor/esm/vs/language/...` 路径打包，并将 `output.globalObject` 设置为 `'self'`，`MonacoEnvironment.getWorkerUrl` 将语言标签（json、css/scss/less、html、typescript/javascript）映射到各语言的 Worker 包，外加一个默认编辑器 Worker 包<sup>[10](https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md)</sup>。使用内置 Web Worker 支持的 Vite 时，集成者使用 Vite 的 `?worker` 导入为每种语言 Worker 实现 `getWorker`（而非 `getWorkerUrl`）<sup>[10](https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md)</sup>。

部分语言共享同一个 Web Worker：javascript 需要 typescript Worker，handlebars 需要 html Worker，scss/less 需要 css Worker，因此包含其中之一也需要包含负责共享 Worker 的语言<sup>[11](https://github.com/microsoft/monaco-editor/blob/main/webpack-plugin/README.md)</sup>。

**已知陷阱。** HTML5 不允许在 `file://` 上加载的页面创建 Web Worker，因此编辑器必须通过 `http://` 或 `https://` 提供<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。当 Worker 无法启动时，Monaco 回退到在主线程中加载 Web Worker 代码，这可能导致 UI 冻结<sup>[23](https://stackoverflow.com/questions/78761371/how-to-write-a-custom-language-worker-with-typescript)</sup>。对于自定义语言 Worker，社区指导是不要在 Worker 内导入 Monaco 特定模块：这会导致难以解决的启动问题，并使通常会被多次实例化的包体膨胀<sup>[23](https://stackoverflow.com/questions/78761371/how-to-write-a-custom-language-worker-with-typescript)</sup>。

## 包体大小与性能

按照浏览器内编辑器的标准，Monaco 体积庞大，而且下限很高。一位维护者在议题跟踪器中表示，gzip 压缩前压缩后的最小包体约为 1.5 MB，因为小地图和标尺等功能无法通过功能标志移除<sup>[12](https://github.com/microsoft/monaco-editor/issues/2730)</sup>。Miro 的工程团队在他们开发时测得，最小压缩加 gzip 压缩后的包体超过 2 MB<sup>[13](https://medium.com/miro-engineering/how-we-integrated-a-code-editor-on-the-miro-canvas-a41e0eff7f21)</sup>。

实际载荷接近这些下限。Sourcegraph 报告称，即使将其 Monaco 包优化以移除未使用的功能后，仅 Monaco 一项就是 2.4 MB 的下载量，占其搜索页面上所有 JavaScript 的 40%；移除 Monaco 后，页面的 JavaScript 下载量降至 3.4 MB，仅替换一个依赖项就带来了 43% 的改善<sup>[14](https://sourcegraph.com/blog/migrating-monaco-codemirror)</sup>。Replit 报告称，`monaco-editor` 及相关库为其包体贡献了 51.17 MB（解析加 gzip 压缩后 5.01 MB），另外还称 Monaco 为其工作区包体增加了约 5 MB 未压缩体积，并且需要作为 [Webpack](https://www.edgepedia.org/zh/webpack) DLL 预编译以及大量 Webpack 配置才能集成<sup>[15](https://replit.com/blog/codemirror)</sup><sup> • </sup><sup>[16](https://replit.com/blog/code-editors)</sup>。

**缓解策略**包括子集化和预取。官方的 `monaco-editor-webpack-plugin`（每周 474.9K 次下载，307 个依赖者）通过仅选择特定语言或功能来生成更小的包体；其选项包括 `languages`（默认全部）、`features`（可用 `!` 前缀排除）、`filename`、`publicPath` 和 `globalAPI`<sup>[17](https://registry.npmjs.org/monaco-editor-webpack-plugin)</sup><sup> • </sup><sup>[11](https://github.com/microsoft/monaco-editor/blob/main/webpack-plugin/README.md)</sup>。Cloud Foundry Stratos 项目测量了各种替代方案：完整的 `editor.main` 构建捆绑了大约 80 种语言以及 css、html 和 typescript 语言服务，动态导入每个功能将编辑器打碎成约 170 个微块，反而增加了 gzip 成本（871 KB 对 868 KB）。相反，精心策划一个 Monaco 子集，将编辑器打开时的载荷从 4 个块共 3.38 MB 原始 / 868 KB gzip 压缩后，降至 3 个块共 3.07 MB 原始 / 785 KB gzip 压缩后，并由生产脚本预算守护（警告 2.5 MB / 错误 3 MB），同时使用 `requestIdleCallback` 预取，使首次编辑器界面打开时无需在点击时再等待下载<sup>[18](https://github.com/cloudfoundry/stratos/pull/5826)</sup>。

## 与 CodeMirror 和 Ace 的比较

权衡在于功能深度与体积和灵活性之间。Monaco 开箱即提供精致的 UI，对 HTML、CSS 和 JavaScript 有非常好的 IntelliSense<sup>[16](https://replit.com/blog/code-editors)</sup>。与此相对，Replit 列举了离开 Monaco 的四个理由：缺乏移动端支持、API 笨拙且文档不佳、难以定制核心组件、以及庞大的包体拖累性能；他们判断 Monaco 在移动端不可用，同时推荐 [CodeMirror](https://www.edgepedia.org/zh/codemirror) 6 用于移动端支持<sup>[15](https://replit.com/blog/codemirror)</sup><sup> • </sup><sup>[16](https://replit.com/blog/code-editors)</sup>。官方仓库也陈述了同样的限制：Monaco 不支持移动浏览器或移动 Web 应用框架<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。

CodeMirror 6 是一次完全重写，专注于可访问性、触摸屏支持和可扩展性，具有一个小的通用核心，几乎所有编辑器功能都作为扩展实现在可组合模块（如 view、state 和 language）中<sup>[15](https://replit.com/blog/codemirror)</sup>。Replit 测得，包含所有扩展和语言包的 CodeMirror 6 为 8.23 MB（解析加 gzip 压缩后 1.26 MB），而 Monaco 为 51.17 MB（解析加 gzip 压缩后 5.01 MB）<sup>[15](https://replit.com/blog/codemirror)</sup>。Miro 的比较显示，CodeMirror 6 压缩加 gzip 压缩后为 124 KB，CodeMirror 5 为 55.7 KB，Ace 为 98 KB，而 Monaco 超过 2 MB；Miro 选择 Ace 用于画布内嵌代码小部件，因为它轻量、开箱即支持缩放、提供静态语法高亮扩展，并且已经过十多年的实战检验<sup>[13](https://medium.com/miro-engineering/how-we-integrated-a-code-editor-on-the-miro-canvas-a41e0eff7f21)</sup>。

当一个页面包含多个编辑器时，这种区别很重要：Monaco 将其语言定义存储在单个共享的全局状态中，该语言驱动语法高亮、自动补全和其他编辑器功能，而每个 CodeMirror 实例完全独立，仅依赖自己的本地状态。<sup>[14](https://sourcegraph.com/blog/migrating-monaco-codemirror)</sup> Replit 还报告称，Monaco 的扩展点有些有限且特定，低配机器上的用户在使用 Monaco 时深受性能问题之苦<sup>[16](https://replit.com/blog/code-editors)</sup>。

## 发布与近期维护

在 VS Code 切换到 ESM 后，Monaco 的发布停滞了。维护者 Alexandru Dima 在仓库的讨论跟踪器中解释说，Monaco 严重依赖 Web Worker 的架构方式，并提供 AMD 打包的源代码，而底层 VS Code 源代码不再是 AMD<sup>[19](https://github.com/microsoft/monaco-editor/discussions/4910)</sup>。他指出，维护者 `@hediet` 一直在努力恢复 Monaco 编辑器的 ESM 变体，可能作为测试版发布，直到可以从核心 ESM 源代码生成伪 AMD 打包源代码<sup>[19](https://github.com/microsoft/monaco-editor/discussions/4910)</sup>。开发版发布恢复，0.53.0-rc 候选版本的流水线启动，结束了自上一年年底以来持续的中断<sup>[19](https://github.com/microsoft/monaco-editor/discussions/4910)</sup>。

随后的发布重塑了打包方式。版本 0.52.0 于 2024-09-19 发布，移除了 `editor.main.nls.js`<sup>[20](https://github.com/microsoft/monaco-editor/releases/tag/v0.52.0)</sup>。版本 0.53.0 弃用 AMD 构建并停止支持它，建议迁移到 ESM 构建；未打包的同步脚本导入的浏览器脚本编辑器场景不再工作，应使用 Vite 或 webpack 等打包器<sup>[8](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md)</sup>。同一版本添加了 Next Edit Suggestion 支持、中键单击滚动、编辑上下文支持、`ariaContainerElement` 选项、`guides.bracketPairs`、限制差异计算的 `maxFileSize` 选项、`CodeActionProvider.resolveCodeAction` 以及 TypeScript 内联提示支持<sup>[8](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md)</sup>。后来的一个提交将 ESM 构建切换到 rollup 进行 d.ts 打包，并将 `monaco.languages.{typescript, json, html, css}` 移至 `monaco.*`，将 `monaco.editor.createWebWorker` 移至 `monaco.createWebWorker`<sup>[21](https://github.com/microsoft/monaco-editor/commit/5a7e91758731415795599cbc4f217a67e3606997)</sup>。版本 0.55.0 通过新的 `lsp` 命名空间添加了原生 LSP 支持，并将嵌套命名空间（`languages.css`、`languages.html`、`languages.json`、`languages.typescript`）移至顶层命名空间以简化构建过程<sup>[8](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md)</sup>。

维护在微软旗下仍然活跃：npm 包版本为 0.57.0，最后更新于 2026-09-24，每周约 996 万次下载<sup>[2](https://www.npmjs.com/package/monaco-editor)</sup>，0.57.0 的发布准备工作在仓库的议题跟踪器中跟踪<sup>[22](https://github.com/microsoft/monaco-editor/releases)</sup>。

## 许可与限制

Monaco 编辑器在仓库和 npm 包中均采用 MIT 许可<sup>[1](https://github.com/microsoft/monaco-editor/)</sup><sup> • </sup><sup>[2](https://www.npmjs.com/package/monaco-editor)</sup>。复用的主要限制是上述扩展边界：VS Code 扩展无法在浏览器中的 Monaco 里运行，维护者指出了基于 LSP 的例外情况<sup>[1](https://github.com/microsoft/monaco-editor/)</sup>。

## References

1. [microsoft/monaco-editor (GitHub repository)](https://github.com/microsoft/monaco-editor/)
2. [monaco-editor on npm](https://www.npmjs.com/package/monaco-editor)
3. [How to embed a Monaco Editor in a browser — TypeFox](https://www.typefox.io/blog/how-to-embed-a-monaco-editor-in-a-browser-as-a-part-of-my-first-task-at-typefox/)
4. [Language Server Protocol (EMISA Journal article)](https://emisa-journal.org/emisa/article/download/320/210)
5. [Comparison of Modern Customizable Code Visualization Tools (ICOSSE 2024)](https://doi.org/10.1109/icosse62619.2024.00016)
6. [[WIP] Code Editor Design Doc — microsoft/vscode Wiki](https://github.com/microsoft/vscode/wiki/%5BWIP%5D-Code-Editor-Design-Doc)
7. [Monarch: declarative syntax highlighting specification](https://microsoft.github.io/monaco-editor/monarch-static.html)
8. [Monaco Editor CHANGELOG](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md)
9. [Monaco Editor API — Environment interface](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor_editor_api.Environment.html)
10. [Monaco Editor ESM integration guide](https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md)
11. [monaco-editor-webpack-plugin README](https://github.com/microsoft/monaco-editor/blob/main/webpack-plugin/README.md)
12. [Smallest possible bundle size for Markdown · Issue #2730](https://github.com/microsoft/monaco-editor/issues/2730)
13. [How we integrated a code editor on the Miro canvas](https://medium.com/miro-engineering/how-we-integrated-a-code-editor-on-the-miro-canvas-a41e0eff7f21)
14. [Migrating from Monaco Editor to CodeMirror | Sourcegraph](https://sourcegraph.com/blog/migrating-monaco-codemirror)
15. [Betting on CodeMirror | Replit](https://replit.com/blog/codemirror)
16. [Comparing Code Editors: Ace, CodeMirror and Monaco | Replit](https://replit.com/blog/code-editors)
17. [monaco-editor-webpack-plugin, npm registry](https://registry.npmjs.org/monaco-editor-webpack-plugin)
18. [perf(editor): curate the monaco subset, prefetch when idle — Stratos PR #5826](https://github.com/cloudfoundry/stratos/pull/5826)
19. [Is monaco-editor dead? · Discussion #4910](https://github.com/microsoft/monaco-editor/discussions/4910)
20. [v0.52.0 release](https://github.com/microsoft/monaco-editor/releases/tag/v0.52.0)
21. [Commit 5a7e917 — Uses rollup for ESM build and d.ts bundling (#5048)](https://github.com/microsoft/monaco-editor/commit/5a7e91758731415795599cbc4f217a67e3606997)
22. [Releases · microsoft/monaco-editor](https://github.com/microsoft/monaco-editor/releases)
23. [How to write a custom language worker with TypeScript (Stack Overflow, 2024)](https://stackoverflow.com/questions/78761371/how-to-write-a-custom-language-worker-with-typescript)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/monaco-editor

English original: https://www.edgepedia.org/monaco-editor
