Skip to content

[lexical][lexical-compiler] Bug Fix: Let esbuild drop the editor from a createCommand-only production import - #9121

Merged
etrepum merged 2 commits into
mainfrom
zurfyx/esbuild-tree-shaking-createcommand
Sep 7, 2026
Merged

etrepum merged 2 commits into
mainfrom
zurfyx/esbuild-tree-shaking-createcommand

Conversation

@zurfyx

@zurfyx zurfyx commented Sep 7, 2026

Copy link
Copy Markdown
Member

Description

A consumer that imports only createCommand from the production ESM lexical package retains most of the core when bundled with esbuild. Rollup already drops the unused editor graph in the same scenario; esbuild does not because four module-initialization points in Lexical.prod.mjs are not provably side-effect free:

  1. The root event table was built (and mutated with beforeinput) at module evaluation time. It is now built lazily on the first addRootElementEvents call.
  2. createRefCountedRegistry is now in the compiler's pure-factory list (its definition already carried @__NO_SIDE_EFFECTS__), so the module-scope documentSelectionChange registry gets a /* @__PURE__ */ annotation. The factory allocates a Map and closures and never invokes the activation callback.
  3. LexicalEditor.version is initialized as a static class field instead of a post-class assignment. Same value, same (writable/enumerable/configurable) property descriptor.
  4. The [CACHED_TEXT_SIZE_KEY] field on LexicalNode is now declare-only, so the class body no longer references the reconciler's symbol at definition time. The constructor still defines the same non-enumerable own property.

A new integration test bundles the built production artifact with esbuild and enforces a 16 KiB ceiling for a createCommand-only consumer. It resolves lexical to the built production export explicitly, since the workspace tsconfig and vitest aliases would otherwise resolve to TypeScript source (which already tree-shakes well and would give a false pass).

Two independent improvements found while measuring are included and can be reviewed (or split out) separately:

  • GenMap.entries() takes a fast path with no per-entry override checks when there is no nursery. Local paired microbenchmarks showed roughly 9–20% higher iteration throughput on unmodified snapshots (100–100,000 entries); edited maps were near parity. This is a local microbenchmark only and does not establish a typing or paste latency improvement.
  • The paste benchmark now resets its editor state through tinybench task hooks outside the timed body and asserts the resulting child count on every sample. Vitest 4.1.x constructs the tinybench Task without forwarding beforeEach/afterEach from bench() options, so the previous hooks never ran and the benchmark accumulated paragraphs across iterations. The shared DOM benchmark helper also detaches the previous editor host between setups.

This lands the patch proposed in #9120 as a PR. The change is limited to module initialization order and annotations; no public API changes.

Closes #9120

Test plan

Sizes are minified esbuild 0.27.7 output (--bundle --minify --format=esm --platform=browser --conditions=production), gzip via gzip -n -9. Full repro steps are in #9120.

Minified output Before (main) After
esbuild createCommand consumer 148,429 B (48,493 B gzip) 1,538 B (867 B gzip)
esbuild createEditor consumer 162,908 B (53,380 B gzip) 163,009 B (53,414 B gzip)
Shipped Lexical.prod.mjs 181,023 B (56,942 B gzip) 181,145 B (57,017 B gzip)

The shipped production ESM grows by 122 bytes; the narrow-import case shrinks by ~99%.

Before

  • scripts/__tests__/integration/tree-shaking.test.mjs fails against the main production artifact (expected 148441 to be less than 16384).

After

  • pnpm run tsc, pnpm run tsc-scripts, pnpm run flow, ESLint and Prettier on the changed files: pass.
  • Unit tests for lexical, @lexical/compiler, and the internal ESLint plugin: 1,555 passed (74 files).
  • pnpm run test-integration: 189 passed, including the new tree-shaking test. The 5 failures are the example-fixture "tests pass" steps, which need Playwright browser system dependencies my container does not have (Host system is missing dependencies to run browsers); they are unrelated to this change.
  • vitest bench --project bench-dom on the paste benchmark: runs with the task hooks and the per-sample size + 9 child-count assertion for both sizes.
  • Built Lexical.prod.mjs inspected: lazy root event table, static version=… inside the class, and no CachedTextSize symbol reference in the class body.

@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 7, 2026 4:20am UTC
lexical-playground Ready Ready Preview Sep 7, 2026 4:20am UTC

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 Sep 7, 2026
Comment thread packages/lexical/src/LexicalGenMap.ts Resolved
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 7, 2026
@etrepum
etrepum added this pull request to the merge queue Sep 7, 2026
Merged via the queue into main with commit 111e732 Sep 7, 2026
49 of 50 checks passed
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 7, 2026
…ode-shiki][lexical-devtools-core][lexical-extension][lexical-hashtag][lexical-history][lexical-html][lexical-link][lexical-markdown][lexical-mdast][lexical-react][lexical-tailwind][lexical-yjs] Bug Fix: Remove module-scope side effects that defeat tree-shaking

## Description

