Skip to content

Commit

Permalink
fix: use derived storybookConfig as the merge base
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leckey <[email protected]>
  • Loading branch information
mehcode committed Aug 3, 2024
1 parent 0a78a27 commit f149e7a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
56 changes: 32 additions & 24 deletions packages/storybook-addon/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,42 +303,50 @@ export const previewAnnotations = async (
}

export const viteFinal: StorybookConfig['viteFinal'] = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: Record<string, any>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any,
config,
options,
) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getStorybookViteConfig = async (c: Record<string, any>, o: any) => {
const presetURL = pathToFileURL(
join(await getPackageDir('@storybook/vue3-vite'), 'preset.js'),
)
const { viteFinal: ViteFile } = await import(presetURL.href)
const { viteFinal: vueViteFinal } = await import(presetURL.href)

if (!vueViteFinal) {
throw new Error(
'unexpected contents in package @storybook/vue3-vite: viteFinal not found',
)
}

if (!ViteFile) throw new Error('ViteFile not found')
return ViteFile(c, o)
return (vueViteFinal as NonNullable<StorybookConfig['viteFinal']>)(c, o)
}

const storybookViteConfig = await getStorybookViteConfig(config, options)
const { viteConfig: nuxtConfig, nuxt } = await loadNuxtViteConfig(
storybookViteConfig.root,
)
const finalViteConfig = mergeViteConfig(config, nuxtConfig, nuxt)
// Write all vite configs to logs
const fs = await import('node:fs')
fs.mkdirSync(join(options.outputDir, 'logs'), { recursive: true })
console.debug(`Writing Vite configs to ${options.outputDir}/logs/...`)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-storybook.config.json'),
stringify(storybookViteConfig, { space: ' ' }),
)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-nuxt.config.json'),
stringify(nuxtConfig, { space: ' ' }),
)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-final.config.json'),
stringify(finalViteConfig, { space: ' ' }),
)

const finalViteConfig = mergeViteConfig(storybookViteConfig, nuxtConfig, nuxt)

if (options.outputDir != null) {
// Write all vite configs to logs
const fs = await import('node:fs')
fs.mkdirSync(join(options.outputDir, 'logs'), { recursive: true })
console.debug(`Writing Vite configs to ${options.outputDir}/logs/...`)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-storybook.config.json'),
stringify(storybookViteConfig, { space: ' ' }),
)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-nuxt.config.json'),
stringify(nuxtConfig, { space: ' ' }),
)
fs.writeFileSync(
join(options.outputDir, 'logs', 'vite-final.config.json'),
stringify(finalViteConfig, { space: ' ' }),
)
}

return finalViteConfig
}
Expand Down
12 changes: 9 additions & 3 deletions packages/storybook-addon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import type {
DecoratorFunction,
} from '@storybook/vue3'
import type { FrameworkOptions as FrameworkOptionsVue } from '@storybook/vue3-vite'
import type { StorybookConfigVite } from '@storybook/builder-vite'
import type {
StorybookConfigVite,
BuilderOptions as BuilderOptionsVite,
} from '@storybook/builder-vite'

declare let STORYBOOK_VUE_GLOBAL_PLUGINS: string[]
declare let STORYBOOK_VUE_GLOBAL_MIXINS: string[]

type FrameworkName = '@storybook-vue/nuxt'
type BuilderName = '@storybook/builder-vite'

type BuilderOptions = BuilderOptionsVite & {
outputDir?: string
}

type StorybookConfigFramework = {
framework:
| FrameworkName
Expand All @@ -25,8 +32,7 @@ type StorybookConfigFramework = {
| BuilderName
| {
name: BuilderName
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>
options?: BuilderOptions
}
}
}
Expand Down

0 comments on commit f149e7a

Please sign in to comment.