Skip to content

Commit

Permalink
feat(js): set the dist file size limits and run the check after the b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
LetItRock committed Jun 3, 2024
1 parent c5f7da4 commit 042aadf
Show file tree
Hide file tree
Showing 3 changed files with 543 additions and 125 deletions.
8 changes: 7 additions & 1 deletion packages/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@
"private": true,
"scripts": {
"start": "pnpm run build -- --watch --sourcemap",
"build": "tsup",
"build": "tsup && pnpm run post:build",
"post:build": "node scripts/size-limit.js",
"lint": "eslint --ext .ts,.tsx src",
"test": "jest"
},
"devDependencies": {
"@size-limit/esbuild": "^11.1.4",
"@size-limit/file": "^11.1.4",
"@types/jest": "^29.2.3",
"@types/node": "^18.11.12",
"bytes-iec": "^3.1.1",
"chalk": "^5.3.0",
"jest": "^29.3.1",
"size-limit": "^11.1.4",
"ts-jest": "^29.0.3",
"tsup": "^8.0.2",
"typescript": "4.9.5"
Expand Down
75 changes: 75 additions & 0 deletions packages/js/scripts/size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import path from 'path';
import sizeLimit from 'size-limit';
import filePlugin from '@size-limit/file';
import esbuildPlugin from '@size-limit/esbuild';
import bytes from "bytes-iec"
import chalk from "chalk"

const LIMIT = '10 kb';
const LIMIT_IN_BYTES = 10_000;
const baseDir = process.cwd();
const esmPath = path.resolve(baseDir, './dist/index.js');
const cjsPath = path.resolve(baseDir, './dist/index.cjs');
const umdPath = path.resolve(baseDir, './dist/novu.min.js');

const formatBytes = (size) => {
return bytes.format(size, { unitSeparator: " " })
}

const checks = [
{
name: 'ESM',
path: esmPath,
limit: LIMIT,
files: [esmPath],
sizeLimit: LIMIT_IN_BYTES,
},
{
name: 'CJS',
path: cjsPath,
limit: LIMIT,
files: [cjsPath],
sizeLimit: LIMIT_IN_BYTES,
},
{
name: 'UMD',
path: umdPath,
limit: LIMIT,
files: [umdPath],
sizeLimit: LIMIT_IN_BYTES,
},
];

const config = {
cwd: process.cwd(),
checks,
};

const calculateSizes = async () => {
console.log(chalk.gray("Checking the build dist files..."));

const results = await sizeLimit([filePlugin, esbuildPlugin], config);
if (config.failed) {
console.log(chalk.bold.red("\nThe build has reached the dist files size limits! 🚨\n"));

results.filter((_, index) => {
const check = checks[index]
const { passed } = check

return !passed;
}).forEach((result, index) => {
const check = checks[index]
const { size } = result
const { name } = check

console.log(chalk.yellow(`The ${name} file has failed the size limit.`));
console.log(chalk.yellow(`Current size is "${formatBytes(size)}" and the limit is "${check.limit}".\n`));
})

process.exit(1);
} else {
console.log(chalk.green("All good! 🙌"));
}
}

calculateSizes();
Loading

0 comments on commit 042aadf

Please sign in to comment.