feat: show fs tree for output

This commit is contained in:
Pooya Parsa 2020-11-14 21:41:38 +01:00
parent 609796a944
commit 6875d5535b
3 changed files with 42 additions and 9 deletions

View File

@ -1,13 +1,11 @@
import { resolve } from 'path'
import consola from 'consola'
import { rollup, OutputOptions } from 'rollup'
import { rollup } from 'rollup'
import Hookable from 'hookable'
import prettyBytes from 'pretty-bytes'
import gzipSize from 'gzip-size'
import chalk from 'chalk'
import { readFile, emptyDir } from 'fs-extra'
import { printFSTree } from './utils/tree'
import { getRollupConfig } from './rollup/config'
import { hl, prettyPath, serializeTemplate, writeFile } from './utils'
import { hl, serializeTemplate, writeFile } from './utils'
import { SLSOptions } from './config'
export async function build (options: SLSOptions) {
@ -30,16 +28,17 @@ export async function build (options: SLSOptions) {
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
options.rollupConfig = getRollupConfig(options)
await hooks.callHook('rollup:before', options)
const build = await rollup(options.rollupConfig).catch((error) => {
error.message = '[serverless] Rollup Error: ' + error.message
throw error
})
const { output } = await build.write(options.rollupConfig.output as OutputOptions)
const size = prettyBytes(output[0].code.length)
const zSize = prettyBytes(await gzipSize(output[0].code))
consola.success('Generated bundle in', prettyPath(options.targetDir), chalk.gray(`(Size: ${size} Gzip: ${zSize})`))
await build.write(options.rollupConfig.output)
await printFSTree(options.targetDir)
await hooks.callHook('done', options)

View File

@ -0,0 +1,34 @@
import { resolve, extname, basename, dirname, relative } from 'path'
import globby from 'globby'
import prettyBytes from 'pretty-bytes'
import gzipSize from 'gzip-size'
import { readFile } from 'fs-extra'
import chalk from 'chalk'
export async function printFSTree (dir) {
const files = await globby('**/*.js', { cwd: dir })
const items = (await Promise.all(files.map(async (file) => {
const path = resolve(dir, file)
const src = await readFile(path)
const size = src.byteLength
const gzip = await gzipSize(src)
return { file, path, size, gzip }
}))).sort((a, b) => b.size - a.size)
let totalSize = 0
let totalGzip = 0
for (const item of items) {
let dir = dirname(item.file)
if (dir === '.') { dir = '' }
const name = basename(item.file, extname(item.file))
const rpath = relative(process.cwd(), item.path)
process.stdout.write(chalk.gray(`[${dir ? `${dir}/` : ''}${name}] at ${rpath} (${prettyBytes(item.size)}) (${prettyBytes(item.gzip)} gzip)\n`))
totalSize += item.size
totalGzip += item.gzip
}
process.stdout.write(`${chalk.cyan('λ Total size:')} ${prettyBytes(totalSize)} (${prettyBytes(totalGzip)} gzip)\n`)
}