From 283c8e74c905b99d8cb73e0d4bc1b8f874d8bada Mon Sep 17 00:00:00 2001 From: Johann Schopplich Date: Mon, 18 Jul 2022 13:36:05 +0200 Subject: [PATCH] feat: pad output asset file info --- src/node/index.ts | 24 +++++++++++++++++++++--- src/node/utils.ts | 10 +++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/node/index.ts b/src/node/index.ts index d7bb682..05ae2af 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -1,4 +1,5 @@ import { existsSync } from 'node:fs' +import { readFile } from 'node:fs/promises' import { basename, dirname, resolve } from 'pathe' import { build as _build, mergeConfig } from 'vite' import vuePlugin from '@vitejs/plugin-vue2' @@ -11,7 +12,7 @@ import postcssDirPseudoClass from 'postcss-dir-pseudo-class' import consola from 'consola' import { debounce } from 'perfect-debounce' import colors from 'picocolors' -import type { OutputChunk, RollupOutput } from 'rollup' +import type { RollupOutput } from 'rollup' import type { InlineConfig } from 'vite' import { name, version } from '../../package.json' import { PrettyError, handleError } from './errors' @@ -85,8 +86,25 @@ async function generate(options: ResolvedCliOptions) { if (result && !options.watch) { const { output } = toArray(result as RollupOutput)[0] - for (const { fileName, type, code } of output as OutputChunk[]) - printFileInfo(options.cwd, outDir, fileName, type, code) + + let longest = 0 + for (const file in output) { + const l = output[file].fileName.length + if (l > longest) + longest = l + } + + // @ts-expect-error: `code` not available in `OutputAsset` + for (const { fileName, type, code } of output) { + printFileInfo( + options.cwd, + outDir, + fileName, + code ?? await readFile(resolve(outDir, fileName), 'utf8'), + type, + longest, + ) + } } return result diff --git a/src/node/utils.ts b/src/node/utils.ts index 869251c..f8ee150 100644 --- a/src/node/utils.ts +++ b/src/node/utils.ts @@ -1,4 +1,3 @@ -import { readFile } from 'fs/promises' import { gzip } from 'zlib' import { promisify } from 'util' import { normalize, relative, resolve } from 'pathe' @@ -23,17 +22,18 @@ export async function printFileInfo( root: string, outDir: string, filePath: string, + content: string, type: string, - content?: string, + maxLength: number, ) { - content ??= await readFile(resolve(outDir, filePath), 'utf8') const prettyOutDir = `${normalize(relative(root, resolve(root, outDir)))}/` const kibs = content.length / 1024 const compressedSize = await getCompressedSize(content) const writeColor = type === 'chunk' ? colors.cyan : colors.magenta consola.log( - `${colors.white(colors.dim(prettyOutDir)) + writeColor(filePath)} ${ - colors.dim(`${kibs.toFixed(2)} KiB${compressedSize}`)}`, + colors.white(colors.dim(prettyOutDir)) + + writeColor(filePath.padEnd(maxLength + 2)) + + colors.dim(`${kibs.toFixed(2)} KiB${compressedSize}`), ) }