|
| 1 | +import { Client, SDK } from '@rsdoctor/types'; |
| 2 | +import { getAssetsDiffResult, diffSize } from './assets'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Compute module diff between baseline and current. |
| 6 | + * Modules are matched by their file path. |
| 7 | + */ |
| 8 | +export function getModulesDiffResult( |
| 9 | + baseline: SDK.ModuleGraphData, |
| 10 | + current: SDK.ModuleGraphData, |
| 11 | +): Client.RsdoctorClientModulesDiffResult { |
| 12 | + const baselineMap = new Map<string, SDK.ModuleData>(); |
| 13 | + for (const m of baseline.modules) { |
| 14 | + baselineMap.set(m.path, m); |
| 15 | + } |
| 16 | + |
| 17 | + const currentMap = new Map<string, SDK.ModuleData>(); |
| 18 | + for (const m of current.modules) { |
| 19 | + currentMap.set(m.path, m); |
| 20 | + } |
| 21 | + |
| 22 | + const added: Client.RsdoctorClientModulesDiffResult['added'] = []; |
| 23 | + const removed: Client.RsdoctorClientModulesDiffResult['removed'] = []; |
| 24 | + const changed: Client.RsdoctorClientModuleDiffItem[] = []; |
| 25 | + |
| 26 | + for (const [path, bModule] of baselineMap) { |
| 27 | + const cModule = currentMap.get(path); |
| 28 | + if (!cModule) { |
| 29 | + removed.push({ path, size: bModule.size }); |
| 30 | + } else if (bModule.size.parsedSize !== cModule.size.parsedSize) { |
| 31 | + const { percent, state } = diffSize( |
| 32 | + bModule.size.parsedSize, |
| 33 | + cModule.size.parsedSize, |
| 34 | + ); |
| 35 | + changed.push({ |
| 36 | + path, |
| 37 | + size: { baseline: bModule.size, current: cModule.size }, |
| 38 | + percent, |
| 39 | + state, |
| 40 | + }); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + for (const [path, cModule] of currentMap) { |
| 45 | + if (!baselineMap.has(path)) { |
| 46 | + added.push({ path, size: cModule.size }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + added.sort((a, b) => a.path.localeCompare(b.path)); |
| 51 | + removed.sort((a, b) => a.path.localeCompare(b.path)); |
| 52 | + changed.sort((a, b) => a.path.localeCompare(b.path)); |
| 53 | + return { added, removed, changed }; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Compute package diff between baseline and current. |
| 58 | + * Packages are matched by name@version. |
| 59 | + */ |
| 60 | +export function getPackagesDiffResult( |
| 61 | + baseline: SDK.PackageGraphData, |
| 62 | + current: SDK.PackageGraphData, |
| 63 | +): Client.RsdoctorClientPackagesDiffResult { |
| 64 | + const toKey = (p: SDK.PackageData) => `${p.name}@${p.version}`; |
| 65 | + |
| 66 | + const baselineMap = new Map<string, SDK.PackageData>(); |
| 67 | + for (const p of baseline.packages) { |
| 68 | + baselineMap.set(toKey(p), p); |
| 69 | + } |
| 70 | + |
| 71 | + const currentMap = new Map<string, SDK.PackageData>(); |
| 72 | + for (const p of current.packages) { |
| 73 | + currentMap.set(toKey(p), p); |
| 74 | + } |
| 75 | + |
| 76 | + const added: Client.RsdoctorClientPackagesDiffResult['added'] = []; |
| 77 | + const removed: Client.RsdoctorClientPackagesDiffResult['removed'] = []; |
| 78 | + const changed: Client.RsdoctorClientPackageDiffItem[] = []; |
| 79 | + |
| 80 | + for (const [key, bPkg] of baselineMap) { |
| 81 | + const cPkg = currentMap.get(key); |
| 82 | + if (!cPkg) { |
| 83 | + removed.push({ |
| 84 | + name: bPkg.name, |
| 85 | + version: bPkg.version, |
| 86 | + root: bPkg.root, |
| 87 | + size: bPkg.size, |
| 88 | + }); |
| 89 | + } else if (bPkg.size.parsedSize !== cPkg.size.parsedSize) { |
| 90 | + const { percent, state } = diffSize( |
| 91 | + bPkg.size.parsedSize, |
| 92 | + cPkg.size.parsedSize, |
| 93 | + ); |
| 94 | + changed.push({ |
| 95 | + name: bPkg.name, |
| 96 | + version: bPkg.version, |
| 97 | + root: bPkg.root, |
| 98 | + size: { baseline: bPkg.size, current: cPkg.size }, |
| 99 | + percent, |
| 100 | + state, |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + for (const [key, cPkg] of currentMap) { |
| 106 | + if (!baselineMap.has(key)) { |
| 107 | + added.push({ |
| 108 | + name: cPkg.name, |
| 109 | + version: cPkg.version, |
| 110 | + root: cPkg.root, |
| 111 | + size: cPkg.size, |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return { added, removed, changed }; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Compute the full bundle diff result between baseline and current manifest data. |
| 121 | + * Combines asset, module, and package diffs into a single result. |
| 122 | + */ |
| 123 | +export function getBundleDiffResult( |
| 124 | + baseline: SDK.BuilderStoreData, |
| 125 | + current: SDK.BuilderStoreData, |
| 126 | +): Client.RsdoctorClientBundleDiffResult { |
| 127 | + return { |
| 128 | + assets: getAssetsDiffResult(baseline.chunkGraph, current.chunkGraph), |
| 129 | + modules: getModulesDiffResult(baseline.moduleGraph, current.moduleGraph), |
| 130 | + packages: getPackagesDiffResult( |
| 131 | + baseline.packageGraph, |
| 132 | + current.packageGraph, |
| 133 | + ), |
| 134 | + }; |
| 135 | +} |
0 commit comments