From a5b49a9b7bae95cfe4daca5cb758d2043d297e81 Mon Sep 17 00:00:00 2001 From: Dany Castillo <31006608+dcastil@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:15:22 +0200 Subject: [PATCH] create entry point config dynamically and report single export sizes of all ESM bundles --- .../metrics-report/src/get-package-size.mjs | 82 +++++++++++-------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/.github/actions/metrics-report/src/get-package-size.mjs b/.github/actions/metrics-report/src/get-package-size.mjs index b2a08e2..e8f5bd6 100644 --- a/.github/actions/metrics-report/src/get-package-size.mjs +++ b/.github/actions/metrics-report/src/get-package-size.mjs @@ -12,34 +12,6 @@ import { rollup } from 'rollup' import { actionRootPath, repoRootPath } from './utils/path.mjs' -/** - * @typedef {object} EntryPointConfiguration - * @property {string} path - * @property {'esm' | 'cjs'} format - * @property {boolean=} shouldMeasureIndividualExports - */ - -/** @type {EntryPointConfiguration[]} */ -const entryPointConfigs = [ - { - path: 'dist/bundle-mjs.mjs', - format: 'esm', - shouldMeasureIndividualExports: true, - }, - { - path: 'dist/bundle-cjs.js', - format: 'cjs', - }, - { - path: 'dist/es5/bundle-mjs.mjs', - format: 'esm', - }, - { - path: 'dist/es5/bundle-cjs.js', - format: 'cjs', - }, -] - /** * @typedef {object} GetPackageSizeOptions * @property {boolean=} shouldOmitFailures @@ -73,17 +45,20 @@ async function buildPackage() { * @returns {Promise} */ async function getEntryPointSizes({ shouldOmitFailures }) { + core.info('Getting entry point configs') + const entryPointConfigs = await getEntryPointConfigs() + core.info('Getting bundle sizes') const maybeEntryPointSizes = await Promise.all( entryPointConfigs.map(async (entryPointConfig, entryPointIndex) => { - const entryPointBundlePath = path.resolve(repoRootPath, entryPointConfig.path) + const entryPointBundlePath = path.resolve(repoRootPath, entryPointConfig.bundlePath) const bundle = await getBundle(entryPointConfig, entryPointBundlePath).catch( (error) => { if (shouldOmitFailures) { core.info( - `Failed to get bundle for ${entryPointConfig.path}: ${error.message}`, + `Failed to get bundle for ${entryPointConfig.entryPointPath}: ${error.message}`, ) return } @@ -97,7 +72,7 @@ async function getEntryPointSizes({ shouldOmitFailures }) { } const [bundleSize, singleExportSizes] = await Promise.all([ - getBundleSize(entryPointConfig.path, bundle), + getBundleSize(entryPointConfig.entryPointPath, bundle), getSingleExportBundleSizes( entryPointConfig, entryPointIndex, @@ -119,6 +94,45 @@ async function getEntryPointSizes({ shouldOmitFailures }) { return entryPointSizes } +/** + * @typedef {object} EntryPointConfiguration + * @property {string} entryPointPath + * @property {string} bundlePath + * @property {'esm' | 'cjs'} format + */ + +/** + * @returns {Promise} + */ +async function getEntryPointConfigs() { + const pkg = await import('../../../../package.json', { assert: { type: 'json' } }) + + return Object.entries(pkg.exports).flatMap(([relativeEntryPointPath, bundleObject]) => { + const entryPointPath = path.resolve('tailwind-merge', relativeEntryPointPath) + + /** @type {EntryPointConfiguration[]} */ + const entryPointConfigs = [] + + if (bundleObject.import) { + entryPointConfigs.push({ + entryPointPath, + bundlePath: bundleObject.import, + format: 'esm', + }) + } + + if (bundleObject.require) { + entryPointConfigs.push({ + entryPointPath, + bundlePath: bundleObject.require, + format: 'cjs', + }) + } + + return entryPointConfigs + }) +} + /** * @param {EntryPointConfiguration} entryPointConfig * @param {string} entryPoint @@ -154,11 +168,7 @@ async function getBundle(entryPointConfig, entryPoint) { * @param {import('rollup').OutputChunk} bundle */ async function getSingleExportBundleSizes(entryPointConfig, entryPointIndex, bundlePath, bundle) { - if ( - entryPointConfig.shouldMeasureIndividualExports && - entryPointConfig.format === 'esm' && - bundle.exports.length !== 0 - ) { + if (entryPointConfig.format === 'esm' && bundle.exports.length !== 0) { const singleExportBundlesDirPath = path.resolve( actionRootPath, `temp/bundle-${entryPointIndex}`,