diff --git a/README.md b/README.md index 11e463b..52468db 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,27 @@ VAT, Person and Tax identifiers. All country validators are in the "namespace" of the ISO country code. +## Bundle size: tree-shakeable imports + +The `stdnum` object eagerly references every country, so importing it bundles +all of them (~200KB minified). If you only need a few validators, use either of +these forms instead: + + // Named country exports — tree-shakeable, bundlers drop unused countries + import { GB, BR } from 'stdnum'; + + GB.nino.validate('AB123456C'); + BR.cpf.validate('390.533.447-05'); + + // Or import a single validator via its subpath + import { validate } from 'stdnum/gb/nino'; + + validate('AB123456C'); + +Subpaths follow the source layout: `stdnum//` (e.g. +`stdnum/it/codicefiscale`), and `stdnum/` for a whole country's +validators. + ## Supported Countries and Numbers How you can help! This library currently support about half the countries in the world. It would be great if we can get to 100%. Submit an issue with any reference documentation and I'll do my best to integrate it, bonus points if you can get detailed descriptions of checksums or other validation criteria. diff --git a/package.json b/package.json index e689865..50cc613 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,25 @@ "main": "./lib/cjs/index.js", "module": "./lib/esm/index.js", "types": "./lib/cjs/index.d.ts", - "exports_NOT_YET": { - "./": { - "import": "./lib/esm/", - "require": "./lib/cjs/" + "sideEffects": false, + "exports": { + ".": { + "types": "./lib/cjs/index.d.ts", + "node": "./lib/cjs/index.js", + "import": "./lib/esm/index.js", + "default": "./lib/cjs/index.js" + }, + "./package.json": "./package.json", + "./lib/esm/*.js": "./lib/esm/*.js", + "./lib/esm/*": "./lib/esm/*.js", + "./lib/cjs/*.js": "./lib/cjs/*.js", + "./lib/cjs/*": "./lib/cjs/*.js", + "./src/*": "./src/*", + "./*": { + "types": "./lib/esm/*.d.ts", + "node": "./lib/cjs/*.js", + "import": "./lib/esm/*.js", + "default": "./lib/cjs/*.js" } }, "overrides": { diff --git a/src/index.ts b/src/index.ts index 082fdda..710ae19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,6 +95,106 @@ import type { Validator } from './types'; export type { Validator } from './types'; +// Named per-country exports. Unlike the `stdnum` object below (which eagerly +// references every country and therefore always bundles all of them), these are +// tree-shakeable: `import { GB } from 'stdnum'` lets a bundler drop the other +// countries entirely. +export { + AD, + AI, + AL, + AO, + AR, + AT, + AU, + AZ, + BA, + BE, + BG, + BO, + BR, + BY, + BZ, + CA, + CH, + CL, + CN, + CO, + CR, + CU, + CY, + CZ, + DE, + DK, + DO, + DZ, + EC, + EE, + EG, + ES, + FI, + FO, + FR, + GB, + GH, + GR, + GN, + GT, + HK, + HR, + HU, + ID, + IE, + IL, + IN, + IS, + IT, + JP, + KE, + KR, + LI, + LT, + LU, + LV, + MA, + MC, + MD, + ME, + MK, + MT, + MU, + MX, + MY, + MZ, + NL, + NO, + NZ, + PE, + PK, + PL, + PT, + PY, + RO, + RS, + RU, + SE, + SG, + SI, + SK, + SM, + SV, + TH, + TN, + TR, + TW, + UA, + US, + UY, + VE, + VN, + ZA, +}; + // Live an uppercase world, to prevent keyword collisions export const stdnum: Record> = { AD,