A consumer that imports one thing from a Lexical package should pay for that
thing alone. That only holds when nothing at module scope has to be kept for
its side effects: a bundler (esbuild and webpack in particular, which never
look inside a function they were not told is pure) keeps every module-scope
statement it cannot prove side-effect free, together with everything that
statement references. `lexical` had four such statements — a `try` around
the `process.env.LEXICAL_VERSION` read, `LexicalEditor.version = ...` after
the class, a computed `[CACHED_TEXT_SIZE_KEY]` class field, and a
`rootElementEvents.push(...)` behind `if (CAN_USE_BEFORE_INPUT)` — and
between them they kept nearly all of `lexical` (148 KB minified) in a bundle
that only imported `createCommand`. meta#9121 fixed those four and brought that
consumer down to about 1.5 KB.

This change builds on it and finds the whole class of problem
systematically: bundle a bare `import 'pkg'` of every published ESM entry with
esbuild (the package's `sideEffects: false` overridden so the module is
included, everything else external) and look at what is retained. Anything
retained is a module-scope side effect. Doing that across every package
turned up, beyond the four above: browser probes reading `navigator` and
`window` (`environment.ts`), the emoji feature-detection IIFE, `new RegExp`
and `Object.freeze` calls, `Date.now` passed as a value, `.name` read off an
imported extension, array and object spreads (`TRANSFORMERS`, key display
maps), `.join`/`.map`/`.split` calls building constants, `in` probes, `|`
over imported format bits, computed-key objects (`ELEMENT_FORMAT_TO_TYPE`,
`CONTROL_OR_META`, the `sel` selector wrappers), `selBase.any`-style property
reads, unannotated calls to local factories (`createBlockNode`, `listReplace`,
`createLinkMatcherWithRegExp`, `join`) and to React's `forwardRef` and
`createContext`, `warnOnlyOnce(...)` default-import calls, an eagerly created
shiki highlighter, terser rewriting `Infinity` to the side-effectful `1/0`,
and `@preact/signals-core` inlined into `@lexical/extension` so that its
prototype patching became our module's side effect.

The fixes follow one rule, now documented in AGENTS.md and the compiler
README: move the work into a function declared `@__NO_SIDE_EFFECTS__` and
call it at module scope so that `@lexical/compiler` annotates the call (the
compiler now also recognizes a default import from a Lexical module named for
the factory, so `warnOnlyOnce` calls are annotated too), or annotate a call to
a builtin or third-party function by hand. `createRefCountedRegistry`,
`createLinkMatcherWithRegExp` and `warnOnlyOnce` join the trusted factory
list. `@preact/signals-core` stays external in the npm build (it declares
`sideEffects: false` itself), terser keeps `Infinity`, and the shiki
highlighter is created on first use.

Two tests keep it that way. `scripts/__tests__/unit/treeShakingSource.test.ts`
runs the bare-import check over every package's TypeScript source entry
through the compiler plugin (no build needed, so it runs with `test-unit`),
and `scripts/__tests__/integration/tree-shaking.test.mjs` runs it over every
published dev and prod ESM build and reproduces the issue's esbuild consumer
with a size ceiling. Both require that nothing is retained, with
`@lexical/code-prism` (which registers grammars on the global Prism when
imported) as the one listed exception.

No public API changes.

Closes meta#9120

## Test plan

Reproduction from the issue (esbuild 0.27.7, minified, `--conditions=production`):

```sh
printf 'import {createCommand} from "lexical"; console.log(createCommand);\n' > consumer/main.mjs
esbuild consumer/main.mjs --bundle --minify --format=esm --platform=browser \
  --conditions=production --define:process.env.NODE_ENV='"production"' --outfile=consumer/out.js
wc -c consumer/out.js
```

### Before

```
148429 consumer/out.js
```

(1538 bytes on main after meta#9121.)

Bare-import residue per published production entry (esbuild, all imports external):

```
=== lexical [prod]: bare import retains 154645 B minified (6093 lines)
=== @lexical/extension [prod]: bare import retains 6649 B minified (266 lines)
=== @lexical/hashtag [prod]: bare import retains 6245 B minified (9 lines)
=== @lexical/markdown [prod]: bare import retains 9044 B minified (254 lines)
=== @lexical/history [prod]: bare import retains 4632 B minified (122 lines)
=== @lexical/html [prod]: bare import retains 4447 B minified (175 lines)
=== @lexical/tailwind [prod]: bare import retains 3754 B minified (6 lines)
=== @lexical/react/ReactExtension [prod]: bare import retains 2288 B minified (50 lines)
=== @lexical/code-prism [prod]: bare import retains 2339 B minified (28 lines)
=== @lexical/mdast [prod]: bare import retains 1177 B minified (28 lines)
=== @lexical/react/LexicalCollaborationContextUtils [prod]: bare import retains 627 B minified (8 lines)
=== @lexical/link [prod]: bare import retains 450 B minified (12 lines)
=== @lexical/devtools-core [prod]: bare import retains 382 B minified (10 lines)
=== @lexical/compiler [prod]: bare import retains 322 B minified (6 lines)
=== @lexical/code-shiki [prod]: bare import retains 284 B minified (8 lines)
=== @lexical/a11y [prod]: bare import retains 255 B minified (3 lines)
...
TOTAL 206463
```

### After

```
45 consumer/out.js
```

(`createEditor` consumer: 162908 → 162850 bytes, so an editor still costs an editor.)

Bare-import residue per published entry, production and development:

```
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
TOTAL 1610
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
TOTAL 1616
```

`pnpm exec vitest run scripts/__tests__/integration/tree-shaking.test.mjs` (after `pnpm run build --prod --release`):

```
 Test Files  1 passed (1)
      Tests  206 passed (206)
```

`pnpm run test-unit` (includes the new `scripts/__tests__/unit/treeShakingSource.test.ts`, 102 entries):

```
 Test Files  324 passed (324)
      Tests  6133 passed | 1 skipped (6134)
```

`pnpm run tsc`, `pnpm run flow`, `pnpm run prettier` and `pnpm run lint` pass.
The `test-integration` project (which runs `prepare-release` first) and the
browser/e2e suites were not run here.
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 7, 2026
…ode-shiki][lexical-devtools-core][lexical-extension][lexical-hashtag][lexical-history][lexical-html][lexical-link][lexical-markdown][lexical-mdast][lexical-react][lexical-tailwind][lexical-yjs] Bug Fix: Remove the remaining module-scope side effects that defeat tree-shaking

