Skip to content

[lexical] Feature: build the node with $create when importing JSON - #9090

Merged
etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/extract-create-updates-pr-7tsm0f
Aug 23, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/extract-create-updates-pr-7tsm0f

Conversation

@etrepum

@etrepum etrepum commented Aug 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Follow-up to #9061 to replace $applyNodeReplacement(new klass()) with $create(klass) in importJSON by making $create compatible with node replacements that are missing withKlass.

In situations where node replacement with withKlass this halves the cost of importing a node. The majority of the cost is in the constructor, and $applyNodeReplacement constructs two nodes when any replacement is configured. $create only constructs a single node when withKlass is specified for the entire chain.

Test plan

New unit tests to confirm that $create directly handles replacement for configurations using with without withKlass.

## Description

The synthesized `importJSON` builds its node with `$applyNodeReplacement(new
klass())`, which constructs the replaced node, discovers a replacement,
constructs the replacement too, and drops the first — one wasted construction
per node, and constructing a node is most of what importing one costs. The
`TODO` left in that spot by meta#9061 asks for the switch to `$create`, which
resolves `withKlass` first and constructs once, which is what it was written
for.

What kept the TODO there is the one replacement `$create` could not express: a
`replace` given without a `withKlass` cannot be resolved ahead of construction,
because only its `with` function knows what to build, and `$create` documented
itself as ignoring those. So `$create` no longer ignores them. The resolve
already stops at a registered node whose `replace` is still set — that is
exactly the deprecated case, with no `withKlass` left to follow — so honoring
it is one comparison on a value already in hand, and needs nothing recorded on
the editor. With that, `importJSON` can call `$create` and the TODO goes away.

Which also makes `$create` agree with the `$create*` factories for a
configuration where it previously did not: with a deprecated `replace`
registered, `$create(ParagraphNode)` and `$createParagraphNode()` both give you
the replacement now.

## Test plan

### Before

The new `$create` case fails, since a `with` without a `withKlass` was ignored:

```
$ npx vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx -t 'create resolves withKlass'

 FAIL  |unit| packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx > LexicalEditor tests > node replacement > $create resolves withKlass directly, and still honors a `with` without one
AssertionError: expected ParagraphNode{ …(17), …(1) } to be an instance of LegacyParagraph
 ❯ legacy.update.discrete packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx:4045:42
    4043|       legacy.update(
    4044|         () => {
    4045|           expect($create(ParagraphNode)).toBeInstanceOf(LegacyParagrap…
       |                                          ^

 Test Files  1 failed (1)
      Tests  1 failed | 92 skipped (93)
```

And switching `importJSON` to `$create` on its own — without teaching `$create`
about the deprecated form — breaks the case meta#9061 added:

```
$ npx vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx -t 'applies the replacement on import'

- Expected
+ Received

- true
+ false

 ❯ packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx:4088:58
    4086|       newEditor.read(() => {
    4087|         const paragraph = $getRoot().getFirstChild();
    4088|         expect(paragraph instanceof CustomParagraphNode).toBe(true);
       |                                                          ^

 Test Files  1 failed (1)
      Tests  1 failed | 92 skipped (93)
```

Parsing a 12,001 node document whose text nodes carry the base `text` type
against a registered `TextNode` replacement, production build, min of 25 after
5 warmup iterations, three runs:

```
  no replacement                    72.11 / 71.56 / 70.60 ms
  replaced, withKlass               93.72 / 93.31 / 95.06 ms
  replaced, legacy (no withKlass)   91.88 / 95.21 / 96.00 ms
```

### After

Both cases pass, along with the rest of the suite:

```
$ npx vitest run --project unit

 Test Files  256 passed (256)
      Tests  4241 passed | 1 skipped (4242)

$ pnpm run test-browser

 Test Files  18 passed (18)
      Tests  131 passed | 1 skipped (132)

$ pnpm run ci-check
No errors!
$ echo $?
0
```

Same benchmark, same machine, three runs:

```
  no replacement                    73.84 / 71.92 / 72.38 ms
  replaced, withKlass               75.57 / 73.41 / 72.73 ms
  replaced, legacy (no withKlass)   96.21 / 94.81 / 95.90 ms
```

About 22% off a parse when a `withKlass` replacement is registered, and the
no-replacement and deprecated-replacement cases are unchanged within run-to-run
noise — the deprecated one still pays for the discarded construction, because
it is the case that cannot avoid it.

One test is new, covering `$create` in both configurations. The E2E matrix
(Chromium/Firefox/WebKit) was not exercised, since it needs the playground dev
server.

Co-Authored-By: Claude Opus 5 <[email protected]>
@vercel

vercel Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 23, 2026 12:43am
lexical-playground Ready Ready Preview Aug 23, 2026 12:43am

Request Review

@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 Aug 23, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Aug 23, 2026
@etrepum
etrepum marked this pull request as ready for review August 23, 2026 00:47
@etrepum
etrepum added this pull request to the merge queue Aug 23, 2026
Merged via the queue into meta:main with commit 97e7d22 Aug 23, 2026
48 checks passed
unix-max pushed a commit to unix-max/lexical that referenced this pull request Aug 28, 2026
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 86c25e3a Deployed Aug 23, 2026 by vercel[bot]
Preview – lexical-playground — 86c25e3a Deployed Aug 23, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants