From c69845903316065dc5a524f6a773c4c36d1f314e Mon Sep 17 00:00:00 2001 From: Mike Odnis Date: Wed, 2 Oct 2024 18:31:52 -0400 Subject: [PATCH] chore: update workflows 8 --- package.json | 2 +- report-bundle-size.js | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index d6e7cfa..b1d46ef 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "scripts": { - "analyze": "cross-env ANALYZE=true next build", + "analyze": "cross-env ANALYZE=true next build && bun report-bundle-size.js", "analyze:browser": "cross-env BUNDLE_ANALYZE=browser next build", "analyze:server": "cross-env BUNDLE_ANALYZE=server next build", "build": "next build", diff --git a/report-bundle-size.js b/report-bundle-size.js index 8d0db1d..53f8c95 100644 --- a/report-bundle-size.js +++ b/report-bundle-size.js @@ -8,10 +8,14 @@ // edited to work with the appdir by @raphaelbadia // @ts-check -const gzipSize = require('gzip-size'); -const mkdirp = require('mkdirp'); -const fs = require('fs'); -const path = require('path'); +import { gzipSize } from 'gzip-size'; +import { mkdirp } from 'mkdirp'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); /** @typedef {{ raw: number, gzip: number }} ScriptSizes */ /** @typedef {Record} PageSizes */ @@ -95,7 +99,8 @@ const rawData = JSON.stringify({ // log outputs to the gh actions panel console.log(rawData); -mkdirp.sync(path.join(nextMetaRoot, 'analyze/')); +// Changed this line to use mkdirp directly +await mkdirp(path.join(nextMetaRoot, 'analyze/')); fs.writeFileSync(path.join(nextMetaRoot, 'analyze/__bundle_analysis.json'), rawData); // -------------- @@ -109,9 +114,9 @@ fs.writeFileSync(path.join(nextMetaRoot, 'analyze/__bundle_analysis.json'), rawD function getScriptSizes(scriptPaths) { return scriptPaths.reduce( (acc, scriptPath) => { - const [rawSize, gzipSize] = getScriptSize(scriptPath); + const [rawSize, gzSize] = getScriptSize(scriptPath); acc.raw += rawSize; - acc.gzip += gzipSize; + acc.gzip += gzSize; return acc; }, { raw: 0, gzip: 0 } @@ -133,9 +138,9 @@ function getScriptSize(scriptPath) { try { const textContent = fs.readFileSync(p, encoding); const rawSize = Buffer.byteLength(textContent, encoding); - const gzipSizeValue = gzipSize.sync(textContent); - memoryCache[p] = [rawSize, gzipSizeValue]; - return [rawSize, gzipSizeValue]; + const gzSize = gzipSize.sync(textContent); + memoryCache[p] = [rawSize, gzSize]; + return [rawSize, gzSize]; } catch (error) { console.error(`Error reading file: ${p}`, error); return [0, 0];