## Description

meta#9121 fixed the four module-scope statements in `lexical` that kept nearly
the whole package in a bundle importing only `createCommand`, and added a
16 KB ceiling for that one consumer. This change finds the rest of that
class of problem, across every package, and adds a guard that requires
nothing of the kind to remain.

The method: bundle a bare `import 'pkg'` of every published ESM entry with
esbuild, with the package's `sideEffects: false` overridden so the module is
included and everything else external, and look at what is retained. esbuild
(like webpack) never looks inside a function it was not told is pure, so
whatever survives is a module-scope statement it has to keep as a side
effect, together with everything that statement references — and a consumer
who imports one thing from the module pays for all of it. On main that still
leaves about 1.5 KB in `lexical` and code in twenty other production
entries (40 KB in total), the largest being `@lexical/markdown` (8.4 KB),
`@lexical/hashtag` (6.2 KB), `@lexical/extension` (6.1 KB), `@lexical/html`
(4.4 KB), `@lexical/history` (4.2 KB) and `@lexical/tailwind` (3.8 KB); the
development build of `lexical` keeps essentially all of itself (217 KB). Any
narrow import from those, or from a package that imports them, carries it.

What was retaining it, by kind: property reads at module scope (`navigator`
and `window` probes in `environment.ts`, `Date.now` passed as a config value,
`ReactProviderExtension.name`, `selBase.any`, `TEXT_TYPE_TO_FORMAT.bold`,
`TabNode.length`); calls the build does not annotate (`new RegExp`,
`Object.freeze`, `.join`/`.map`/`.split` building constants, the emoji
feature-detection IIFE, React's `forwardRef` and `createContext`, local
factories such as `createBlockNode`, `listReplace`,
`createLinkMatcherWithRegExp` and tailwind's `join`, and `warnOnlyOnce(...)`,
whose default import the compiler did not recognize); array and object
spreads (`TRANSFORMERS`, `DEFAULT_TRANSFORMERS`, the key display maps,
`ALT_SHIFT_KEY_ANY`); computed-key object literals (`ELEMENT_FORMAT_TO_TYPE`,
`TEXT_TYPE_TO_MODE`, `CONTROL_OR_META`, the `sel` selector wrappers); `in`
and `|` expressions over other bindings (`SUPPORTS_CSS_HIGHLIGHTS`,
`TEXT_FORMAT_MASK`, `IS_ALL_FORMATTING`); a shiki highlighter and regex
engine created at import time; terser rewriting `Infinity` to `1/0`, which
esbuild keeps as a division; and `@preact/signals-core` inlined into
`@lexical/extension`, so that its prototype patching became our module's
side effect in every bundle that imports anything from it.

