From ac0b28e6081eb388bbb7f63e1828d2bc511d7c87 Mon Sep 17 00:00:00 2001 From: Aaron Klinker Date: Tue, 11 Jul 2023 17:18:22 -0500 Subject: [PATCH] fix: Cleanup build summary with sourcemaps --- src/core/log/printBuildSummary.ts | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/core/log/printBuildSummary.ts b/src/core/log/printBuildSummary.ts index b2c877cf..e738c89f 100644 --- a/src/core/log/printBuildSummary.ts +++ b/src/core/log/printBuildSummary.ts @@ -1,4 +1,4 @@ -import path, { extname, relative, resolve } from 'path'; +import path, { relative, resolve } from 'path'; import { BuildOutput, InternalConfig } from '../types'; import { printTable } from './printTable'; import pc from 'picocolors'; @@ -13,14 +13,8 @@ export async function printBuildSummary( ...output.steps.flatMap((step) => step.chunks), ...output.publicAssets, ].sort((l, r) => { - const lWeight = - CHUNK_SORT_WEIGHTS[l.fileName] ?? - CHUNK_SORT_WEIGHTS[extname(l.fileName)] ?? - DEFAULT_SORT_WEIGHT; - const rWeight = - CHUNK_SORT_WEIGHTS[r.fileName] ?? - CHUNK_SORT_WEIGHTS[extname(r.fileName)] ?? - DEFAULT_SORT_WEIGHT; + const lWeight = getChunkSortWeight(l.fileName); + const rWeight = getChunkSortWeight(r.fileName); const diff = lWeight - rWeight; if (diff !== 0) return diff; return l.fileName.localeCompare(r.fileName); @@ -34,9 +28,8 @@ export async function printBuildSummary( relative(process.cwd(), config.outDir) + path.sep, chunk.fileName, ]; - const ext = extname(chunk.fileName); const prefix = i === chunks.length - 1 ? ' └─' : ' ├─'; - const color = CHUNK_COLORS[ext] ?? DEFAULT_COLOR; + const color = getChunkColor(chunk.fileName); const stats = await fs.lstat(resolve(config.outDir, chunk.fileName)); totalSize += stats.size; const size = String(filesize(stats.size)); @@ -58,13 +51,29 @@ const DEFAULT_SORT_WEIGHT = 100; const CHUNK_SORT_WEIGHTS: Record = { 'manifest.json': 0, '.html': 1, + '.js.map': 2, '.js': 2, '.css': 3, }; +function getChunkSortWeight(filename: string) { + return ( + Object.entries(CHUNK_SORT_WEIGHTS).find(([key, value]) => { + if (filename.endsWith(key)) return value; + })?.[1] ?? DEFAULT_SORT_WEIGHT + ); +} const DEFAULT_COLOR = pc.blue; const CHUNK_COLORS: Record string> = { + '.js.map': pc.gray, '.html': pc.green, '.css': pc.magenta, '.js': pc.cyan, }; +function getChunkColor(filename: string) { + return ( + Object.entries(CHUNK_COLORS).find(([key, value]) => { + if (filename.endsWith(key)) return value; + })?.[1] ?? DEFAULT_COLOR + ); +}