Skip to content

Commit 74df3cb

Browse files
committed
chore: migrate to official storybook nuxt module
Fixes #1917 and fixes #2227.
1 parent 174a65c commit 74df3cb

File tree

5 files changed

+2233
-1101
lines changed

5 files changed

+2233
-1101
lines changed

.storybook/main.ts

+2-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { buildNuxt, loadNuxt, tryUseNuxt } from '@nuxt/kit'
2-
import type { StorybookConfig } from '@storybook/vue3-vite'
3-
import { mergeConfig } from 'vite'
1+
import type { StorybookConfig } from '@storybook-vue/nuxt'
42

53
const config: StorybookConfig = {
64
// Need to specify stories as workaround for https://github.com/storybookjs/storybook/issues/20761
@@ -9,81 +7,10 @@ const config: StorybookConfig = {
97
disableTelemetry: true,
108
},
119
framework: {
12-
name: '@storybook/vue3-vite',
10+
name: '@storybook-vue/nuxt',
1311
options: {},
1412
},
1513
addons: ['storybook-vue-addon'],
16-
async viteFinal(config) {
17-
const nuxtViteConfig = (await startNuxtAndGetViteConfig()).viteConfig
18-
// Need to remove storybook-vue-addon since it will be inserted by the storybook-vue-addon plugin
19-
// TODO: Would be better if we would check there that the plugin is not already added
20-
config.plugins = config.plugins.filter((plugin) => {
21-
if (
22-
plugin !== null &&
23-
typeof plugin === 'object' &&
24-
'name' in plugin &&
25-
plugin.name === 'storybook-vue-addon'
26-
) {
27-
return false
28-
}
29-
return true
30-
})
31-
return mergeConfig(
32-
{
33-
resolve: nuxtViteConfig.resolve,
34-
optimizeDeps: nuxtViteConfig.optimizeDeps,
35-
plugins: nuxtViteConfig.plugins,
36-
define: nuxtViteConfig.define,
37-
},
38-
config,
39-
)
40-
},
41-
}
42-
43-
// https://github.com/nuxt/nuxt/issues/14534
44-
// From: https://github.com/danielroe/nuxt-vitest/blob/main/packages/nuxt-vitest/src/config.ts
45-
async function startNuxtAndGetViteConfig(rootDir = process.cwd()) {
46-
let nuxt = tryUseNuxt()
47-
let nuxtAlreadyRunnnig = true
48-
if (!nuxt) {
49-
nuxtAlreadyRunnnig = false
50-
nuxt = await loadNuxt({
51-
cwd: rootDir,
52-
dev: false,
53-
overrides: {
54-
ssr: false,
55-
app: {
56-
rootId: 'nuxt-test',
57-
},
58-
},
59-
})
60-
}
61-
62-
const promise = new Promise<{ nuxt; viteConfig }>((resolve, reject) => {
63-
nuxt.hook('vite:extendConfig', (viteConfig, { isClient }) => {
64-
if (isClient) {
65-
resolve({ nuxt, viteConfig })
66-
if (!nuxtAlreadyRunnnig) {
67-
throw new Error('_stop_')
68-
}
69-
}
70-
})
71-
72-
// TODO: Need better handling if Nuxt is already running
73-
// we don't really want to build nuxt again,
74-
// or at least shutdown the second build after vite:extendConfig is called
75-
buildNuxt(nuxt).catch((err) => {
76-
if (!err.toString().includes('_stop_')) {
77-
reject(err)
78-
}
79-
})
80-
}).finally(() => {
81-
if (!nuxtAlreadyRunnnig) {
82-
nuxt.close()
83-
}
84-
})
85-
86-
return promise
8714
}
8815

8916
export default config

modules/storybook.ts

-109
This file was deleted.

nuxt.config.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ export default defineNuxtConfig({
2828
},
2929
// Prevent 'reflect-metadata' and 'json-bigint-patch' from being treeshaked (since we don't explicitly use the import it would otherwise be removed)
3030
moduleSideEffects: ['reflect-metadata', 'json-bigint-patch'],
31-
prerender: {
32-
// Needed for storybook support (otherwise the file is not created during nuxi generate)
33-
routes: ['/_storybook/external-iframe'],
34-
},
3531
esbuild: {
3632
options: {
3733
tsconfigRaw: {
@@ -49,11 +45,6 @@ export default defineNuxtConfig({
4945
typedPages: true,
5046
},
5147

52-
vue: {
53-
// Add support for vue runtime compiler (needed to render stories in storybook)
54-
runtimeCompiler: true,
55-
},
56-
5748
// Workaround for https://github.com/nuxt/nuxt/issues/22933
5849
hooks: {
5950
close: (nuxt) => {
@@ -143,6 +134,9 @@ export default defineNuxtConfig({
143134
// Add support for writing content in markdown
144135
// https://content.nuxtjs.org/
145136
'@nuxt/content',
137+
// Add support for storybook
138+
// https://storybook.nuxtjs.org/
139+
'@nuxtjs/storybook',
146140
// Add support for native vue stories
147141
// https://github.com/tobiasdiez/storybook-vue-addon
148142
'storybook-vue-addon/nuxt',

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"prisma:studio": "prisma studio",
2424
"prisma:generate": "prisma generate",
2525
"prisma:generate:watch": "prisma generate --watch",
26-
"storybook": "storybook dev -p 6006 --preview-url=http://localhost:3000/_storybook/external-iframe --no-manager-cache",
26+
"storybook": "storybook dev -p 6006",
2727
"storybook:build": "storybook build",
2828
"storybook:publish": "chromatic --exit-zero-on-changes --build-script-name storybook:build",
2929
"graphql:generate": "graphql-codegen-esm",
@@ -70,6 +70,7 @@
7070
"nodemailer": "^6.8.0",
7171
"pinia": "^2.1.6",
7272
"reflect-metadata": "^0.1.13",
73+
"storybook": "^7.4.5",
7374
"ts-loader": "^9.4.4",
7475
"ts-node": "^10.9.1",
7576
"tsyringe": "^4.8.0",
@@ -94,10 +95,9 @@
9495
"@nuxt/devtools": "^0.8.3",
9596
"@nuxtjs/eslint-config": "^12.0.0",
9697
"@nuxtjs/eslint-config-typescript": "^12.1.0",
98+
"@nuxtjs/storybook": "^7.0.0",
9799
"@nuxtjs/tailwindcss": "^6.8.0",
98-
"@storybook/core-server": "^7.0.26",
99-
"@storybook/vue3": "^7.0.26",
100-
"@storybook/vue3-vite": "7.0.26",
100+
"@storybook-vue/nuxt": "^0.1.4",
101101
"@tailwindcss/forms": "^0.5.6",
102102
"@tailwindcss/line-clamp": "^0.4.4",
103103
"@tailwindcss/typography": "^0.5.10",
@@ -138,7 +138,6 @@
138138
"prettier": "^3.0.3",
139139
"prettier-plugin-organize-imports": "^3.2.3",
140140
"prisma": "^5.3.1",
141-
"storybook": "7.0.26",
142141
"storybook-vue-addon": "^0.4.0",
143142
"supertest": "^6.3.3",
144143
"supertest-graphql": "^1.1.4",

0 commit comments

Comments
 (0)