@nx-lang/monaco

中文文档
0.10.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证

One Monaco integration for NX: highlighting from the published grammar through Shiki, hover and completion over an NxLanguageService, and diagnostics-to-marker mapping.

原始使用文档
文档目录5 节
  1. Peer dependencies
  2. registerNxLanguage(monaco, options?)
  3. With @monaco-editor/react
  4. Markers
  5. Exports

@nx-lang/monaco

One Monaco integration for NX, shared by every NX web editor: the nx language and its configuration from @nx-lang/language, highlighting through Shiki with that package's TextMate grammar, hover and completion over an NxLanguageService, and a mapping from protocol diagnostics to editor markers. Framework-free — a host composes it with monaco.editor.create or @monaco-editor/react as it likes.

import * as monaco from "monaco-editor";
import { registerNxLanguage } from "@nx-lang/monaco";
import { createHttpLanguageService } from "@nx-lang/language-client";

const registration = registerNxLanguage(monaco, {
  service: createHttpLanguageService({ baseUrl: "/api/language" }),
});

const editor = monaco.editor.create(container, {
  value: source,
  language: "nx",
  theme: "github-dark",
});

Peer dependencies

The host installs monaco-editor, shiki, @shikijs/monaco, and @nx-lang/language, so there is one Monaco on the page and the host controls versions. Monaco 0.52.0 and later are supported: every API the package calls exists there, and the package is compiled against 0.52's declarations. Until @nx-lang/language is on the registry, a host consuming this repository links it by path, the way this repository's workspace does ("@nx-lang/language": "file:../../src/vscode").

The package never imports monaco-editor itself; it only uses the namespace it is handed. That namespace may be an ES module import, what @monaco-editor/react passes to beforeMount, or the monaco global an AMD-loaded Monaco leaves on window — BlazorMonaco loads it that way — so a host with its own Monaco never ends up with a second copy on the page.

// An AMD-loaded Monaco: the global is the namespace.
registerNxLanguage(window.monaco, { service });

registerNxLanguage(monaco, options?)

Registers NX with the given Monaco namespace and returns a handle with dispose() and a ready promise that resolves once the highlighter is loaded.

OptionDefaultMeaning
servicenoneAn NxLanguageService. With one, hover and completion providers are registered; without, only highlighting and language configuration.
workspacethe model alone(model) => LanguageDocument[]: the document set a query about model is asked against.
themes["github-light", "github-dark"]Shiki themes to load and define in Monaco, by bundled name or theme object. Pick one with the editor's theme option or monaco.editor.setTheme; without a pick, the first becomes active once the highlighter loads.
onErrornoneCalled when a service call rejects or the highlighter fails to load. Nothing is thrown into Monaco.

Doc comments. Every theme loaded gets rules for the Markdown in /// doc comments, so it reads the way documentation does in other editors rather than the way a Markdown file does: **bold** and *italic* keep the comment's color and change only the font, a code span takes the theme's code-span color, and a doc link such as [Name] takes that color with an underline (the comment's color, underlined, in a theme with no code-span color). withNxDocCommentStyles(theme) applies the same rules to a theme object, for a host that defines its themes some other way.

Idempotent. A repeat call on the same Monaco namespace returns a handle onto the existing registration rather than registering again, so @monaco-editor/react's beforeMount and onMount pair, and React strict mode's double mount, register one set of providers and every completion appears once. A call without a service registers highlighting only; the first later call that brings a service adds the hover and completion providers to the existing registration, so a host that sets up highlighting before its service is ready still gets them. Providers are removed when the last handle is disposed; the language, its configuration and the highlighter stay, because Monaco has no way to unregister a language, and a registration made after that reuses them rather than loading the highlighter again. A React effect that registers on mount and disposes on cleanup therefore costs one highlighter load, however often it runs.

