Skip to content

Commit

Permalink
feat(build): result logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Mar 15, 2022
1 parent 2f60fd8 commit 0648d58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/histoire/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { join } from 'pathe'
import { build as viteBuild, createServer as createViteServer } from 'vite'
import fs from 'fs-extra'
import { lookup as lookupMime } from 'mrmime'
import pc from 'picocolors'
import { performance } from 'perf_hooks'
import { APP_PATH } from './alias.js'
import { Context } from './context.js'
import { createVitePlugins } from './plugin.js'
Expand All @@ -10,6 +12,7 @@ import type { RollupOutput } from 'rollup'
import { useCollectStories } from './collect.js'

export async function build (ctx: Context) {
const startTime = performance.now()
await findAllStories(ctx)

// Collect story data
Expand All @@ -27,6 +30,10 @@ export async function build (ctx: Context) {
await server.close()
await destroyCollectStories()

const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0)
const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0)
const emptyStoryCount = ctx.storyFiles.length - storyCount

const results = await viteBuild({
plugins: await createVitePlugins(ctx),
build: {
Expand All @@ -52,6 +59,12 @@ export async function build (ctx: Context) {
// Sandbox
const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk')
await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', ctx)

const duration = performance.now() - startTime
if (emptyStoryCount) {
console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`))
}
console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`))
}

async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileName: string, ctx: Context) {
Expand Down
4 changes: 2 additions & 2 deletions packages/histoire/src/node/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
const storyData: Story[] = []
await run(storyFile, storyData, el)
if (storyData.length === 0) {
console.warn(pc.yellow(`No story found for ${storyFile.path}`))
console.warn(pc.yellow(`⚠️ No story found for ${storyFile.path}`))
return
} else if (storyData.length > 1) {
console.warn(pc.yellow(`Multiple stories not supported: ${storyFile.path}`))
console.warn(pc.yellow(`⚠️ Multiple stories not supported: ${storyFile.path}`))
}
storyFile.story = storyData[0]
const file: TreeFile = {
Expand Down

0 comments on commit 0648d58

Please sign in to comment.