Skip to content

Commit

Permalink
fix dist-stats command (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Aug 28, 2023
1 parent 2e95835 commit 372d376
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 84 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
- uses: actions/cache@v3
with:
path: ~/.npm
Expand All @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
- uses: actions/cache@v3
with:
path: ~/.npm
Expand All @@ -55,7 +55,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
- uses: actions/cache@v3
with:
path: ~/.npm
Expand All @@ -75,14 +75,14 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.OS }}-node-
- run: npm ci
- run: npm run website:build
- run: NODE_OPTIONS=--openssl-legacy-provider npm run website:build
- run: npm run check-git-clean

publish:
Expand All @@ -96,15 +96,15 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.OS }}-node-
- run: npm ci
- run: npm run build
- run: npm run website:build
- run: NODE_OPTIONS=--openssl-legacy-provider npm run website:build
- name: Push NPM Branch
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'
registry-url: 'https://registry.npmjs.org'
- uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -71,7 +71,7 @@
"build:esm": "rollup -c ./resources/rollup-config-es.js",
"build:copy": "cpy ./type-definitions/immutable.* dist",
"build:prepare": "./resources/prepare-dist.sh",
"build:stats": "node ./resources/dist-stats.js",
"build:stats": "node ./resources/dist-stats.mjs",
"unit-test": "jest",
"website:build": "cd website && next build && next-sitemap && next export",
"website:dev": "cd website && next dev",
Expand Down
75 changes: 0 additions & 75 deletions resources/dist-stats.js

This file was deleted.

65 changes: 65 additions & 0 deletions resources/dist-stats.mjs
@@ -0,0 +1,65 @@
import fs from 'node:fs/promises';
import { deflate } from 'zlib';
import 'colors';

const VERIFY_AGAINST_VERSION = '4';

const deflateContent = content =>
new Promise((resolve, reject) =>
deflate(content, (error, out) => (error ? reject(error) : resolve(out)))
);

const space = (n, s) =>
new Array(Math.max(0, 10 + n - (s || '').length)).join(' ') + (s || '');

const bytes = b =>
`${b.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')} bytes`;

const diff = (n, o) => {
const d = n - o;
return d === 0 ? '' : d < 0 ? ` ${bytes(d)}`.green : ` +${bytes(d)}`.red;
};

const percentage = (s, b) => ` ${Math.floor(10000 * (1 - s / b)) / 100}%`.grey;

let bundlephobaInfoCache;

async function bundlephobaInfo(key) {
if (!bundlephobaInfoCache) {
bundlephobaInfoCache = await fetch(
`https://bundlephobia.com/api/size?package=immutable@${VERIFY_AGAINST_VERSION}`
).then(res => res.json());
}

return bundlephobaInfoCache[key];
}

Promise.all([
fs.readFile('dist/immutable.js'),
fs.readFile('dist/immutable.min.js'),
bundlephobaInfo('size'),
fs.readFile('dist/immutable.min.js').then(deflateContent),
bundlephobaInfo('gzip'),
])
.then(results =>
results.map(result =>
typeof result === 'number'
? result
: Number(Buffer.byteLength(result, 'utf8'))
)
)
.then(([rawNew, minNew, minOld, zipNew, zipOld]) => {
console.log(` Raw: ${space(14, bytes(rawNew).cyan)}`);
console.log(
` Min: ${space(14, bytes(minNew).cyan)}${percentage(
minNew,
rawNew
)}${space(15, diff(minNew, minOld))}`
);
console.log(
` Zip: ${space(14, bytes(zipNew).cyan)}${percentage(
zipNew,
rawNew
)}${space(15, diff(zipNew, zipOld))}`
);
});

0 comments on commit 372d376

Please sign in to comment.