Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Feb 5, 2024
1 parent 0635f48 commit 0787d68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/blade/scripts/generateBundleDiff.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,64 @@
// 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 =
process.env.BASE_BUNDLE_SIZE_STATS_URL ||
'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 }) =>
!currentBundleSizeStats.some(({ size: currentSize }) => currentSize === baseSize),
);
}

// 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;
component.isSizeIncreased = component.diffSize > 0;
}
});

// Generate a Markdown table for the bundle size differences
const diffTable = `
| Status | Component | Base Size (kb) | Current Size (kb) | Diff |
| --- | --- | --- | --- | --- |
Expand Down
7 changes: 6 additions & 1 deletion packages/blade/scripts/generateBundleSizeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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));
};
Expand Down

0 comments on commit 0787d68

Please sign in to comment.