Skip to content

Commit

Permalink
feat(builder): support emit stats
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed May 30, 2024
1 parent bca5f36 commit 9df7b05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
42 changes: 32 additions & 10 deletions packages/builder-rsbuild/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { Options } from '@storybook/types'
import { dirname, join, parse } from 'path'
import express from 'express'
import fs from 'fs-extra'
import {
NoStatsForViteDevError,
WebpackInvocationError,
} from '@storybook/core-events/server-errors'
import { WebpackInvocationError } from '@storybook/core-events/server-errors'
import type { RsbuildBuilder } from './types'
import rsbuildConfig, {
type RsbuildBuilderOptions,
Expand All @@ -19,6 +16,10 @@ export * from './types'
export * from './preview/virtual-module-mapping'

type RsbuildDevServer = Awaited<ReturnType<typeof createDevServer>>
type StatsOrMultiStats = Parameters<rsbuildReal.OnAfterBuildFn>[0]['stats']
export type Stats = NonNullable<
Exclude<StatsOrMultiStats, { stats: unknown[] }>
>

export const printDuration = (startTime: [number, number]) =>
prettyTime(process.hrtime(startTime))
Expand Down Expand Up @@ -98,6 +99,16 @@ export const start: RsbuildBuilder['start'] = async ({
})

const rsbuildServer = await rsbuildBuild.createDevServer()

const waitFirstCompileDone = new Promise<StatsOrMultiStats>((resolve) => {
rsbuildBuild.onDevCompileDone(({ stats, isFirstCompile }) => {
if (!isFirstCompile) {
return
}
resolve(stats)
})
})

server = rsbuildServer

if (!rsbuildBuild) {
Expand All @@ -117,19 +128,20 @@ export const start: RsbuildBuilder['start'] = async ({

router.use(rsbuildServer.middlewares)
storybookServer.on('upgrade', rsbuildServer.onHTTPUpgrade)
const stats = await waitFirstCompileDone

return {
bail,
stats: {
toJson: () => {
throw new NoStatsForViteDevError()
},
},
stats,
totalTime: process.hrtime(startTime),
}
}

export const build = async ({ options }: BuilderStartOptions) => {
// explicit type annotation to bypass TypeScript check
// see: https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189
export const build: ({
options,
}: BuilderStartOptions) => Promise<Stats> = async ({ options }) => {
const { createRsbuild } = await executor.get(options)
const config = await getConfig(options)
const rsbuildBuild = await createRsbuild({
Expand All @@ -140,6 +152,11 @@ export const build = async ({ options }: BuilderStartOptions) => {
const previewResolvedDir = getAbsolutePath('@storybook/preview')
const previewDirOrigin = join(previewResolvedDir, 'dist')
const previewDirTarget = join(options.outputDir || '', `sb-preview`)
let stats: Stats

rsbuildBuild.onAfterBuild((params) => {
stats = params.stats as Stats
})

const previewFiles = fs.copy(previewDirOrigin, previewDirTarget, {
filter: (src) => {
Expand All @@ -151,7 +168,12 @@ export const build = async ({ options }: BuilderStartOptions) => {
},
})

rsbuildBuild.onAfterBuild((params) => {
stats = params.stats as Stats
})

await Promise.all([rsbuildBuild.build(), previewFiles])
return stats!
}

export const corePresets = [join(__dirname, './preview-preset.js')]
Expand Down
6 changes: 6 additions & 0 deletions packages/builder-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Stats } from './index'
import type { RsbuildConfig } from '@rsbuild/core'
import type {
Builder,
Options,
TypescriptOptions as TypeScriptOptionsBase,
BuilderResult as BuilderResultBase,
} from '@storybook/types'
import type { PluginTypeCheckerOptions } from '@rsbuild/plugin-type-check'

Expand Down Expand Up @@ -38,3 +40,7 @@ export type BuilderOptions = {
*/
rsbuildConfigPath?: string
}

export interface BuilderResult extends BuilderResultBase {
stats?: Stats
}

0 comments on commit 9df7b05

Please sign in to comment.