Repository navigation
Publish one ES module per source file so bundlers can drop unused distributions - #54
Open
ZacKienzle2 wants to merge 1 commit into
Open
ZacKienzle2 wants to merge 1 commit into
ZacKienzle2 wants to merge 1 commit into
Conversation
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.
Member
|
ref. d3/d3-shape#217 |
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.
Since 3.0.2 (#53) the package entry is one bundled file,
dist/d3-random.mjs, instead of the separate files undersrc/.sideEffects: falseused 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,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 withoutput.preserveModulesso that it has one file per source module, and pointsmodule,mainandexports.defaultatdist/esm/index.js. That is the layout 3.0.1 published assrc/, now built by Rollup.dist/d3-random.mjs,dist/d3-random.min.mjsand the UMD builds are unchanged (the rebuiltdist/d3-random.mjsis 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
randomIntin two drill chunks and onlyrandomNormalin a third chunk. Client bytes of d3-random per chunk, raw / compressed:randomIntrandomNormalsrc/files)/*#__PURE__*/on each IIFE insteadWith 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, thennpm install <packed tarball> --no-save(ord3-random@3.0.1), thennext 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 ownsizeandcompressed_sizeof the chunk parts whose source is undernode_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: 2inturbopack-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.