Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dist-stats command #1964

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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))}`
);
});