Find the cost of adding an npm package to your app's bundle size teardown.kelinci.dev
14
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: move stuff around

Mary abb26e2f b1f39c42

+20 -51
+20 -2
src/npm/lib/hoist.test.ts
··· 1 1 import { describe, expect, it } from 'vitest'; 2 2 3 - import { hoist, hoistedToPaths } from './hoist'; 4 - import type { ResolvedPackage } from './types'; 3 + import { hoist } from './hoist'; 4 + import type { HoistedNode, HoistedResult, ResolvedPackage } from './types'; 5 + 6 + /** converts hoisted result to flat paths for easy testing. */ 7 + function hoistedToPaths(result: HoistedResult): string[] { 8 + const paths: string[] = []; 9 + 10 + function walk(nodes: Map<string, HoistedNode>, prefix: string): void { 11 + for (const [name, node] of nodes) { 12 + const path = `${prefix}/${name}`; 13 + paths.push(path); 14 + if (node.nested.size > 0) { 15 + walk(node.nested, `${path}/node_modules`); 16 + } 17 + } 18 + } 19 + 20 + walk(result.root, 'node_modules'); 21 + return paths.sort(); 22 + } 5 23 6 24 // #region test helpers 7 25
-24
src/npm/lib/hoist.ts
··· 143 143 return { root }; 144 144 } 145 145 146 - /** 147 - * converts a hoisted result to a flat list of paths. 148 - * useful for debugging and testing. 149 - * 150 - * @param result the hoisted result 151 - * @returns array of paths like ["node_modules/react", "node_modules/react/node_modules/scheduler"] 152 - */ 153 - export function hoistedToPaths(result: HoistedResult): string[] { 154 - const paths: string[] = []; 155 - 156 - function walk(nodes: Map<string, HoistedNode>, prefix: string): void { 157 - for (const [name, node] of nodes) { 158 - const path = `${prefix}/${name}`; 159 - paths.push(path); 160 - if (node.nested.size > 0) { 161 - walk(node.nested, `${path}/node_modules`); 162 - } 163 - } 164 - } 165 - 166 - walk(result.root, 'node_modules'); 167 - // oxlint-disable-next-line unicorn/no-array-sort 168 - return paths.sort(); 169 - }
-8
src/npm/lib/registry.ts
··· 101 101 packumentCache.set(cacheKey, packument); 102 102 return packument; 103 103 } 104 - 105 - /** 106 - * clears the packument cache. 107 - * useful for testing or when you want fresh data. 108 - */ 109 - export function clearPackumentCache(): void { 110 - packumentCache.clear(); 111 - }
-17
src/npm/lib/subpaths.ts
··· 281 281 }; 282 282 } 283 283 284 - /** 285 - * gets the import specifier for a subpath entry. 286 - * this is what you'd write in an import statement. 287 - * 288 - * @param packageName the package name 289 - * @param entry the subpath entry 290 - * @returns the import specifier (e.g., "react", "react/jsx-runtime") 291 - */ 292 - export function getImportSpecifier(packageName: string, entry: Subpath): string { 293 - if (entry.subpath === '.') { 294 - return packageName; 295 - } 296 - 297 - // "./foo" -> "package/foo" 298 - return `${packageName}${entry.subpath.slice(1)}`; 299 - } 300 - 301 284 // #endregion