Skip to content

Commit

Permalink
feat: pad output asset file info
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jul 18, 2022
1 parent 8395471 commit 283c8e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFile } from 'fs/promises'
import { gzip } from 'zlib'
import { promisify } from 'util'
import { normalize, relative, resolve } from 'pathe'
Expand All @@ -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}`),
)
}

0 comments on commit 283c8e7

Please sign in to comment.