feat: add exports map, sideEffects flag, and named country exports for tree-shaking - #166
Open
jsumnerp wants to merge 1 commit into
Open
feat: add exports map, sideEffects flag, and named country exports for tree-shaking#166jsumnerp wants to merge 1 commit into
jsumnerp wants to merge 1 commit into
Conversation
…r tree-shaking
Importing the stdnum barrel object bundles every country's validators
(~205KB minified) even when only a few are used, because the eagerly-built
object defeats tree-shaking and the package exposes no other entry points.
- Add an "exports" map (replacing the sketched "exports_NOT_YET"): official
per-validator subpaths (stdnum/gb/nino) with a "node" condition so Node
keeps resolving to the CJS build (lib/esm is bundler-only: no type:module
and extensionless relative imports). Identity passthroughs preserve
existing deep imports (stdnum/lib/esm/..., stdnum/lib/cjs/..., stdnum/src/...).
- Add "sideEffects": false so bundlers can drop unused modules.
- Add named per-country exports (import { GB } from 'stdnum') as a
tree-shakeable alternative to the stdnum object, which is unchanged.
Measured with esbuild (minify, browser): barrel 205KB, named country
import 8KB, single-validator subpath 5KB.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
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.
Problem
Importing anything from stdnum currently bundles every country's validators (~205KB minified / ~50KB gzip), because the
stdnumobject eagerly references all of them and the package publishes no other entry points. Consumers who need a handful of validators resort to fragile deep imports intolib/esm/...internals.This PR finishes what the
exports_NOT_YETsketch in package.json started (its trailing-slash form was deprecated and later removed by Node, so it wouldn't work as-is).Changes
exportsmap — official subpaths for every validator and country:nodecondition resolves to the CJS build for bothrequireandimportin Node. This is deliberate:lib/esmis bundler-only today (the package has no"type": "module"and the ESM output uses extensionless relative imports, which Node's ESM resolver rejects), so pointing Node at it would break. Node behaviour is exactly whatmaingives today.nodecondition when targeting browsers) hitimport→ the ESM build, same as today'smodulefield.stdnum/lib/esm/*,stdnum/lib/cjs/*(with or without.js), andstdnum/src/*.main/module/typesfields are unchanged, so old resolvers (including TSnode10) are unaffected."sideEffects": false— lets bundlers drop unused modules.Named per-country exports in
src/index.ts—import { GB } from 'stdnum'is now tree-shakeable. Thestdnumobject export is unchanged for full backward compatibility.README section documenting the tree-shakeable forms.
Verification
npm test: 1611/1611 pass (underTZ=UTC;mx/curphas a pre-existing timezone-sensitive assertion that fails on non-UTC machines on main too)npm run lint,npm run prepublishOnly(both tsconfigs),prettier --check: cleanrequire('stdnum'),require('stdnum/gb/nino'),require('stdnum/lib/cjs/gb/nino')±.js— all workimport { stdnum, GB } from 'stdnum'andimport { validate } from 'stdnum/gb/nino'— work (resolve to CJS via thenodecondition)moduleResolution: bundlerandnode16{ stdnum }205KB (unchanged), named{ GB }8KB, subpathstdnum/gb/nino5KB, legacystdnum/lib/esm/gb/ninostill resolves🤖 Generated with Claude Code