The fixes follow one rule, now documented in AGENTS.md and the compiler
README: move the work into a function declared `@__NO_SIDE_EFFECTS__` and
call it at module scope so that `@lexical/compiler` annotates the call, or
annotate a call to a builtin or third-party function by hand. The compiler
now also recognizes a default import from a Lexical module named for the
factory, so `warnOnlyOnce` calls are annotated, and
`createLinkMatcherWithRegExp` and `warnOnlyOnce` join the trusted factory
list. `@preact/signals-core` is left external in the npm build (it declares
`sideEffects: false` itself, so a consumer's bundler drops it when unused),
terser keeps `Infinity`, and the shiki highlighter is created on first use.

Two tests replace the single size ceiling with an invariant.
`scripts/__tests__/unit/treeShakingSource.test.ts` runs the bare-import
check over every package's TypeScript source entry through the compiler
plugin, which needs no build and so runs on every change with `test-unit`.
`scripts/__tests__/integration/tree-shaking.test.mjs` runs it over every
published dev and prod ESM build, and keeps meta#9121's consumer reproduction
with the ceiling lowered to 2 KB. Both require that nothing is retained,
with `@lexical/code-prism` (which registers grammars on the global Prism
when imported) as the one listed exception. A failure prints the retained
code; its first statement is usually the culprit.

No public API changes.

Closes meta#9120

## Test plan

Bare-import residue per published entry (esbuild, all imports external),
and the issue's consumer (esbuild 0.27.7, minified, `--conditions=production`):

```sh
printf 'import {createCommand} from "lexical"; console.log(createCommand);\n' > consumer/main.mjs
esbuild consumer/main.mjs --bundle --minify --format=esm --platform=browser \
  --conditions=production --define:process.env.NODE_ENV='"production"' --outfile=consumer/out.js
wc -c consumer/out.js
```

### Before

On main (111e732, after meta#9121):

```
=== lexical [prod]: bare import retains 1465 B minified (44 lines)
=== @lexical/a11y [prod]: bare import retains 212 B minified (1 lines)
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
=== @lexical/code-shiki [prod]: bare import retains 42 B minified (1 lines)
=== @lexical/compiler [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/compiler/PureAnnotations [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/devtools-core [prod]: bare import retains 237 B minified (3 lines)
=== @lexical/extension [prod]: bare import retains 6122 B minified (263 lines)
=== @lexical/hashtag [prod]: bare import retains 6207 B minified (7 lines)
=== @lexical/history [prod]: bare import retains 4152 B minified (120 lines)
=== @lexical/html [prod]: bare import retains 4358 B minified (171 lines)
=== @lexical/internal/version [prod]: bare import retains 38 B minified (5 lines)
=== @lexical/link [prod]: bare import retains 343 B minified (7 lines)
=== @lexical/markdown [prod]: bare import retains 8369 B minified (248 lines)
=== @lexical/mdast [prod]: bare import retains 527 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [prod]: bare import retains 589 B minified (7 lines)
=== @lexical/react/LexicalComposerContext [prod]: bare import retains 15 B minified (1 lines)
=== @lexical/react/LexicalDraggableBlockPlugin [prod]: bare import retains 11 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [prod]: bare import retains 230 B minified (4 lines)
=== @lexical/react/ReactExtension [prod]: bare import retains 1761 B minified (42 lines)
=== @lexical/tailwind [prod]: bare import retains 3754 B minified (6 lines)
=== @lexical/yjs [prod]: bare import retains 65 B minified (1 lines)
TOTAL 40597
=== lexical [dev]: bare import retains 216829 B minified (13360 lines)
=== @lexical/a11y [dev]: bare import retains 229 B minified (1 lines)
=== @lexical/code-core [dev]: bare import retains 167 B minified (12 lines)
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
=== @lexical/code-shiki [dev]: bare import retains 96 B minified (5 lines)
=== @lexical/compiler [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/compiler/PureAnnotations [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/devtools-core [dev]: bare import retains 336 B minified (13 lines)
=== @lexical/extension [dev]: bare import retains 6659 B minified (489 lines)
=== @lexical/hashtag [dev]: bare import retains 6360 B minified (38 lines)
=== @lexical/history [dev]: bare import retains 5666 B minified (366 lines)
=== @lexical/html [dev]: bare import retains 5078 B minified (361 lines)
=== @lexical/internal/version [dev]: bare import retains 69 B minified (5 lines)
=== @lexical/link [dev]: bare import retains 435 B minified (16 lines)
=== @lexical/markdown [dev]: bare import retains 10930 B minified (614 lines)
=== @lexical/mdast [dev]: bare import retains 766 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [dev]: bare import retains 706 B minified (12 lines)
=== @lexical/react/LexicalComposerContext [dev]: bare import retains 48 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [dev]: bare import retains 327 B minified (19 lines)
=== @lexical/react/LexicalNestedComposer [dev]: bare import retains 824 B minified (15 lines)
=== @lexical/react/ReactExtension [dev]: bare import retains 2095 B minified (111 lines)
=== @lexical/selection [dev]: bare import retains 171 B minified (12 lines)
=== @lexical/tailwind [dev]: bare import retains 3157 B minified (122 lines)
=== @lexical/yjs [dev]: bare import retains 86 B minified (1 lines)
TOTAL 263242
```

```
1538 consumer/out.js
```

### After

```
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
TOTAL 1610
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
TOTAL 1616
```

```
45 consumer/out.js
```

(`createEditor` consumer: 162,994 → 162,894 bytes, so an editor still costs an editor.)

`pnpm exec vitest run scripts/__tests__/integration/tree-shaking.test.mjs` (after `pnpm run build --prod --release`):

```
 Test Files  1 passed (1)
      Tests  206 passed (206)
```

`pnpm run test-unit` (includes the new `scripts/__tests__/unit/treeShakingSource.test.ts`, 102 entries):

```
 Test Files  324 passed (324)
      Tests  6133 passed | 1 skipped (6134)
```

`pnpm run tsc`, `pnpm run flow`, `pnpm run prettier` and `pnpm run lint` pass.
The `test-integration` project (which runs `prepare-release` first) and the
browser/e2e suites were not run here.
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 7, 2026
…ode-shiki][lexical-devtools-core][lexical-extension][lexical-hashtag][lexical-history][lexical-html][lexical-link][lexical-markdown][lexical-mdast][lexical-react][lexical-tailwind][lexical-yjs] Bug Fix: Remove the remaining module-scope side effects that defeat tree-shaking

## Description

meta#9121 fixed the four module-scope statements in `lexical` that kept nearly
the whole package in a bundle importing only `createCommand`, and added a
16 KB ceiling for that one consumer. This change finds the rest of that
class of problem, across every package, and adds a guard that requires
nothing of the kind to remain.

The method: bundle a bare `import 'pkg'` of every published ESM entry with
esbuild, with the package's `sideEffects: false` overridden so the module is
included and everything else external, and look at what is retained. esbuild
(like webpack) never looks inside a function it was not told is pure, so
whatever survives is a module-scope statement it has to keep as a side
effect, together with everything that statement references — and a consumer
who imports one thing from the module pays for all of it. On main that still
leaves about 1.5 KB in `lexical` and code in twenty other production
entries (40 KB in total), the largest being `@lexical/markdown` (8.4 KB),
`@lexical/hashtag` (6.2 KB), `@lexical/extension` (6.1 KB), `@lexical/html`
(4.4 KB), `@lexical/history` (4.2 KB) and `@lexical/tailwind` (3.8 KB); the
development build of `lexical` keeps essentially all of itself (217 KB). Any
narrow import from those, or from a package that imports them, carries it.

What was retaining it, by kind: property reads at module scope (`navigator`
and `window` probes in `environment.ts`, `Date.now` passed as a config value,
`ReactProviderExtension.name`, `selBase.any`, `TEXT_TYPE_TO_FORMAT.bold`,
`TabNode.length`); calls the build does not annotate (`new RegExp`,
`Object.freeze`, `.join`/`.map`/`.split` building constants, the emoji
feature-detection IIFE, React's `forwardRef` and `createContext`, local
factories such as `createBlockNode`, `listReplace`,
`createLinkMatcherWithRegExp` and tailwind's `join`, and `warnOnlyOnce(...)`,
whose default import the compiler did not recognize); array and object
spreads (`TRANSFORMERS`, `DEFAULT_TRANSFORMERS`, the key display maps,
`ALT_SHIFT_KEY_ANY`); computed-key object literals (`ELEMENT_FORMAT_TO_TYPE`,
`TEXT_TYPE_TO_MODE`, `CONTROL_OR_META`, the `sel` selector wrappers); `in`
and `|` expressions over other bindings (`SUPPORTS_CSS_HIGHLIGHTS`,
`TEXT_FORMAT_MASK`, `IS_ALL_FORMATTING`); a shiki highlighter and regex
engine created at import time; terser rewriting `Infinity` to `1/0`, which
esbuild keeps as a division; and `@preact/signals-core` inlined into
`@lexical/extension`, so that its prototype patching became our module's
side effect in every bundle that imports anything from it.

The fixes follow one rule, now documented in AGENTS.md and the compiler
README: move the work into a function declared `@__NO_SIDE_EFFECTS__` and
call it at module scope so that `@lexical/compiler` annotates the call, or
annotate a call to a builtin or third-party function by hand. The compiler
now also recognizes a default import from a Lexical module named for the
factory, so `warnOnlyOnce` calls are annotated, and
`createLinkMatcherWithRegExp` and `warnOnlyOnce` join the trusted factory
list. `@preact/signals-core` is left external in the npm build (it declares
`sideEffects: false` itself, so a consumer's bundler drops it when unused),
terser keeps `Infinity`, and the shiki highlighter is created on first use.

Two tests replace the single size ceiling with an invariant.
`scripts/__tests__/unit/treeShakingSource.test.ts` runs the bare-import
check over every package's TypeScript source entry through the compiler
plugin, which needs no build and so runs on every change with `test-unit`.
`scripts/__tests__/integration/tree-shaking.test.mjs` runs it over every
published dev and prod ESM build, and keeps meta#9121's consumer reproduction
with the ceiling lowered to 2 KB. Both require that nothing is retained,
with `@lexical/code-prism` (which registers grammars on the global Prism
when imported) as the one listed exception. A failure prints the retained
code; its first statement is usually the culprit.

No public API changes.

Closes meta#9120

## Test plan

Bare-import residue per published entry (esbuild, all imports external),
and the issue's consumer (esbuild 0.27.7, minified, `--conditions=production`):

```sh
printf 'import {createCommand} from "lexical"; console.log(createCommand);\n' > consumer/main.mjs
esbuild consumer/main.mjs --bundle --minify --format=esm --platform=browser \
  --conditions=production --define:process.env.NODE_ENV='"production"' --outfile=consumer/out.js
wc -c consumer/out.js
```

### Before

On main (111e732, after meta#9121):

```
=== lexical [prod]: bare import retains 1465 B minified (44 lines)
=== @lexical/a11y [prod]: bare import retains 212 B minified (1 lines)
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
=== @lexical/code-shiki [prod]: bare import retains 42 B minified (1 lines)
=== @lexical/compiler [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/compiler/PureAnnotations [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/devtools-core [prod]: bare import retains 237 B minified (3 lines)
=== @lexical/extension [prod]: bare import retains 6122 B minified (263 lines)
=== @lexical/hashtag [prod]: bare import retains 6207 B minified (7 lines)
=== @lexical/history [prod]: bare import retains 4152 B minified (120 lines)
=== @lexical/html [prod]: bare import retains 4358 B minified (171 lines)
=== @lexical/internal/version [prod]: bare import retains 38 B minified (5 lines)
=== @lexical/link [prod]: bare import retains 343 B minified (7 lines)
=== @lexical/markdown [prod]: bare import retains 8369 B minified (248 lines)
=== @lexical/mdast [prod]: bare import retains 527 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [prod]: bare import retains 589 B minified (7 lines)
=== @lexical/react/LexicalComposerContext [prod]: bare import retains 15 B minified (1 lines)
=== @lexical/react/LexicalDraggableBlockPlugin [prod]: bare import retains 11 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [prod]: bare import retains 230 B minified (4 lines)
=== @lexical/react/ReactExtension [prod]: bare import retains 1761 B minified (42 lines)
=== @lexical/tailwind [prod]: bare import retains 3754 B minified (6 lines)
=== @lexical/yjs [prod]: bare import retains 65 B minified (1 lines)
TOTAL 40597
=== lexical [dev]: bare import retains 216829 B minified (13360 lines)
=== @lexical/a11y [dev]: bare import retains 229 B minified (1 lines)
=== @lexical/code-core [dev]: bare import retains 167 B minified (12 lines)
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
=== @lexical/code-shiki [dev]: bare import retains 96 B minified (5 lines)
=== @lexical/compiler [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/compiler/PureAnnotations [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/devtools-core [dev]: bare import retains 336 B minified (13 lines)
=== @lexical/extension [dev]: bare import retains 6659 B minified (489 lines)
=== @lexical/hashtag [dev]: bare import retains 6360 B minified (38 lines)
=== @lexical/history [dev]: bare import retains 5666 B minified (366 lines)
=== @lexical/html [dev]: bare import retains 5078 B minified (361 lines)
=== @lexical/internal/version [dev]: bare import retains 69 B minified (5 lines)
=== @lexical/link [dev]: bare import retains 435 B minified (16 lines)
=== @lexical/markdown [dev]: bare import retains 10930 B minified (614 lines)
=== @lexical/mdast [dev]: bare import retains 766 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [dev]: bare import retains 706 B minified (12 lines)
=== @lexical/react/LexicalComposerContext [dev]: bare import retains 48 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [dev]: bare import retains 327 B minified (19 lines)
=== @lexical/react/LexicalNestedComposer [dev]: bare import retains 824 B minified (15 lines)
=== @lexical/react/ReactExtension [dev]: bare import retains 2095 B minified (111 lines)
=== @lexical/selection [dev]: bare import retains 171 B minified (12 lines)
=== @lexical/tailwind [dev]: bare import retains 3157 B minified (122 lines)
=== @lexical/yjs [dev]: bare import retains 86 B minified (1 lines)
TOTAL 263242
```

```
1538 consumer/out.js
```

### After

```
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
TOTAL 1610
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
TOTAL 1616
```

```
45 consumer/out.js
```

(`createEditor` consumer: 162,994 → 162,894 bytes, so an editor still costs an editor.)

`pnpm exec vitest run scripts/__tests__/integration/tree-shaking.test.mjs` (after `pnpm run build --prod --release`):

```
 Test Files  1 passed (1)
      Tests  206 passed (206)
```

`pnpm run test-unit` (includes the new `scripts/__tests__/unit/treeShakingSource.test.ts`, 102 entries):

```
 Test Files  324 passed (324)
      Tests  6133 passed | 1 skipped (6134)
```

`pnpm run tsc`, `pnpm run tsc-scripts`, `pnpm run flow`, `pnpm run prettier` and
`pnpm run lint` pass.
The `test-integration` project (which runs `prepare-release` first) and the
browser/e2e suites were not run here.
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 7, 2026
…ode-shiki][lexical-devtools-core][lexical-extension][lexical-hashtag][lexical-history][lexical-html][lexical-link][lexical-markdown][lexical-mdast][lexical-react][lexical-tailwind][lexical-yjs] Bug Fix: Remove the remaining module-scope side effects that defeat tree-shaking

## Description

meta#9121 fixed the four module-scope statements in `lexical` that kept nearly
the whole package in a bundle importing only `createCommand`, and added a
16 KB ceiling for that one consumer. This change finds the rest of that
class of problem, across every package, and adds a guard that requires
nothing of the kind to remain.

The method: bundle a bare `import 'pkg'` of every published ESM entry with
esbuild, with the package's `sideEffects: false` overridden so the module is
included and everything else external, and look at what is retained. esbuild
(like webpack) never looks inside a function it was not told is pure, so
whatever survives is a module-scope statement it has to keep as a side
effect, together with everything that statement references — and a consumer
who imports one thing from the module pays for all of it. On main that still
leaves about 1.5 KB in `lexical` and code in twenty other production
entries (40 KB in total), the largest being `@lexical/markdown` (8.4 KB),
`@lexical/hashtag` (6.2 KB), `@lexical/extension` (6.1 KB), `@lexical/html`
(4.4 KB), `@lexical/history` (4.2 KB) and `@lexical/tailwind` (3.8 KB); the
development build of `lexical` keeps essentially all of itself (217 KB). Any
narrow import from those, or from a package that imports them, carries it.

What was retaining it, by kind: property reads at module scope (`navigator`
and `window` probes in `environment.ts`, `Date.now` passed as a config value,
`ReactProviderExtension.name`, `selBase.any`, `TEXT_TYPE_TO_FORMAT.bold`,
`TabNode.length`); calls the build does not annotate (`new RegExp`,
`Object.freeze`, `.join`/`.map`/`.split` building constants, the emoji
feature-detection IIFE, React's `forwardRef` and `createContext`, local
factories such as `createBlockNode`, `listReplace`,
`createLinkMatcherWithRegExp` and tailwind's `join`, and `warnOnlyOnce(...)`,
whose default import the compiler did not recognize); array and object
spreads (`TRANSFORMERS`, `DEFAULT_TRANSFORMERS`, the key display maps,
`ALT_SHIFT_KEY_ANY`); computed-key object literals (`ELEMENT_FORMAT_TO_TYPE`,
`TEXT_TYPE_TO_MODE`, `CONTROL_OR_META`, the `sel` selector wrappers); `in`
and `|` expressions over other bindings (`SUPPORTS_CSS_HIGHLIGHTS`,
`TEXT_FORMAT_MASK`, `IS_ALL_FORMATTING`); a shiki highlighter and regex
engine created at import time; terser rewriting `Infinity` to `1/0`, which
esbuild keeps as a division; and `@preact/signals-core` inlined into
`@lexical/extension`, so that its prototype patching became our module's
side effect in every bundle that imports anything from it.

The fixes follow one rule, now documented in AGENTS.md and the compiler
README: move the work into a function declared `@__NO_SIDE_EFFECTS__` and
call it at module scope so that `@lexical/compiler` annotates the call, or
annotate a call to a builtin or third-party function by hand. The compiler
now also recognizes a default import from a Lexical module named for the
factory, so `warnOnlyOnce` calls are annotated, and
`createLinkMatcherWithRegExp` and `warnOnlyOnce` join the trusted factory
list. `@preact/signals-core` is left external in the npm build (it declares
`sideEffects: false` itself, so a consumer's bundler drops it when unused),
terser keeps `Infinity`, and the shiki highlighter is created on first use.

Two tests replace the single size ceiling with an invariant.
`scripts/__tests__/unit/treeShakingSource.test.ts` runs the bare-import
check over every package's TypeScript source entry through the compiler
plugin, which needs no build and so runs on every change with `test-unit`.
`scripts/__tests__/integration/tree-shaking.test.mjs` runs it over every
published dev and prod ESM build, and keeps meta#9121's consumer reproduction
with the ceiling lowered to 2 KB. Both require that nothing is retained,
with `@lexical/code-prism` (which registers grammars on the global Prism
when imported) as the one listed exception. A failure prints the retained
code; its first statement is usually the culprit.

No public API changes.

Closes meta#9120

## Test plan

Bare-import residue per published entry (esbuild, all imports external),
and the issue's consumer (esbuild 0.27.7, minified, `--conditions=production`):

```sh
printf 'import {createCommand} from "lexical"; console.log(createCommand);\n' > consumer/main.mjs
esbuild consumer/main.mjs --bundle --minify --format=esm --platform=browser \
  --conditions=production --define:process.env.NODE_ENV='"production"' --outfile=consumer/out.js
wc -c consumer/out.js
```

### Before

On main (111e732, after meta#9121):

```
=== lexical [prod]: bare import retains 1465 B minified (44 lines)
=== @lexical/a11y [prod]: bare import retains 212 B minified (1 lines)
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
=== @lexical/code-shiki [prod]: bare import retains 42 B minified (1 lines)
=== @lexical/compiler [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/compiler/PureAnnotations [prod]: bare import retains 245 B minified (2 lines)
=== @lexical/devtools-core [prod]: bare import retains 237 B minified (3 lines)
=== @lexical/extension [prod]: bare import retains 6122 B minified (263 lines)
=== @lexical/hashtag [prod]: bare import retains 6207 B minified (7 lines)
=== @lexical/history [prod]: bare import retains 4152 B minified (120 lines)
=== @lexical/html [prod]: bare import retains 4358 B minified (171 lines)
=== @lexical/internal/version [prod]: bare import retains 38 B minified (5 lines)
=== @lexical/link [prod]: bare import retains 343 B minified (7 lines)
=== @lexical/markdown [prod]: bare import retains 8369 B minified (248 lines)
=== @lexical/mdast [prod]: bare import retains 527 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [prod]: bare import retains 589 B minified (7 lines)
=== @lexical/react/LexicalComposerContext [prod]: bare import retains 15 B minified (1 lines)
=== @lexical/react/LexicalDraggableBlockPlugin [prod]: bare import retains 11 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [prod]: bare import retains 230 B minified (4 lines)
=== @lexical/react/ReactExtension [prod]: bare import retains 1761 B minified (42 lines)
=== @lexical/tailwind [prod]: bare import retains 3754 B minified (6 lines)
=== @lexical/yjs [prod]: bare import retains 65 B minified (1 lines)
TOTAL 40597
=== lexical [dev]: bare import retains 216829 B minified (13360 lines)
=== @lexical/a11y [dev]: bare import retains 229 B minified (1 lines)
=== @lexical/code-core [dev]: bare import retains 167 B minified (12 lines)
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
=== @lexical/code-shiki [dev]: bare import retains 96 B minified (5 lines)
=== @lexical/compiler [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/compiler/PureAnnotations [dev]: bare import retains 296 B minified (10 lines)
=== @lexical/devtools-core [dev]: bare import retains 336 B minified (13 lines)
=== @lexical/extension [dev]: bare import retains 6659 B minified (489 lines)
=== @lexical/hashtag [dev]: bare import retains 6360 B minified (38 lines)
=== @lexical/history [dev]: bare import retains 5666 B minified (366 lines)
=== @lexical/html [dev]: bare import retains 5078 B minified (361 lines)
=== @lexical/internal/version [dev]: bare import retains 69 B minified (5 lines)
=== @lexical/link [dev]: bare import retains 435 B minified (16 lines)
=== @lexical/markdown [dev]: bare import retains 10930 B minified (614 lines)
=== @lexical/mdast [dev]: bare import retains 766 B minified (8 lines)
=== @lexical/react/LexicalCollaborationContextUtils [dev]: bare import retains 706 B minified (12 lines)
=== @lexical/react/LexicalComposerContext [dev]: bare import retains 48 B minified (1 lines)
=== @lexical/react/LexicalMarkdownShortcutPlugin [dev]: bare import retains 327 B minified (19 lines)
=== @lexical/react/LexicalNestedComposer [dev]: bare import retains 824 B minified (15 lines)
=== @lexical/react/ReactExtension [dev]: bare import retains 2095 B minified (111 lines)
=== @lexical/selection [dev]: bare import retains 171 B minified (12 lines)
=== @lexical/tailwind [dev]: bare import retains 3157 B minified (122 lines)
=== @lexical/yjs [dev]: bare import retains 86 B minified (1 lines)
TOTAL 263242
```

```
1538 consumer/out.js
```

### After

```
=== @lexical/code-prism [prod]: bare import retains 1610 B minified (9 lines)
TOTAL 1610
=== @lexical/code-prism [dev]: bare import retains 1616 B minified (78 lines)
TOTAL 1616
```

```
45 consumer/out.js
```

(`createEditor` consumer: 162,994 → 162,894 bytes, so an editor still costs an editor.)

`pnpm exec vitest run scripts/__tests__/integration/tree-shaking.test.mjs` (after `pnpm run build --prod --release`):

```
 Test Files  1 passed (1)
      Tests  206 passed (206)
```

`pnpm run test-unit` (includes the new `scripts/__tests__/unit/treeShakingSource.test.ts`, 102 entries):

```
 Test Files  324 passed (324)
      Tests  6133 passed | 1 skipped (6134)
```

`pnpm run tsc`, `pnpm run tsc-scripts`, `pnpm run flow`, `pnpm run prettier` and
`pnpm run lint` pass.
The `test-integration` project (which runs `prepare-release` first) and the
browser/e2e suites were not run here.
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 7, 2026
Brings in the two tree-shaking commits (meta#9121, meta#9124), which add a
`treeShakingSource` test asserting that a bare import of each package's
source entry retains nothing. Two conflicts with this branch:

- `PURE_FACTORY_FUNCTIONS` gained entries on both sides; kept the union.

- `SchemaJsonCodegen`'s emitted-helper sources were module-scope template
  literals interpolating each other, which esbuild retains — an interpolation
  can reach user code through `ToPrimitive`, so it is a side effect to a
  bundler that cannot see the operand is a string, and one retained constant
  pinned every constant it read. Assembled behind `@__NO_SIDE_EFFECTS__`
  functions instead, the idiom meta#9124 introduced for `INLINE_FACTORY_FORMS`.
  A bare import of the module now retains nothing.

- `inline.test.ts`'s pure-factory cross-check read `<pkg>/src/index.ts` for
  every package declaring a side-effect-free function. meta#9124 marked one in
  `@lexical/react`, which is multi-entry and has no barrel, so the read threw
  ENOENT. A package with no barrel is now treated as exporting nothing to
  this check rather than crashing it.

Co-Authored-By: Claude Opus 5 <[email protected]>
@etrepum etrepum mentioned this pull request Sep 17, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — c74f0322 Deployed Sep 7, 2026 by vercel[bot]
Preview – lexical-playground — c74f0322 Deployed Sep 7, 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.

Bug: production ESM retains most of lexical for a createCommand-only esbuild import

2 participants