Skip to content

Commit

Permalink
fix: extend storybook config types from vue3-vite framework (#649)
Browse files Browse the repository at this point in the history
Allows for type-safe usage of the new `docgen` setting added in
Storybook v8 -
https://storybook.js.org/docs/8.0/get-started/vue3-vite#using-vue-component-meta

Fixes storybook-vue/storybook-nuxt#103.

---------

Signed-off-by: Ryan Leckey <ryan@calaxy.com>
Co-authored-by: Tobias Diez <code@tobiasdiez.de>
  • Loading branch information
mehcode and tobiasdiez authored Jun 16, 2024
1 parent c40a4dd commit a816e6e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ dist
build-storybook.log
storybook-static

# Nuxt build artificats
.nuxt

# Test artifacts
coverage
tsconfig.vitest-temp.json
1 change: 0 additions & 1 deletion packages/storybook-nuxt/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export const previewAnnotations: StorybookConfig['previewAnnotations'] = async (
return [...entry, resolve(packageDir, 'preview')]
}

// @ts-expect-error: viteFinal can be a function, but it's not typed as such
export const viteFinal: StorybookConfig['viteFinal'] = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: Record<string, any>,
Expand Down
42 changes: 23 additions & 19 deletions packages/storybook-nuxt/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
BuilderOptions,
StorybookConfig as StorybookConfigBase,
} from '@storybook/types'
import type { StorybookConfig as StorybookConfigBase } from '@storybook/types'
import type {
Preview,
StoryFn,
Expand All @@ -10,31 +7,38 @@ import type {
Meta,
DecoratorFunction,
} from '@storybook/vue3'
import type { FrameworkOptions as FrameworkOptionsVue } from '@storybook/vue3-vite'
import type { StorybookConfigVite } 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'

export type FrameworkOptions = NuxtOptions & {
builder?: BuilderOptions
}

type StorybookConfigFramework = {
framework: FrameworkName | { name: FrameworkName; options: FrameworkOptions }
core?: StorybookConfigBase['core'] & { builder?: BuilderName }
typescript?: StorybookConfigBase['typescript']
previewAnnotations?: StorybookConfigBase['previewAnnotations']
stories?: StorybookConfigBase['stories']
addons?: StorybookConfigBase['addons']
docs?: StorybookConfigBase['docs']
framework:
| FrameworkName
| { name: FrameworkName; options: FrameworkOptionsVue }
core?: Omit<StorybookConfigBase['core'], 'builder'> & {
builder?:
| BuilderName
| {
name: BuilderName
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>
}
}
}

/**
* The interface for Storybook configuration in `main.ts` files.
*/
export type StorybookConfig = {
viteFinal?: Record<string, unknown>
} & StorybookConfigFramework
export interface NuxtOptions {}
export type StorybookConfig = Omit<
StorybookConfigBase,
keyof StorybookConfigVite | keyof StorybookConfigFramework
> &
StorybookConfigVite &
StorybookConfigFramework

export { Meta, StoryFn, StoryObj, Preview, VueRenderer, DecoratorFunction }
50 changes: 50 additions & 0 deletions packages/storybook-nuxt/src/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expectTypeOf, test } from 'vitest'
import type { StorybookConfig } from './types'

describe('StorybookConfig', () => {
test('should restrict framework name', () => {
expectTypeOf<StorybookConfig>()
.toHaveProperty('framework')
.extract<string>()
.toEqualTypeOf<'@storybook-vue/nuxt'>()
expectTypeOf<StorybookConfig>()
.toHaveProperty('framework')
.extract<{ name: string }>()
.toHaveProperty('name')
.toEqualTypeOf<'@storybook-vue/nuxt'>()
})
test('should restrict builder name', () => {
expectTypeOf<StorybookConfig>()
.toHaveProperty('core')
.exclude<undefined>()
.toHaveProperty('builder')
.extract<string>()
.toEqualTypeOf<'@storybook/builder-vite'>()

expectTypeOf<StorybookConfig>()
.toHaveProperty('core')
.exclude<undefined>()
.toHaveProperty('builder')
.extract<{ name: string }>()
.toHaveProperty('name')
.toEqualTypeOf<'@storybook/builder-vite'>()
})
test('should allow setting docgen option', () => {
const _config: StorybookConfig = {
stories: [],
framework: {
name: '@storybook-vue/nuxt',
options: {
docgen: 'vue-component-meta',
},
},
}
})
test('should allow setting staticDirs', () => {
const _config: StorybookConfig = {
stories: [],
framework: '@storybook-vue/nuxt',
staticDirs: ['public'],
}
})
})
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
modules: ['../src/module'],
modules: ['../src/module', '@nuxt/test-utils/module'],
storybook: {
// Very verbose logs for debugging
logLevel: Number.POSITIVE_INFINITY,
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({
test: {
typecheck: {
enabled: true,
// Don't fail the test if typecheck of source files fails for the moment
ignoreSourceErrors: true,
},
},
})

0 comments on commit a816e6e

Please sign in to comment.