Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(builder): support emit stats #20

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}