Skip to content

Commit 8cb3eb1

Browse files
authored
Add bundle-size script (#455)
1 parent 9e77668 commit 8cb3eb1

File tree

3 files changed

+222
-12
lines changed

3 files changed

+222
-12
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"scripts": {
2424
"test": "jest",
2525
"lint": "eslint .",
26-
"prepare": "husky install"
26+
"prepare": "husky install",
27+
"bundle-size": "node scripts/bundle-size"
2728
},
2829
"author": "Nicolas Charpentier <[email protected]>",
2930
"license": "MIT",
@@ -39,9 +40,11 @@
3940
"jest": "29.5.0",
4041
"lint-staged": "13.2.3",
4142
"metro-react-native-babel-preset": "0.76.7",
43+
"nanoid": "3.3.6",
4244
"prettier": "2.8.8",
4345
"react": "18.2.0",
4446
"react-native": "0.72.1",
47+
"react-native-bundle-scale": "1.1.0",
4548
"typescript": "5.1.6"
4649
},
4750
"peerDependencies": {

scripts/bundle-size.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/node
2+
const util = require('node:util');
3+
const exec = util.promisify(require('node:child_process').exec);
4+
const {spawn} = require('node:child_process');
5+
const fs = require('node:fs');
6+
const {nanoid} = require('nanoid');
7+
8+
const {name, version} = require('../package.json');
9+
10+
/**
11+
* We can't simply provide the generated tarball to `react-native-bundle-scale`, because
12+
* Yarn will cache it based on the name of the tarball, and subsequent runs of this script
13+
* will always result in the same output even though the content may have changed.
14+
*
15+
* That's why we need to rename the tarball with something unique.
16+
*/
17+
(async () => {
18+
await exec('npm pack');
19+
20+
const tarballName = `${name}-${nanoid()}`;
21+
await exec(`mv ${name}-${version}.tgz ${tarballName}.tgz`);
22+
23+
const childProcess = spawn(
24+
'npx',
25+
[
26+
'react-native-bundle-scale',
27+
JSON.stringify({
28+
'react-native-url-polyfill': `file:${__dirname}/../${tarballName}.tgz`,
29+
}),
30+
'--packages-as-json',
31+
'--debug',
32+
],
33+
{stdio: 'inherit'},
34+
);
35+
36+
childProcess.on('close', () => {
37+
fs.unlinkSync(`${tarballName}.tgz`);
38+
});
39+
})();

0 commit comments

Comments
 (0)