From 8495e5a5a15b38edf22a8ce9b76a75998fbef69d Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Oct 2024 12:59:59 +0200 Subject: [PATCH] fix(cli): count preloaded modules in bundle size and use decimal system for file size (#77) --- .../--/bundles/[bundle]/modules/graph+api.ts | 6 ++++-- .../--/bundles/[bundle]/modules/index+api.ts | 6 ++++-- packages/expo-atlas-ui/components/FileSize.tsx | 17 ++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/graph+api.ts b/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/graph+api.ts index 8cb180a..453e0a5 100644 --- a/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/graph+api.ts +++ b/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/graph+api.ts @@ -37,8 +37,10 @@ export async function GET(request: Request, params: Record<'bundle', string>) { data: finalizeModuleTree(createModuleTree(bundle, filteredModules)), bundle: { platform: bundle.platform as any, - moduleSize: allModules.reduce((size, module) => size + module.size, 0), - moduleFiles: bundle.modules.size, + moduleFiles: bundle.modules.size + bundle.runtimeModules.length, + moduleSize: allModules + .concat(bundle.runtimeModules) + .reduce((size, module) => size + module.size, 0), }, filtered: { moduleSize: filteredModules.reduce((size, module) => size + module.size, 0), diff --git a/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/index+api.ts b/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/index+api.ts index 2ce338a..96606a0 100644 --- a/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/index+api.ts +++ b/packages/expo-atlas-ui/app/--/bundles/[bundle]/modules/index+api.ts @@ -44,8 +44,10 @@ export async function GET(request: Request, params: Record<'bundle', string>) { })), bundle: { platform: bundle.platform as any, - moduleSize: allModules.reduce((size, module) => size + module.size, 0), - moduleFiles: bundle.modules.size, + moduleFiles: bundle.modules.size + bundle.runtimeModules.length, + moduleSize: allModules + .concat(bundle.runtimeModules) + .reduce((size, module) => size + module.size, 0), }, filtered: { moduleSize: filteredModules.reduce((size, module) => size + module.size, 0), diff --git a/packages/expo-atlas-ui/components/FileSize.tsx b/packages/expo-atlas-ui/components/FileSize.tsx index 01ddd1c..f6af4d4 100644 --- a/packages/expo-atlas-ui/components/FileSize.tsx +++ b/packages/expo-atlas-ui/components/FileSize.tsx @@ -40,13 +40,16 @@ export function FileSize(props: BundleFileSizeProps) { ); } -/** Format files or bundle size, from bytes to the nearest unit */ -export function formatByteSize(size: number) { - if (size < 1024) { - return size + 'B'; - } else if (size < 1024 * 1024) { - return (size / 1024).toFixed(1) + 'KB'; +/** + * Format files or bundle size, from bytes to the nearest unit. + * This uses the decimal system with a scaling factor of `1000`. + */ +export function formatByteSize(byteSize: number, scalingFactor = 1000) { + if (byteSize < scalingFactor) { + return byteSize + 'B'; + } else if (byteSize < scalingFactor * scalingFactor) { + return (byteSize / scalingFactor).toFixed(1) + 'KB'; } else { - return (size / 1024 / 1024).toFixed(1) + 'MB'; + return (byteSize / scalingFactor / scalingFactor).toFixed(1) + 'MB'; } }