Repository navigation
[lexical][lexical-compiler] Bug Fix: Let esbuild drop the editor from a createCommand-only production import - #9121
Merged
Conversation
… a createCommand-only production import
zurfyx
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7 and
potatowagon
as code owners
September 7, 2026 00:30
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Sep 7, 2026
etrepum
reviewed
Sep 7, 2026
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]>
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A consumer that imports only
createCommandfrom the production ESMlexicalpackage 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 inLexical.prod.mjsare not provably side-effect free:beforeinput) at module evaluation time. It is now built lazily on the firstaddRootElementEventscall.createRefCountedRegistryis now in the compiler's pure-factory list (its definition already carried@__NO_SIDE_EFFECTS__), so the module-scopedocumentSelectionChangeregistry gets a/* @__PURE__ */annotation. The factory allocates aMapand closures and never invokes the activation callback.LexicalEditor.versionis initialized as a static class field instead of a post-class assignment. Same value, same (writable/enumerable/configurable) property descriptor.[CACHED_TEXT_SIZE_KEY]field onLexicalNodeis nowdeclare-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 resolveslexicalto 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.Taskwithout forwardingbeforeEach/afterEachfrombench()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 viagzip -n -9. Full repro steps are in #9120.main)createCommandconsumercreateEditorconsumerLexical.prod.mjsThe shipped production ESM grows by 122 bytes; the narrow-import case shrinks by ~99%.
Before
scripts/__tests__/integration/tree-shaking.test.mjsfails against themainproduction 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.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-domon the paste benchmark: runs with the task hooks and the per-samplesize + 9child-count assertion for both sizes.Lexical.prod.mjsinspected: lazy root event table,static version=…inside the class, and noCachedTextSizesymbol reference in the class body.