Repository navigation
[lexical-website] Documentation Update: document that Lexical is a singleton per app and libraries must declare it as a peer dependency - #9125
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
left a comment
There was a problem hiding this comment.
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.
17e663c to
ace7951
Compare
|
Good catch — you're right, and the page was ambiguous about exactly that. Fixed in ace7951. Two things were wrong:
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 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, Everything after that section is scoped to the real failure case — two copies inside one app — and 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 |
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 failuremode 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 fixit.
This adds
docs/concepts/one-lexical-per-app.md, which states the rule andbacks it with the mechanics that make Lexical a singleton:
LexicalUpdates.ts, which every$function reads;instanceof LexicalNodecompare class references, notnames;
createCommand();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 aninstanceofcheck precisely "to prevent issues withmultiple 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
lexicaland the@lexical/*packages it imports inpeerDependencies(plusdevDependencies),never in
dependencies; the application brings its own Lexical and keeps everyLexical package at one version — along with the caret-range trap for a pre-1.0
package, how to confirm a duplicate (
npm ls lexicalfrom the app root), andhow 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, andthat
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:
The only mention of npm peer dependencies was the passing remark in
docs/react/faq.md, anddocs/extensions/peer-dependencies.mdis about adifferent, extension-level concept with the same name.
After
pnpm -C packages/lexical-website run build— the new page builds and everycross-link resolves (
onBrokenMarkdownLinks: 'throw'):The two
HTML minifier diagnosticwarnings the build prints for/docs/api/are pre-existing on
mainand unrelated to this change.No unit or e2e tests were run: this change touches only Markdown and the
Docusaurus sidebar.