|
| 1 | +import dedent from 'dedent'; |
| 2 | +import type { Check, SizeLimitConfig } from 'size-limit'; |
| 3 | +import * as v from './src'; |
| 4 | + |
| 5 | +const paths = ['dist/index.js', 'dist/index.cjs']; |
| 6 | + |
| 7 | +const checks = paths.map( |
| 8 | + (path): Check => ({ |
| 9 | + name: `Full bundle (${path.includes('.cjs') ? 'CJS' : 'ESM'})`, |
| 10 | + path, |
| 11 | + import: '*', |
| 12 | + }) |
| 13 | +); |
| 14 | + |
| 15 | +const importNames = new Set(Object.keys(v)); |
| 16 | + |
| 17 | +/** |
| 18 | + * Build a config for an individual export's size. |
| 19 | + * @param {string} importName Name of the export. |
| 20 | + * @returns {Check} The config. |
| 21 | + */ |
| 22 | +function individualExportConfig(importName: string): Check { |
| 23 | + return { |
| 24 | + name: `\`v.${importName}\``, |
| 25 | + // only check ESM individual exports |
| 26 | + path: paths[0], |
| 27 | + import: `{ ${importName} }`, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +for (const importName of importNames) { |
| 32 | + // if it's a reserved name or internal, skip it |
| 33 | + if (importNames.has(importName + '_') || importName.startsWith('_')) continue; |
| 34 | + checks.push(individualExportConfig(importName)); |
| 35 | +} |
| 36 | + |
| 37 | +// a v.<name>( call |
| 38 | +const vRegex = /v\.(\w+)\(/g; |
| 39 | + |
| 40 | +/** |
| 41 | + * Build a config for a TypeScript example's size. |
| 42 | + * @param {TemplateStringsArray} strings Template strings. |
| 43 | + * @param {...unknown} values Template values. |
| 44 | + * @returns {Check} The config. |
| 45 | + */ |
| 46 | +function ts(strings: TemplateStringsArray, ...values: unknown[]): Check { |
| 47 | + const dedented = dedent(strings, ...values); |
| 48 | + const usedImports = Array.from(dedented.matchAll(vRegex)).map((m) => m[1]); |
| 49 | + const uniqueImports = Array.from(new Set(usedImports)).map((importName) => |
| 50 | + importNames.has(importName + '_') ? importName + '_' : importName |
| 51 | + ); |
| 52 | + return { |
| 53 | + name: `\`\`\`ts\n${dedented}\n\`\`\``, |
| 54 | + // ESM only |
| 55 | + path: paths[0], |
| 56 | + import: `{ ${uniqueImports.join(', ')} }`, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +checks.push( |
| 61 | + ts` |
| 62 | + v.object({ |
| 63 | + email: v.pipe(v.string(), v.email()), |
| 64 | + password: v.pipe(v.string(), v.minLength(8)), |
| 65 | + }) |
| 66 | +` |
| 67 | +); |
| 68 | + |
| 69 | +export default checks satisfies SizeLimitConfig; |
0 commit comments