Skip to content

Commit

Permalink
Fix bundle analyzer script
Browse files Browse the repository at this point in the history
Signed-off-by: Gowtham Shanmugasundaram <[email protected]>
  • Loading branch information
GowthamShanmugam committed Oct 8, 2024
1 parent 6f69f2e commit 53ea858
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
'**/*.spec.tsx',
'./jest/setup.ts',
'./cypress/**',
'./analyzeTest.ts',
],
optionalDependencies: false,
peerDependencies: false,
Expand Down
15 changes: 8 additions & 7 deletions analyzeTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import { parseChunked } from '@discoveryjs/json-ext';

const pluginName = process.env.PLUGIN;
const MAX_ASSET_SIZE = 17; //17 MiB
Expand All @@ -12,17 +13,17 @@ const stringifyMiB = (value: ReturnType<typeof toMiB>): string =>

const getParsedStatFile = () => {
const filePath = getStatsFilePath();
const statsFile = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(statsFile);

return parseChunked(fs.createReadStream(filePath, { encoding: 'utf8' }));
};

type BundleDataMap = Record<string, number>;

// [Valid Bundles, Violating Bunldes]
type GetBundleInformation = () => [BundleDataMap, BundleDataMap];
type GetBundleInformation = () => Promise<[BundleDataMap, BundleDataMap]>;

const getBundleInformation: GetBundleInformation = () => {
const statsData = getParsedStatFile();
const getBundleInformation: GetBundleInformation = async () => {
const statsData = await getParsedStatFile();
const validAssets = {};
const violatingAssets = {};

Expand All @@ -39,8 +40,8 @@ const getBundleInformation: GetBundleInformation = () => {
return [validAssets, violatingAssets];
};

const validateBuild = () => {
const [validAssets, violatingAssets] = getBundleInformation();
const validateBuild = async () => {
const [validAssets, violatingAssets] = await getBundleInformation();
if (Object.keys(violatingAssets).length > 0) {
// eslint-disable-next-line no-console
console.error('Assets are larger than expected', violatingAssets);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
},
"devDependencies": {
"@cypress/webpack-preprocessor": "^5.9.1",
"@discoveryjs/json-ext": "0.6.1",
"@swc/core": "1.3.19",
"@swc/jest": "^0.2.23",
"@testing-library/jest-dom": "^6.4.5",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,11 @@
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==

"@discoveryjs/[email protected]":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.6.1.tgz#593da7a17a31a72a874e313677183334a49b01c9"
integrity sha512-boghen8F0Q8D+0/Q1/1r6DUEieUJ8w2a1gIknExMSHBsJFOr2+0KUfHiVYBvucPwl3+RU5PFBK833FjFCh3BhA==

"@discoveryjs/json-ext@^0.5.0":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.3.tgz#90420f9f9c6d3987f176a19a7d8e764271a2f55d"
Expand Down

0 comments on commit 53ea858

Please sign in to comment.