Skip to content

Commit e2db2c7

Browse files
refactor(signals): chunk only the prod ESM dist, keep dev/node flat
The per-module tree only pays off for bundlers, which are the sole consumers of the prod ESM path and scope-hoist it back into one module. Serving the tree everywhere charged real costs elsewhere: vitest's per-module SSR transform made the flush hot path ~2x slower (the CodSpeed -47% on update-partial was this artifact, not the hook refactor, which measures 0-5% in flat-vs-flat comparison), and unbundled node consumers paid ~10% per-module-boundary overhead that CJS require can't ever recoup through tree-shaking. Dev and node CJS return to flat single files; dist/prod stays a preserveModules tree. Co-authored-by: Cursor <[email protected]>
1 parent 6ed3e5a commit e2db2c7

4 files changed

Lines changed: 31 additions & 25 deletions

File tree

‎.changeset/pay-for-use-treeshaking-phase-1.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"@solidjs/signals": patch
33
---
44

5-
Make `affects()` and `isPending()`/`latest()` pay-for-use (#2883, phase 1). The affects mark engine and sentinel propagation move out of `async.ts`/`scheduler.ts` into the `affects` feature module, and the whole isPending/latest verdict layer (probe, companions, re-ask classification, the `latest()` read path) moves to a new internal `verdict` module; both install nullable `GlobalQueue` hooks on import, so bundles that never import these APIs never ship their machinery. The published dist is now a per-module tree (`dist/dev/`, `dist/prod/`, `dist/node/`) instead of one flat file — statement-level tree-shaking can never drop a flat file's top-level hook installs, so the split is what makes the hooks pay off for npm consumers. `_`-prefixed property mangling runs as a single sequential pass with one shared name cache per tree, keeping mangled names consistent across module files.
5+
Make `affects()` and `isPending()`/`latest()` pay-for-use (#2883, phase 1). The affects mark engine and sentinel propagation move out of `async.ts`/`scheduler.ts` into the `affects` feature module, and the whole isPending/latest verdict layer (probe, companions, re-ask classification, the `latest()` read path) moves to a new internal `verdict` module; both install nullable `GlobalQueue` hooks on import, so bundles that never import these APIs never ship their machinery. The prod ESM dist is now a per-module tree (`dist/prod/`) instead of one flat file — statement-level tree-shaking can never drop a flat file's top-level hook installs, so the split is what makes the hooks pay off for npm consumers. It is the only output that changes shape: bundlers are its sole consumers and scope-hoist it back into one module, so nobody pays the per-module-boundary call cost at runtime. Dev (`dist/dev.js`) and node CJS (`dist/node.cjs`) stay flat single files — dev bundle size doesn't matter and CJS can't tree-shake, while the flat hot path stays fast for unbundled and test-transformed consumers. `_`-prefixed property mangling runs as a single sequential pass with one shared name cache per prod output, keeping mangled names consistent across module files.
66

77
Measured on a minimal app (render + one signal) bundled from the published dist: 12.7 → 11.4 KB gzip. Core-primitives floor from src: 8.9 → 8.2 KB gzip. Apps importing every feature pay ~0.7 KB min of hook shims. A new `treeshake` test guards the retained-module graph and the floor's byte ceiling so re-coupling fails CI.
88

‎packages/solid-signals/package.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"publishConfig": {
1414
"access": "public"
1515
},
16-
"main": "./dist/node/index.cjs",
16+
"main": "./dist/node.cjs",
1717
"module": "./dist/prod/index.js",
1818
"types": "./dist/types/index.d.ts",
1919
"type": "module",
@@ -26,21 +26,21 @@
2626
".": {
2727
"import": {
2828
"types": "./dist/types/index.d.ts",
29-
"test": "./dist/dev/index.js",
30-
"development": "./dist/dev/index.js",
29+
"test": "./dist/dev.js",
30+
"development": "./dist/dev.js",
3131
"default": "./dist/prod/index.js"
3232
},
3333
"require": {
3434
"types": "./dist/types-cjs/index.d.cts",
35-
"default": "./dist/node/index.cjs"
35+
"default": "./dist/node.cjs"
3636
}
3737
},
3838
"./package.json": "./package.json"
3939
},
4040
"scripts": {
4141
"build": "npm-run-all -nl build:* && pnpm types",
4242
"build:clean": "rimraf dist/dev dist/prod dist/node dist/dev.js dist/prod.js dist/node.cjs",
43-
"build:js": "rollup -c && node ./scripts/mangle-props.mjs dist/prod dist/node",
43+
"build:js": "rollup -c && node ./scripts/mangle-props.mjs dist/prod dist/node.cjs",
4444
"types": "tsc -p tsconfig.build.json && node ../../scripts/sync-dual-types.mjs ./dist/types ./dist/types-cjs",
4545
"test": "vitest run",
4646
"test:watch": "vitest watch tests",

‎packages/solid-signals/rollup.config.js‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ import replace from "@rollup/plugin-replace";
22
import typescript from "@rollup/plugin-typescript";
33
import prettier from "rollup-plugin-prettier";
44

5-
// Each output is a per-module tree (`preserveModules`), not a flat bundle, so
6-
// consumer bundlers can drop whole feature modules — including their top-level
7-
// GlobalQueue hook installs, which statement-level shaking of a flat file can
8-
// never remove (#2883). `_`-prefixed property mangling runs as a single
9-
// sequential post-pass (scripts/mangle-props.mjs) with one shared nameCache
10-
// per tree; per-chunk terser would mangle the same property to different
11-
// names in different modules and break every cross-module member access.
5+
// Only the prod ESM build is a per-module tree (`preserveModules`); it is
6+
// consumed exclusively by bundlers, which can drop whole feature modules —
7+
// including their top-level GlobalQueue hook installs, which statement-level
8+
// shaking of a flat file can never remove (#2883) — and scope-hoist the rest
9+
// back into one module. Dev and node stay flat single files: dev bundle size
10+
// doesn't matter (and vitest's per-module SSR transform makes a chunked tree
11+
// ~2x slower in the flush hot path, poisoning CI benches), and CJS `require`
12+
// can't tree-shake, so a tree would charge unbundled SSR the per-module-
13+
// boundary cost (~10% native ESM) for nothing.
14+
//
15+
// `_`-prefixed property mangling for prod outputs runs as a single sequential
16+
// post-pass (scripts/mangle-props.mjs) with one shared nameCache per output;
17+
// per-chunk terser would mangle the same property to different names in
18+
// different modules and break every cross-module member access.
1219
export default [
1320
{
1421
input: "src/index.ts",
1522
output: {
16-
dir: "dist/dev",
17-
format: "esm",
18-
preserveModules: true,
19-
preserveModulesRoot: "src"
23+
file: "dist/dev.js",
24+
format: "esm"
2025
},
2126
plugins: [
2227
replace({
@@ -26,7 +31,7 @@ export default [
2631
}),
2732
typescript({
2833
declaration: false,
29-
outDir: "dist/dev",
34+
outDir: "dist",
3035
module: "esnext",
3136
target: "esnext",
3237
moduleResolution: "bundler",
@@ -67,11 +72,8 @@ export default [
6772
{
6873
input: "src/index.ts",
6974
output: {
70-
dir: "dist/node",
75+
file: "dist/node.cjs",
7176
format: "cjs",
72-
preserveModules: true,
73-
preserveModulesRoot: "src",
74-
entryFileNames: "[name].cjs",
7577
exports: "named"
7678
},
7779
plugins: [
@@ -82,7 +84,7 @@ export default [
8284
}),
8385
typescript({
8486
declaration: false,
85-
outDir: "dist/node",
87+
outDir: "dist",
8688
module: "esnext",
8789
target: "esnext",
8890
moduleResolution: "bundler",

‎packages/solid-signals/scripts/mangle-props.mjs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
* options, so `_pendingValue` could mangle to different names in different
88
* modules and break cross-module member access at runtime (#2883).
99
*
10-
* Usage: node scripts/mangle-props.mjs <dist-dir> [<dist-dir> ...]
10+
* Flat single-file bundles are self-contained consistency domains, so each
11+
* argument (directory tree or single file) gets its own nameCache.
12+
*
13+
* Usage: node scripts/mangle-props.mjs <dist-dir-or-file> [<dist-dir-or-file> ...]
1114
*/
12-
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
15+
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
1316
import { join } from "node:path";
1417
import { minify } from "terser";
1518

1619
function walk(dir) {
20+
if (statSync(dir).isFile()) return [dir];
1721
const files = [];
1822
for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) =>
1923
a.name.localeCompare(b.name)

0 commit comments

Comments
 (0)