TExcelTAD

Field Notes: Bundles & Publishing

Advice from shipping JavaScript to browsers and to npm, collected from the mistakes that keep recurring.

For applications

  • Measure before optimising. Run the analyzer (Toolkit: Inspecting Bundles) and look at the top five modules. The fix is almost always one of: a dependency imported in full (lodash, moment, an icon set), a development build shipped to production (NODE_ENV), or a heavy feature that should be a lazy chunk.
  • Budget the entry point on gzip size and fail CI when it grows past the budget. A budget nobody enforces is a wish.
  • Split by route, not by component. A lazy chunk per page is a big win; a lazy chunk per button is a request waterfall.
  • Hash everything except index.html. Hashed assets get a one-year immutable cache; index.html gets no-cache. Never reuse a file name for different content.
  • Keep source maps, but not necessarily public. Upload them to your error tracker on every deploy; use hidden-source-map (Webpack) or sourcemap: 'hidden' (Vite) if the code shouldn't be readable.
  • Target what you support. Set target/browserslist from your real analytics; lowering to ES2015 for browsers nobody uses costs size and speed for everybody.
  • One bundler per app. Mixing a Webpack build with an esbuild build of the same code means two sets of resolution rules and two places for bugs.

For libraries

  • Don't bundle your dependencies. Mark dependencies and peerDependencies external; let the app's package manager dedupe them.
  • Ship ES modules, and CommonJS too only if your users need it. If you ship both, use an exports map with per-format types (.d.ts / .d.cts), and keep module-level state in one place to avoid the dual-package hazard.
  • Declare sideEffects precisely: false for pure code, a list for CSS and polyfills.
  • Don't minify a library's npm build: apps minify everything anyway, and readable code in node_modules makes debugging possible. Do ship source maps.
  • Lowest reasonable syntax target, no polyfills: the app decides what to polyfill.
  • files allow-list, publishConfig, prepack builds, prepublishOnly tests. The tarball should be built fresh, from a clean checkout, in CI.

Test the tarball, not the folder

Most broken releases work perfectly in the author's repository: dist/ exists locally, the exports path has a typo nobody hits in tests, a file is missing from files. The reliable check is to pack the package, install the tarball into an empty project, and import it every supported way:

bash · try it yourself
mkdir -p lib/dist && cd lib
cat > package.json <<'JSON'
{ "name": "@learner/fmt", "version": "1.0.0", "type": "module",
  "exports": { ".": { "import": "./dist/index.js", "require": "./dist/index.cjs" } },
  "files": ["dist/index.js"] }
JSON
echo 'export const pct = (x) => `${Math.round(x * 100)}%`;' > dist/index.js
echo 'exports.pct = (x) => `${Math.round(x * 100)}%`;' > dist/index.cjs
cat > ../smoke.sh <<'SH'
set -e
tgz=$(npm pack --silent)
rm -rf /tmp/smoke && mkdir /tmp/smoke && cd /tmp/smoke && npm init -y > /dev/null
npm install "$OLDPWD/$tgz" > /dev/null
node --input-type=module -e 'import { pct } from "@learner/fmt"; console.log("import  OK", pct(0.25))'
node -e 'console.log("require OK", require("@learner/fmt").pct(0.5))'
SH
echo "== works in the repo:"
node -e 'console.log(require("./dist/index.cjs").pct(0.5))'
echo "== smoke test of the tarball:"
bash ../smoke.sh 2>&1 | grep -E 'OK|code|Cannot find'
echo "== after adding dist/index.cjs to files:"
npm pkg set 'files[]=dist/index.cjs'
bash ../smoke.sh 2>&1 | grep -E 'OK|code|Cannot find'
== works in the repo:
50%
== smoke test of the tarball:
import  OK 25%
Error: Cannot find module '/tmp/smoke/node_modules/@learner/fmt/dist/index.cjs'
  code: 'MODULE_NOT_FOUND',
== after adding dist/index.cjs to files:
import  OK 25%
require OK 50%

The repository test passed and the package was still broken: index.cjs wasn't in files, so CommonJS users would have got MODULE_NOT_FOUND. The smoke test caught it before anyone else did. Put it in CI right before npm publish. Tools such as publint and arethetypeswrong add static checks of the same kind (export paths, type declarations per condition).

Versioning and releases

  • Follow semver honestly. Dropping a Node version, removing an export, changing a default or tightening a peer range are major changes. Users' ^ ranges trust you.
  • Pre-releases go under a dist-tag (--tag next); latest is for stable releases only.
  • Deprecate, don't unpublish. A deprecation message keeps lockfiles working; unpublishing breaks them.
  • Automate the boring parts: Changesets or release-please write changelogs and bump versions from merged pull requests; CI publishes with provenance via trusted publishing.
  • Keep a CHANGELOG that says what users must do, not just what you did.

Test yourself

Why test the packed tarball instead of the repository folder?

  1. Tarballs are faster
  2. The tarball is exactly what users get; files missing from files or wrong export paths only show up there
  3. npm requires it
  4. It tests the registry

Should a library's npm build be minified?

  1. Yes, always
  2. Usually not: apps minify the final bundle, and readable library code helps debugging
  3. Only the CJS build
  4. Only the types

Which of these is a breaking (major) change for a library?

  1. Fixing a typo in the README
  2. Adding a new optional parameter
  3. Dropping support for Node 20
  4. Improving performance