Skip to content

Commit 8bb283b

Browse files
authored
feat(clerk-js): Add bundlewatch fix command (#6780)
1 parent bcf24f2 commit 8bb283b

File tree

4 files changed

+131
-23
lines changed

4 files changed

+131
-23
lines changed

.changeset/itchy-zebras-divide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { writeFile } from 'node:fs/promises';
2+
import bundlewatch from 'bundlewatch';
3+
import { minimatch } from 'minimatch';
4+
import { format } from 'prettier';
5+
import { $ } from 'zx';
6+
import bundlewatchConfig from './bundlewatch.config.json' with { type: 'json' };
7+
8+
const { fullResults } = await bundlewatch.default(bundlewatchConfig);
9+
const failedFiles = fullResults.filter(result => result.status === 'fail');
10+
11+
for (const file of failedFiles) {
12+
const matchingFileIndex = bundlewatchConfig.files.findIndex(f => {
13+
return minimatch(file.filePath, f.path);
14+
});
15+
if (matchingFileIndex !== -1) {
16+
// update maxSize to the file size plus 1KB, rounded up to the nearest KB
17+
bundlewatchConfig.files[matchingFileIndex].maxSize = `${Math.ceil((file.size + 1024) / 1024)}KB`;
18+
}
19+
}
20+
21+
const formattedConfig = await format(JSON.stringify(bundlewatchConfig), { parser: 'json' });
22+
await writeFile('./bundlewatch.config.json', formattedConfig);
23+
// print the git diff of the bundlewatch.config.json file
24+
const diff = await $`git -c color.ui=always --no-pager diff bundlewatch.config.json`.then(res => res.stdout);
25+
console.log(diff);
26+
console.log('Bundlewatch config updated!');

packages/clerk-js/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"build:declarations": "tsc -p tsconfig.declarations.json",
3939
"build:sandbox": "rspack build --config rspack.config.js --env production --env sandbox",
4040
"build:stats": "rspack build --config rspack.config.js --env production --json=stats.json --env variant=\"clerk.browser\"",
41-
"bundlewatch": "pnpm --package=bundlewatch -c dlx \"FORCE_COLOR=1 bundlewatch --config bundlewatch.config.json\"",
41+
"bundlewatch": "FORCE_COLOR=1 bundlewatch --config bundlewatch.config.json",
42+
"bundlewatch:fix": "node bundlewatch-fix.mjs",
4243
"clean": "rimraf ./dist",
4344
"dev": "rspack serve --config rspack.config.js",
4445
"dev:chips": "rspack serve --config rspack.config.js --env variant=\"clerk.chips.browser\"",
@@ -100,7 +101,9 @@
100101
"@types/cloudflare-turnstile": "^0.2.2",
101102
"@types/node": "^22.18.1",
102103
"@types/webpack-env": "^1.18.8",
104+
"bundlewatch": "^0.4.1",
103105
"jsdom": "^24.1.3",
106+
"minimatch": "^10.0.3",
104107
"webpack-merge": "^5.10.0"
105108
},
106109
"peerDependencies": {

0 commit comments

Comments
 (0)