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

fix: use derived storybookConfig as the merge base #740

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 0 additions & 4 deletions examples/showcase/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,5 @@ export default defineNuxtConfig({
},
},

vue: {
runtimeCompiler: true,
},

compatibilityDate: '2024-08-03',
})
57 changes: 33 additions & 24 deletions packages/storybook-addon/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ function mergeViteConfig(
}

extendedConfig.plugins = plugins

// Storybook adds 'vue' as dependency that should be optimized, but nuxt explicitly excludes it from pre-bundling
// Prioritize `optimizeDeps.exclude`. If same dep is in `include` and `exclude`, remove it from `include`
extendedConfig.optimizeDeps = extendedConfig.optimizeDeps || {}
Expand Down Expand Up @@ -303,42 +304,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
@@ -1,13 +1,20 @@
import type { StorybookConfig as StorybookConfigBase } from '@storybook/types'
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 @@ -17,8 +24,7 @@ type StorybookConfigFramework = {
| BuilderName
| {
name: BuilderName
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>
options?: BuilderOptions
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions playground/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ const config: StorybookConfig = {
name: '@storybook-vue/nuxt',
options: {},
},
docs: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autodocs: 'tag',
},
viteFinal: (config) => {
// For debugging purposes
// View intermediate state of Vite plugins at http://localhost:6006/__inspect
Expand Down
Loading