Skip to content

[lexical-website] Documentation Update: document that Lexical is a singleton per app and libraries must declare it as a peer dependency - #9125

Merged
potatowagon merged 1 commit into
mainfrom
potatowagon/docs-one-lexical-instance-peer-deps
Sep 8, 2026
Merged

potatowagon merged 1 commit into
mainfrom
potatowagon/docs-one-lexical-instance-peer-deps

Conversation

@potatowagon

@potatowagon potatowagon commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

Nothing in the docs states the rule that an app must resolve exactly one copy of
lexical, or what a library built on Lexical should do about it. The failure
mode is common and the errors it produces point nowhere near the cause — a node
class that plainly implements getType() is reported as not implementing it,
because the check runs against the other copy (#4225, #7819). The React FAQ
mentions duplicate builds in passing, and notes that packages "should have
Lexical as peerDependencies, but not all do", without saying why or how to fix
it.

This adds docs/concepts/one-lexical-per-app.md, which states the rule and
backs it with the mechanics that make Lexical a singleton:

  • the active editor and editor state are module-scope variables in
    LexicalUpdates.ts, which every $ function reads;
  • node registration and instanceof LexicalNode compare class references, not
    names;
  • commands are the object identities returned by createCommand();
  • two copies are usually two versions, i.e. API drift inside one editor.

The scope is the app, not the page, and the page says so explicitly: several
self-contained apps, each bundling its own Lexical, can coexist on one page —
isLexicalEditor() is an instanceof check precisely "to prevent issues with
multiple embedded Lexical installations", and editors are tagged on their own
root element rather than in a page-global registry. What such apps cannot do is
interoperate, so the page spells out the boundary rule: pass serialized JSON or
HTML between them, never nodes, editors, EditorStates, selections or commands.
The Detected on the page: ... incompatible editors with versions ...
diagnostic is page-wide and will count a legitimately separate app too, which
the page now notes so it is not read as proof of a bug.

It then gives the packaging rule — a library declares lexical and the
@lexical/* packages it imports in peerDependencies (plus devDependencies),
never in dependencies; the application brings its own Lexical and keeps every
Lexical package at one version — along with the caret-range trap for a pre-1.0
package, how to confirm a duplicate (npm ls lexical from the app root), and
how to deduplicate with package-manager overrides or bundler dedupe.

Supporting changes, all pointing at that one page so the rule has a single home:

  • getting-started/creating-plugin.md: a "Publishing your plugin" section.
  • faq.md: an entry keyed to the two error strings people actually search for.
  • react/faq.md: a link from the existing multiple-builds paragraph.
  • sidebars.js: the new page under Concepts.
  • AGENTS.md: the same rule for agents, including the app-not-page boundary,
    why the monorepo's own workspace:* dependencies are correct as they are, and
    that examples/ and the playground are applications.

Docs only — no source, API, or serialization changes.

Test plan

Before

The rule was not documented anywhere in the docs tree:

$ git grep -n -i "one copy of lexical\|duplicate.*install" packages/lexical-website/docs
$ echo $?
1

The only mention of npm peer dependencies was the passing remark in
docs/react/faq.md, and docs/extensions/peer-dependencies.md is about a
different, extension-level concept with the same name.

After

pnpm -C packages/lexical-website run build — the new page builds and every
cross-link resolves (onBrokenMarkdownLinks: 'throw'):

[info] markdown generated at ./docs/api
[SUCCESS] Generated static files in "build".

$ ls packages/lexical-website/build/docs/concepts/ | grep one-lexical
one-lexical-per-app

The two HTML minifier diagnostic warnings the build prints for /docs/api/
are pre-existing on main and unrelated to this change.

$ npx prettier --list-different AGENTS.md packages/lexical-website/docs packages/lexical-website/sidebars.js
$ echo $?
0

No unit or e2e tests were run: this change touches only Markdown and the
Docusaurus sidebar.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 7, 2026
@vercel

vercel Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 8, 2026 8:18am UTC
lexical-playground Ready Ready Preview Sep 8, 2026 8:18am UTC

Request Review

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to have multiple self-contained lexical instances on the same page, just not in the same “app” because they just don’t have compatible APIs

…ngleton per app and libraries must declare it as a peer dependency

## Description

Nothing in the docs states the rule that an app must resolve exactly one copy of
`lexical`, or what a library built on Lexical should do about it. The failure
mode is common and the errors it produces point nowhere near the cause — a node
class that plainly implements `getType()` is reported as not implementing it,
because the check runs against the other copy (#4225, #7819). The React FAQ
mentions duplicate builds in passing, and notes that packages "should have
Lexical as `peerDependencies`, but not all do", without saying why or how to fix
it.

This adds `docs/concepts/one-lexical-per-app.md`, which states the rule and
backs it with the mechanics that make Lexical a singleton:

- the active editor and editor state are module-scope variables in
  `LexicalUpdates.ts`, which every `$` function reads;
- node registration and `instanceof LexicalNode` compare class references, not
  names;
- commands are the object identities returned by `createCommand()`;
- two copies are usually two versions, i.e. API drift inside one editor.

The scope is the app, not the page, and the page says so explicitly: several
self-contained apps, each bundling its own Lexical, can coexist on one page —
`isLexicalEditor()` is an `instanceof` check precisely "to prevent issues with
multiple embedded Lexical installations", and editors are tagged on their own
root element rather than in a page-global registry. What such apps cannot do is
interoperate, so the page spells out the boundary rule: pass serialized JSON or
HTML between them, never nodes, editors, `EditorState`s, selections or commands.
The `Detected on the page: ... incompatible editors with versions ...`
diagnostic is page-wide and will count a legitimately separate app too, which
the page now notes so it is not read as proof of a bug.

It then gives the packaging rule — a library declares `lexical` and the
`@lexical/*` packages it imports in `peerDependencies` (plus `devDependencies`),
never in `dependencies`; the application brings its own Lexical and keeps every
Lexical package at one version — along with the caret-range trap for a pre-1.0
package, how to confirm a duplicate (`npm ls lexical` from the app root), and
how to deduplicate with package-manager overrides or bundler dedupe.

Supporting changes, all pointing at that one page so the rule has a single home:

- `getting-started/creating-plugin.md`: a "Publishing your plugin" section.
- `faq.md`: an entry keyed to the two error strings people actually search for.
- `react/faq.md`: a link from the existing multiple-builds paragraph.
- `sidebars.js`: the new page under Concepts.
- `AGENTS.md`: the same rule for agents, including the app-not-page boundary,
  why the monorepo's own `workspace:*` dependencies are correct as they are, and
  that `examples/` and the playground are applications.

Docs only — no source, API, or serialization changes.

## Test plan

### Before

The rule was not documented anywhere in the docs tree:

```
$ git grep -n -i "one copy of lexical\|duplicate.*install" packages/lexical-website/docs
$ echo $?
1
```

The only mention of npm peer dependencies was the passing remark in
`docs/react/faq.md`, and `docs/extensions/peer-dependencies.md` is about a
different, extension-level concept with the same name.

### After

`pnpm -C packages/lexical-website run build` — the new page builds and every
cross-link resolves (`onBrokenMarkdownLinks: 'throw'`):

```
[info] markdown generated at ./docs/api
[SUCCESS] Generated static files in "build".

$ ls packages/lexical-website/build/docs/concepts/ | grep one-lexical
one-lexical-per-app
```

The two `HTML minifier diagnostic` warnings the build prints for `/docs/api/`
are pre-existing on `main` and unrelated to this change.

```
$ npx prettier --list-different AGENTS.md packages/lexical-website/docs packages/lexical-website/sidebars.js
$ echo $?
0
```

No unit or e2e tests were run: this change touches only Markdown and the
Docusaurus sidebar.
@potatowagon
potatowagon force-pushed the potatowagon/docs-one-lexical-instance-peer-deps branch from 17e663c to ace7951 Compare September 8, 2026 08:17
@potatowagon potatowagon changed the title [lexical-website] Documentation Update: document that Lexical is a singleton and libraries must declare it as a peer dependency [lexical-website] Documentation Update: document that Lexical is a singleton per app and libraries must declare it as a peer dependency Sep 8, 2026
@potatowagon

potatowagon commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Good catch — you're right, and the page was ambiguous about exactly that. Fixed in ace7951.

Two things were wrong:

  • The title. "One Lexical instance" reads as one-per-page, and "instance" is the same word for a whole self-contained app. Renamed to One Lexical per app (one-lexical-per-app.md).
  • The diagnostic paragraph. It quoted Detected on the page: … incompatible editors with versions … as proof of a duplicate. That scan is page-wide, so it counts a legitimately separate app too — as written, it would have told a valid multi-app page it was broken.

There's now a section up front, before any of the failure-mode material, saying the boundary is the app and not the page: several self-contained apps each bundling their own Lexical can share a page (CMS widgets, micro-frontends), and that is not a bug. It cites isLexicalEditor()'s own comment — "Check instanceof to prevent issues with multiple embedded Lexical installations" — and the fact that editors are tagged on their own root element rather than in a page-global registry.

Your "just don't have compatible APIs" is spelled out as the boundary rule, since that seemed like the part worth being explicit about: pass serialized JSON or HTML between such apps, never nodes, editors, EditorStates, selections or commands; a plugin or shared editor config bundles with the app it serves rather than being a live import shared between apps; and @lexical/react context doesn't cross either, since the context object itself belongs to one copy.

Everything after that section is scoped to the real failure case — two copies inside one app — and AGENTS.md carries the same app-not-page nuance in short form.

Re-approve when you get a chance, since the push dismissed nothing but the content did change.

Note I amended rather than stacked, so the branch is still one commit. If it's easier to review just the delta since your approval: 17e663c...ace7951

@potatowagon
potatowagon added this pull request to the merge queue Sep 8, 2026
Merged via the queue into main with commit a835064 Sep 8, 2026
46 checks passed
@etrepum etrepum mentioned this pull request Sep 17, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — ace7951c Deployed Sep 8, 2026 by vercel[bot]
Preview – lexical-playground — ace7951c Deployed Sep 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants