Skip to content

Commit 4405a91

Browse files
committed
chore: fix memory leak
1 parent f2db245 commit 4405a91

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

packages/blade/scripts/getBundleSize.js

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const traverse = require('@babel/traverse').default;
55
const execa = require('execa');
66

77
const main = () => {
8-
const sizeLimitConfig = [];
9-
108
// Get all the components name exported from the bundle and add them to the size-limit configuration
119
const fileContent = fs.readFileSync(
1210
path.resolve(__dirname, '../build/lib/web/production/components/index.js'),
@@ -18,71 +16,74 @@ const main = () => {
1816
sourceType: 'module',
1917
});
2018

21-
const componentNameList = [];
19+
const sizes = [];
20+
const excludedComponents = [
21+
'useTheme',
22+
'BladeProvider',
23+
'announce',
24+
'clearAnnouncer',
25+
'destroyAnnouncer',
26+
'getTextProps',
27+
'screenReaderStyles',
28+
'useActionListContext',
29+
];
2230

2331
// Traverse the AST to get the component names
2432
traverse(ast, {
2533
// Get the component name from the export statement
2634
ExportSpecifier: ({ node }) => {
2735
const componentName = node.exported.name;
2836
// We don't want to add Icon components to the size-limit configuration
29-
// We don't want to add the same component name twice, so we'll check if it's already added
30-
if (!(componentName.includes('Icon') || componentNameList.includes(componentName))) {
31-
componentNameList.push(componentName);
32-
sizeLimitConfig.push({
33-
name: componentName,
34-
path: './build/lib/web/production/components/index.js',
35-
import: `{ ${componentName} }`,
36-
// Set high limit for the component size so that it doesn't fail the size-limit check
37-
limit: '200 kb',
38-
running: false,
39-
gzip: true,
40-
});
37+
if (!(componentName.includes('Icon') || excludedComponents.includes(componentName))) {
38+
console.log('🚀 Analyzing: ', componentName);
39+
40+
fs.writeFileSync(
41+
path.resolve(__dirname, '../.size-limit.json'),
42+
JSON.stringify(
43+
[
44+
{
45+
name: componentName,
46+
path: './build/lib/web/production/components/index.js',
47+
import: `{ ${componentName} }`,
48+
// Set high limit for the component size so that it doesn't fail the size-limit check
49+
limit: '200 kb',
50+
running: false,
51+
gzip: true,
52+
},
53+
],
54+
null,
55+
2,
56+
),
57+
);
58+
59+
const { stdout } = execa.commandSync('yarn size-limit --json');
60+
61+
const jsonLikeString = stdout
62+
.split('\n') // remove new line chars => []
63+
.map((item) => item.trim()) // remove whitespace
64+
.filter((item) => item !== '') // filter empty array items
65+
.join('');
66+
67+
sizes.push(
68+
JSON.parse(
69+
jsonLikeString.substring(jsonLikeString.indexOf('[') + 1, jsonLikeString.indexOf(']')),
70+
),
71+
);
4172
}
4273
},
4374
});
4475

45-
// All components import
46-
sizeLimitConfig.unshift({
47-
name: '*',
48-
path: './build/lib/web/production/components/index.js',
49-
import: '*',
50-
limit: '200 kb',
51-
running: false,
52-
gzip: true,
53-
});
54-
55-
fs.writeFileSync(
56-
path.resolve(__dirname, '../.size-limit.json'),
57-
JSON.stringify(sizeLimitConfig, null, 2),
76+
const story = fs.readFileSync(
77+
path.resolve(__dirname, '../docs/utils/bundleSizeReport.stories.mdx'),
78+
'utf-8',
5879
);
5980

60-
// Run the size-limit command
61-
try {
62-
const { stdout } = execa.commandSync('yarn size-limit --json');
63-
64-
const jsonLikeString = stdout
65-
.split('\n') // remove new line chars => []
66-
.map((item) => item.trim()) // remove whitespace
67-
.filter((item) => item !== '') // filter empty array items
68-
.join('');
81+
const updatedStory = story.replace(/BUNDLE_SIZE_DATA/g, JSON.stringify(sizes));
6982

70-
const sizes = JSON.parse(
71-
jsonLikeString.substring(jsonLikeString.indexOf('['), jsonLikeString.indexOf(']') + 1),
72-
);
73-
74-
const story = fs.readFileSync(
75-
path.resolve(__dirname, '../docs/utils/bundleSizeReport.stories.mdx'),
76-
'utf-8',
77-
);
78-
const updatedStory = story.replace(/BUNDLE_SIZE_DATA/g, JSON.stringify(sizes));
79-
fs.writeFileSync(
80-
path.resolve(__dirname, '../docs/utils/bundleSizeReport.stories.mdx'),
81-
updatedStory,
82-
);
83-
} catch (error) {
84-
throw new Error(error);
85-
}
83+
fs.writeFileSync(
84+
path.resolve(__dirname, '../docs/utils/bundleSizeReport.stories.mdx'),
85+
updatedStory,
86+
);
8687
};
8788

8889
main();

0 commit comments

Comments
 (0)