From 0787d68289229f8ea2b3ff3ba6d4d3e80f80dcbf Mon Sep 17 00:00:00 2001 From: Nitin Date: Mon, 5 Feb 2024 10:28:07 +0530 Subject: [PATCH] chore: add comments --- packages/blade/scripts/generateBundleDiff.js | 12 ++++++++++++ packages/blade/scripts/generateBundleSizeInfo.js | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/blade/scripts/generateBundleDiff.js b/packages/blade/scripts/generateBundleDiff.js index 21269385155..2e2bfba24cc 100644 --- a/packages/blade/scripts/generateBundleDiff.js +++ b/packages/blade/scripts/generateBundleDiff.js @@ -1,4 +1,6 @@ +// Asynchronous function to generate the bundle size difference report const generateBundleDiff = async () => { + // Array to store the base bundle size statistics let baseBundleSizeStats = []; // Get the base bundle size report from the master branch const baseBundleStatsURL = @@ -6,14 +8,18 @@ const generateBundleDiff = async () => { 'https://raw.githubusercontent.com/razorpay/blade/master/packages/blade/baseBundleSizeStats.json'; const response = await fetch(baseBundleStatsURL); + // Parse the JSON response if the request is successful if (response.status === 200) { baseBundleSizeStats = await response.json(); } + // Import the current bundle size statistics from the PR // eslint-disable-next-line import/extensions const currentBundleSizeStats = require('../PRBundleSizeStats.json'); + // Initialize the bundle difference array with current bundle stats let bundleDiff = currentBundleSizeStats; + // Filter the components that don't have the same size in the base and current bundle if (baseBundleSizeStats.length > 0) { bundleDiff = baseBundleSizeStats.filter( ({ size: baseSize }) => @@ -21,25 +27,30 @@ const generateBundleDiff = async () => { ); } + // If there is no difference, return null if (bundleDiff.length === 0) { return { diffTable: null }; } + // Calculate the size differences and create a formatted diff table bundleDiff.forEach((component) => { const currentComponent = currentBundleSizeStats.find((stat) => stat.name === component.name); const baseComponent = baseBundleSizeStats.find((stat) => stat.name === component.name); if (baseComponent && !currentComponent) { + // Component removed in the PR component.diffSize = -baseComponent.size / 1000; component.baseSize = baseComponent.size / 1000; component.prSize = 0; component.isSizeIncreased = false; } else if (!baseComponent && currentComponent) { + // Component added in the PR component.diffSize = currentComponent.size / 1000; component.baseSize = 0; component.prSize = currentComponent.size / 1000; component.isSizeIncreased = true; } else { + // Component size changed in the PR component.diffSize = (currentComponent.size - baseComponent.size) / 1000; component.baseSize = baseComponent.size / 1000; component.prSize = currentComponent.size / 1000; @@ -47,6 +58,7 @@ const generateBundleDiff = async () => { } }); + // Generate a Markdown table for the bundle size differences const diffTable = ` | Status | Component | Base Size (kb) | Current Size (kb) | Diff | | --- | --- | --- | --- | --- | diff --git a/packages/blade/scripts/generateBundleSizeInfo.js b/packages/blade/scripts/generateBundleSizeInfo.js index 1626370c488..d783c22c1fa 100644 --- a/packages/blade/scripts/generateBundleSizeInfo.js +++ b/packages/blade/scripts/generateBundleSizeInfo.js @@ -11,11 +11,12 @@ const main = () => { 'utf8', ); - // Parse the file content to get the AST + // Parse the file content to generate an AST const ast = babelParser.parse(fileContent, { sourceType: 'module', }); + // Arrays to store size-limit stats and component names to exclude const sizes = []; const excludedComponents = [ 'useTheme', @@ -47,6 +48,7 @@ const main = () => { const componentName = node.exported.name; // We don't want to add Icon components to the size-limit configuration if (!(componentName.includes('Icon') || excludedComponents.includes(componentName))) { + // Write size-limit configuration to .size-limit.json for each component fs.writeFileSync( path.resolve(__dirname, '../.size-limit.json'), JSON.stringify( @@ -66,8 +68,10 @@ const main = () => { ), ); + // Run size-limit command and capture the output to gather size information const { stdout } = execa.commandSync('yarn size-limit --json'); + // Process the size-limit output to extract relevant information const jsonLikeString = stdout .split('\n') // remove new line chars => [] .map((item) => item.trim()) // remove whitespace @@ -83,6 +87,7 @@ const main = () => { }, }); + // Write the gathered size information to the specified file const filename = process.env.BUNDLE_SIZE_STATS_FILENAME || 'PRBundleSizeStats.json'; fs.writeFileSync(path.resolve(__dirname, `../${filename}`), JSON.stringify(sizes)); };