Themes. The highlighter loads after registerNxLanguage returns. A theme chosen before ready resolves, through monaco.editor.setTheme or an editor's theme option, is kept once it has, so neither example above needs to await ready for its theme to hold. A theme that is not one of themes stays the editor's theme, but the highlighter cannot recolor for it and stays on its first.

Positions. Monaco's one-based line and column become the protocol's zero-based line and UTF-16 character, and answer ranges come back the other way. The model's text is read at the moment of the query, so an edit followed by an immediate hover asks about the edited text. Monaco's cancellation token aborts the service call.

Hover code blocks are highlighted by Monaco's own colorizer through the tokens provider Shiki installs, with the same grammar as the buffer.

With @monaco-editor/react

import Editor, { type BeforeMount, type OnMount } from "@monaco-editor/react";
import { registerNxLanguage, setNxMarkers } from "@nx-lang/monaco";
import { createHttpLanguageService } from "@nx-lang/language-client";

const service = createHttpLanguageService({
  baseUrl: "/api/language",
  headers: async () => ({ authorization: `Bearer ${await getToken()}` }),
});

// A multi-file configuration: the model being edited plus its siblings, with unsaved text.
const beforeMount: BeforeMount = (monaco) => {
  registerNxLanguage(monaco, {
    service,
    workspace: (model) => [
      { uri: model.uri.toString(), source: model.getValue(), version: model.getVersionId() },
      ...otherFiles.map((file) => ({ uri: file.uri, source: file.draftText })),
    ],
    onError: (error) => console.warn("nx language", error),
  });
};

const onMount: OnMount = (editor, monaco) => {
  const model = editor.getModel();
  if (model) {
    setNxMarkers(monaco, model, diagnosticsForThisFile);
  }
};

<Editor language="nx" theme="github-dark" beforeMount={beforeMount} onMount={onMount} value={text} />;

Markers

toMonacoMarkers(diagnostics) converts protocol diagnostics — or anything with severity, message, code and a range that may be null — to IMarkerData[]: severity mapped to Monaco's, columns one-based, a zero-width range widened by one column so an insertion-point diagnostic (expected } here) stays visible, and a range-less diagnostic omitted. setNxMarkers(monaco, model, diagnostics, owner = "nx") sets them on a model.

Exports

registerNxLanguage, NX_LANGUAGE_ID, NX_DEFAULT_LIGHT_THEME, NX_DEFAULT_DARK_THEME, NX_COMPLETION_TRIGGER_CHARACTERS, defaultWorkspace, toMonacoLanguageConfiguration, nxShikiLanguage, toProtocolPosition, toMonacoRange, toMonacoCompletionKind, toMonacoMarkers, setNxMarkers, and the types MonacoNamespace, RegisterNxLanguageOptions, NxLanguageRegistration, NxMonacoTheme, MarkerDiagnostic, NxLanguageService.

版本与发布记录

@nx-lang/monaco 共发布 10 个版本,首次发布于 2026/09/15;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/11/021 天前
0.9.02026/10/268 天前
0.8.02026/10/1816 天前
0.7.02026/10/1024 天前
0.6.02026/10/0628 天前
0.5.02026/09/291 个月前

关于 @nx-lang/monaco 的常见问题

@nx-lang/monaco 是什么?

针对NX的一个Monaco集成:通过Shiki突出显示已发布的语法,在NxLanguageService上悬停和补全,以及从诊断到标记的映射。

如何安装 @nx-lang/monaco?

在项目目录运行 npm install @nx-lang/monaco,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:shiki(^4.0.0)、monaco-editor(>=0.52.0)、@shikijs/monaco(^4.0.0)、@nx-lang/language(>=0.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@nx-lang/monaco 支持 TypeScript 吗?

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

@nx-lang/monaco 支持 ESM 还是 CommonJS?

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

@nx-lang/monaco 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@nx-lang/monaco 可以商用吗?

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

@nx-lang/monaco 还在维护吗?

最新版本 0.10.0 发布于 2026/11/02(1 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/09/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。