From 19a9695bca523997db42908535c6757dd4f5acae Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 28 Aug 2023 14:36:59 +0000 Subject: [PATCH 1/3] fix dist-stats command --- package.json | 2 +- resources/dist-stats.js | 75 ---------------------------------------- resources/dist-stats.mjs | 63 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 76 deletions(-) delete mode 100644 resources/dist-stats.js create mode 100644 resources/dist-stats.mjs diff --git a/package.json b/package.json index b66eaf482..db5b84bed 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/resources/dist-stats.js b/resources/dist-stats.js deleted file mode 100644 index 65fd5b981..000000000 --- a/resources/dist-stats.js +++ /dev/null @@ -1,75 +0,0 @@ -const { exec } = require('child_process'); -const { deflate } = require('zlib'); -const fs = require('fs'); - -require('colors'); - -const fileContent = filePath => - new Promise((resolve, reject) => - fs.readFile(filePath, (error, out) => - error ? reject(error) : resolve(out) - ) - ); - -const gitContent = gitPath => - new Promise(resolve => - exec(`git show ${gitPath}`, (error, out) => { - if (error) { - console.log( - `"git show ${gitPath}" failed, resulting in an empty diff.`.yellow - ); - resolve(''); - return; - } - resolve(out); - }) - ); - -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 pct = (s, b) => ` ${Math.floor(10000 * (1 - s / b)) / 100}%`.grey; - -Promise.all([ - fileContent('dist/immutable.js'), - gitContent('main:dist/immutable.js'), - fileContent('dist/immutable.min.js'), - gitContent('main:dist/immutable.min.js'), - fileContent('dist/immutable.min.js').then(deflateContent), - gitContent('main:dist/immutable.min.js').then(deflateContent), -]) - .then(results => results.map(result => Buffer.byteLength(result, 'utf8'))) - .then(results => results.map(result => parseInt(result, 10))) - .then(([rawNew, rawOld, minNew, minOld, zipNew, zipOld]) => { - console.log( - ` Raw: ${space(14, bytes(rawNew).cyan)} ${space( - 15, - diff(rawNew, rawOld) - )}` - ); - console.log( - ` Min: ${space(14, bytes(minNew).cyan)}${pct(minNew, rawNew)}${space( - 15, - diff(minNew, minOld) - )}` - ); - console.log( - ` Zip: ${space(14, bytes(zipNew).cyan)}${pct(zipNew, rawNew)}${space( - 15, - diff(zipNew, zipOld) - )}` - ); - }); diff --git a/resources/dist-stats.mjs b/resources/dist-stats.mjs new file mode 100644 index 000000000..36f64a4c4 --- /dev/null +++ b/resources/dist-stats.mjs @@ -0,0 +1,63 @@ +import fs from 'node:fs/promises'; +import { deflate } from 'zlib'; +import 'colors'; + +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@4' + ).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))}` + ); + }); From 958a5f28f777a5560637537f707cc5a3a70bcf08 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 28 Aug 2023 14:50:33 +0000 Subject: [PATCH 2/3] set version as constant --- resources/dist-stats.mjs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/resources/dist-stats.mjs b/resources/dist-stats.mjs index 36f64a4c4..986e533df 100644 --- a/resources/dist-stats.mjs +++ b/resources/dist-stats.mjs @@ -2,6 +2,8 @@ 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))) @@ -25,7 +27,7 @@ let bundlephobaInfoCache; async function bundlephobaInfo(key) { if (!bundlephobaInfoCache) { bundlephobaInfoCache = await fetch( - 'https://bundlephobia.com/api/size?package=immutable@4' + `https://bundlephobia.com/api/size?package=immutable@${VERIFY_AGAINST_VERSION}` ).then(res => res.json()); } From aeeb684ae22bc8f1fb68283cade78b04fcb0c6ca Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 28 Aug 2023 15:27:58 +0000 Subject: [PATCH 3/3] upgrade CI to node 18 --- .github/workflows/ci.yml | 14 +++++++------- .github/workflows/release.yml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2754fbfe..9f8ad1d06 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 @@ -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 @@ -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: @@ -96,7 +96,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-node@v2 with: - node-version: '16' + node-version: '18' - uses: actions/cache@v3 with: path: ~/.npm @@ -104,7 +104,7 @@ jobs: 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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a3f0a02a5..666e497bd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: