Skip to content

Publish one ES module per source file so bundlers can drop unused distributions - #54

Open
ZacKienzle2 wants to merge 1 commit into
d3:mainfrom
ZacKienzle2:fix/preserve-modules
Open

ZacKienzle2 wants to merge 1 commit into
d3:mainfrom
ZacKienzle2:fix/preserve-modules

Conversation

@ZacKienzle2

Copy link
Copy Markdown

Since 3.0.2 (#53) the package entry is one bundled file, dist/d3-random.mjs, instead of the separate files under src/. sideEffects: false used to let a bundler drop every distribution a project does not import. With one file the module is always kept, and the bundler then has to prove that each top-level statement can be removed. Every distribution is exported as an immediately invoked function,

var int = (function sourceRandomInt(source) { ... })(defaultSource);

which esbuild and Next.js's Turbopack keep, so importing one distribution ships all seventeen. d3/d3-shape#217 describes the same effect in d3-shape, d3-array and d3-time.

This adds one more Rollup output, dist/esm, written with output.preserveModules so that it has one file per source module, and points module, main and exports.default at dist/esm/index.js. That is the layout 3.0.1 published as src/, now built by Rollup. dist/d3-random.mjs, dist/d3-random.min.mjs and the UMD builds are unchanged (the rebuilt dist/d3-random.mjs is byte-identical to the published 3.0.2), and the source is not touched. Tests and lint pass. The packed tarball grows from 6.1 kB to 7.7 kB.

Effect

A Next.js 16.4.4 (Turbopack) site that imports only randomInt in two drill chunks and only randomNormal in a third chunk. Client bytes of d3-random per chunk, raw / compressed:

d3-random build chunk importing randomInt chunk importing randomNormal
3.0.2 3,297 / 1,037 B 3,308 / 1,041 B
this PR 240 / 189 B 315 / 244 B
3.0.1 (src/ files) 228 / 179 B 307 / 238 B
/*#__PURE__*/ on each IIFE instead 896 / 475 B 907 / 478 B

With esbuild (echo 'export {randomInt} from "d3-random"' | esbuild --bundle --minify --format=esm) the output goes from 3.3 kB to 214 B.

How it was measured

The site was built once per d3-random build in one run: npm ci, then npm install <packed tarball> --no-save (or d3-random@3.0.1), then next experimental-analyze --output. That writes the bundle analyzer's data to .next/diagnostics/analyze/data/<route>/analyze.data. The numbers above are the analyzer's own size and compressed_size of the chunk parts whose source is under node_modules/d3-random/, summed per client output file. The site's seeded-random tests give the same streams with every build.

Why not pure annotations

Adding /*#__PURE__*/ before each of the seventeen calls is enough for esbuild (233 B), and Rollup keeps the annotations in its output, but Turbopack stops at 896 B. Its minifier runs two compress passes (passes: 2 in turbopack-ecmascript/src/emit_options.rs), and the distributions refer to one another (poisson to binomial and gamma, binomial to beta and geometric, beta to gamma, gamma and logNormal to normal), so a short chain outlives the passes. With separate files the bundler drops whole modules from its module graph, so the result does not depend on minifier passes.

Since 3.0.2 the package entry is one bundled file, so sideEffects: false no
longer lets a bundler drop unused distributions, and a bundler has to prove
each top-level call removable instead. Rollup's output.preserveModules writes
dist/esm with one file per source module, as 3.0.1 published src/, and the
package entry points there. dist/d3-random.mjs and the UMD builds are
unchanged.

Bundling an import of randomInt alone with `esbuild --bundle --minify`
gives 3.3 kB before and 214 B after.
@Fil

Fil commented Nov 22, 2026

Copy link
Copy Markdown
Member

ref. d3/d3-shape#217

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants