Repository navigation
[*] Bug Fix: Remove the remaining module-scope side effects that defeat tree-shaking - #9124
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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
force-pushed
the
claude/lexical-tree-shaking-y5xg1v
branch
from
September 7, 2026 05:20
ee528e7 to
05ddba4
Compare
etrepum
marked this pull request as ready for review
September 7, 2026 05:40
etrepum
requested review from
acywatson,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
September 7, 2026 05:40
potatowagon
approved these changes
Sep 7, 2026
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
pushed a commit
to etrepum/lexical
that referenced
this pull request
Sep 7, 2026
…t against every published entry, not just a barrel ## Description The cross-check that every `@__NO_SIDE_EFFECTS__` function a package exports is listed in `PURE_FACTORY_FUNCTIONS` read `<pkg>/src/index.ts`. A barrel is not what makes a name public — the `exports` map is — and `@lexical/react` publishes each module as its own entry and has no barrel at all, so the read threw `ENOENT` as soon as meta#9124 marked a function there. It also collected only `export {…}` specifiers, which is what a barrel is made of, and missed the `export function` form a module that is its own entry point uses. It now reads the `source` export condition of every entry in the package's `exports` map, through the same `packagesManager` the tree-shaking test uses, and collects both forms. That surfaces one function the list was missing: `newContext`, exported from `@lexical/react/LexicalCollaborationContextUtils` and marked side-effect free there. `@internal` marks it as unsupported API, not as unreachable — it is importable by name from another package — so a module-scope call to it went unannotated and pinned the caller's module. Listing it is what the annotation contract in AGENTS.md asks for, and it only ever adds an annotation to a call to a function already declared pure. ## Test plan ### Before ``` $ npx vitest run --project unit packages/lexical-compiler FAIL the inlined factories are still trivial > agrees with the declarations about which factories are pure Error: ENOENT: no such file or directory, open 'packages/lexical-react/src/index.ts' ``` With the read fixed but the list untouched, the check reports what it was blind to: ``` AssertionError: expected [ 'newContext' ] to deeply equal [] ``` A cross-package call was left unannotated: ``` $ node -e "…transformPureAnnotations(\"import {newContext} from '@lexical/react/LexicalCollaborationContextUtils';\nexport const ctx = newContext();\")" (no change) ``` ### After ``` $ node -e "…" import {newContext} from '@lexical/react/LexicalCollaborationContextUtils'; export const ctx = /* @__PURE__ */newContext(); $ pnpm run test-unit Test Files 339 passed (339) Tests 6443 passed | 1 skipped (6444) $ pnpm run ci-check Running Flow... No errors! ``` Browser and E2E suites were not run; the change is build-tooling and test-tooling only, and no runtime behavior is touched. Co-Authored-By: Claude Opus 5 <[email protected]>
etrepum
pushed a commit
to etrepum/lexical
that referenced
this pull request
Sep 7, 2026
…dle that imports @lexical/table ## Description `tableCellNodeSchema` named its default header state as `TableCellHeaderStates.NO_STATUS`. That is a property read at module scope, and esbuild and webpack treat one as a side effect — they cannot see that the object has no getter — so the statement was retained together with everything it references, which is the whole schema and the states object. Every consumer of `@lexical/table` paid for it, however little of the package they imported. The states are now bindings that the exported object is collected from, so the schema names one directly and the read is gone. `TableCellHeaderStates` keeps the same shape, the same members and the same inferred member type. Found by the tree-shaking integration test added in meta#9124, which measures the published bundles. Its source-level counterpart did not catch it, and cannot: `bareImportResidue` overrides `sideEffects: false` for the entry module alone, so for a package whose entry is a barrel — which is most of them — esbuild drops every re-exported module wholesale and there is nothing left to measure. Measured directly, `packages/lexical-table/src/LexicalTableCellNode.ts` retained 2129 characters where `src/index.ts` retained none. Left alone here rather than changed under a table fix, since widening it is main's call and would surface whatever else it has been unable to see. Swept the rest of the tree the same way in the meantime: of the 28 modules that declare a serialization schema or a `$config`, this was the only one retaining anything. ## Test plan ### Before ``` $ pnpm run test-integration FAIL a bare import of a published ESM build retains nothing > @lexical/table (packages/lexical-table/dist/LexicalTable.prod.mjs) AssertionError: expected 'var gt = { BOTH: 3, COLUMN: 2, NO_STA…' to be '' + var gt = { BOTH: 3, COLUMN: 2, NO_STATUS: 0, ROW: 1 }; + var mt = /* @__PURE__ */ u()({ backgroundColor: …, headerState: /* @__PURE__ */ g(/* @__PURE__ */ f(gt.NO_STATUS), …), … }); ``` The reduced case, which is the rule in AGENTS.md: ``` property read on local object: RETAINED -> var S = { A: 0 }; var v = /* @__PURE__ */ f(S.A); property read on exported object: RETAINED -> var S = { A: 0 }; var v = /* @__PURE__ */ f(S.A); identifier, object built from it: dropped ``` ### After ``` $ pnpm run build-prod && npx vitest run --project integration scripts/__tests__/integration/tree-shaking.test.mjs ✓ @lexical/table (packages/lexical-table/dist/LexicalTable.prod.mjs) 15ms ✓ @lexical/table (packages/lexical-table/dist/LexicalTable.dev.mjs) 13ms Test Files 1 passed (1) Tests 210 passed (210) $ pnpm run test-unit Test Files 339 passed (339) Tests 6443 passed | 1 skipped (6444) $ pnpm run ci-check Running Flow... No errors! ``` Browser and E2E suites were not run; the change is a module-scope binding with no runtime behavior attached. 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
#9121 fixed the four module-scope statements in
lexicalthat kept nearly the whole package in a bundle importing onlycreateCommand, 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'ssideEffects: falseoverridden 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 inlexicaland 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 oflexicalkeeps 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 (
navigatorandwindowprobes inenvironment.ts,Date.nowpassed 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/.splitbuilding constants, the emoji feature-detection IIFE, React'sforwardRefandcreateContext, local factories such ascreateBlockNode,listReplace,createLinkMatcherWithRegExpand tailwind'sjoin, andwarnOnlyOnce(...), 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, theselselector wrappers);inand|expressions over other bindings (SUPPORTS_CSS_HIGHLIGHTS,TEXT_FORMAT_MASK,IS_ALL_FORMATTING); a shiki highlighter and regex engine created at import time; terser rewritingInfinityto1/0, which esbuild keeps as a division; and@preact/signals-coreinlined 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/compilerannotates 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, sowarnOnlyOncecalls are annotated, andcreateLinkMatcherWithRegExpandwarnOnlyOncejoin the trusted factory list.@preact/signals-coreis left external in the npm build (it declaressideEffects: falseitself, so a consumer's bundler drops it when unused), terser keepsInfinity, and the shiki highlighter is created on first use.Two tests replace the single size ceiling with an invariant.
scripts/__tests__/unit/treeShakingSource.test.tsruns 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 withtest-unit.scripts/__tests__/integration/tree-shaking.test.mjsruns it over every published dev and prod ESM build, and keeps #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.
Follow-up to #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):Before
On main (111e732, after #9121):
After
(
createEditorconsumer: 162,994 → 162,894 bytes, so an editor still costs an editor.)pnpm exec vitest run scripts/__tests__/integration/tree-shaking.test.mjs(afterpnpm run build --prod --release):pnpm run test-unit(includes the newscripts/__tests__/unit/treeShakingSource.test.ts, 102 entries):pnpm run tsc,pnpm run tsc-scripts,pnpm run flow,pnpm run prettierandpnpm run lintpass.The
test-integrationproject (which runsprepare-releasefirst) and the browser/e2e suites were not run here.