-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
preset.ts
383 lines (350 loc) · 11.9 KB
/
preset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { createRequire } from 'node:module'
import { resolve, normalize } from 'pathe'
import { resolvePath } from 'mlly'
import type { PresetProperty, PreviewAnnotation } from '@storybook/types'
import {
type UserConfig as ViteConfig,
mergeConfig,
searchForWorkspaceRoot,
} from 'vite'
import type { Nuxt } from '@nuxt/schema'
import vuePlugin from '@vitejs/plugin-vue'
import replace from '@rollup/plugin-replace'
import type { StorybookConfig } from './types'
import { componentsDir, composablesDir, pluginsDir, runtimeDir } from './dirs'
import stringify from 'json-stable-stringify'
import nuxtRuntimeConfigPlugin from './runtimeConfig'
const packageDir = resolve(fileURLToPath(import.meta.url), '../..')
const distDir = resolve(fileURLToPath(import.meta.url), '../..', 'dist')
const dirs = [distDir, packageDir, pluginsDir, componentsDir]
/**
* extend nuxt-link component to use storybook router
* @param nuxt
*/
function extendComponents(nuxt: Nuxt) {
nuxt.hook('components:extend', (components) => {
const nuxtLink = components.find(
({ pascalName }) => pascalName === 'NuxtLink',
)
if (!nuxtLink) {
throw new Error('NuxtLink component not found')
}
nuxtLink.filePath = join(runtimeDir, 'components/nuxt-link')
nuxtLink.shortPath = join(runtimeDir, 'components/nuxt-link')
nuxt.options.build.transpile.push(nuxtLink.filePath)
})
}
/**
* extend composables to override router ( fix undefined router useNuxtApp )
*
* @param nuxt
*/
async function extendComposables(nuxt: Nuxt) {
const { addImportsSources } = await import('@nuxt/kit')
nuxt.options.build.transpile.push(composablesDir)
addImportsSources({
imports: ['useRouter'],
from: join(composablesDir, 'router'),
})
}
async function loadNuxtViteConfig(root: string | undefined) {
const { loadNuxt, tryUseNuxt, buildNuxt, extendPages } = await import(
'@nuxt/kit'
)
let nuxt = tryUseNuxt()
if (nuxt) {
// Nuxt is already started in the current process (i.e. in dev mode)
// We assume that we are called from the Nuxt module, which means that
// Nuxt is in the "load module" state and we can access the Vite config later via the hook
const nuxtRes = nuxt
return new Promise<{ viteConfig: ViteConfig; nuxt: Nuxt }>((resolve) => {
nuxtRes.hook('vite:configResolved', (config, { isClient }) => {
if (isClient) {
resolve({
viteConfig: config,
nuxt: nuxtRes,
})
}
})
})
}
nuxt = await loadNuxt({
cwd: root,
ready: false,
dev: false,
overrides: {
appId: 'nuxt-app',
buildId: 'storybook',
ssr: false,
},
})
if ((nuxt.options.builder as string) !== '@nuxt/vite-builder')
throw new Error(
`Storybook-Nuxt does not support '${nuxt.options.builder}' for now.`,
)
nuxt.options.build.transpile.push(join(packageDir, 'preview'))
nuxt.hook('modules:done', () => {
extendComposables(nuxt)
// Override nuxt-link component to use storybook router
extendComponents(nuxt)
// nuxt.options.build.transpile.push('@storybook-vue/nuxt')
// Add iframe page
extendPages((pages) => {
pages.push({
name: 'storybook-iframe',
path: '/iframe.html',
})
})
})
// Get Vite config from Nuxt
// https://nuxt.com/docs/api/kit/examples#accessing-nuxt-vite-config
await nuxt.ready()
return new Promise<{ viteConfig: ViteConfig; nuxt: Nuxt }>(
(resolve, reject) => {
nuxt.hook('vite:configResolved', (config, { isClient }) => {
if (isClient) {
resolve({
viteConfig: config,
nuxt,
})
// Stop the build process, as we don't need to build the Nuxt app
throw new Error('_stop_')
}
})
buildNuxt(nuxt).catch((err) => {
if (!err.toString().includes('_stop_')) {
reject(err)
}
})
},
).finally(() => nuxt.close())
}
function mergeViteConfig(
storybookConfig: ViteConfig,
nuxtConfig: ViteConfig,
nuxt: Nuxt,
): ViteConfig {
const extendedConfig: ViteConfig = mergeConfig(nuxtConfig, storybookConfig)
const plugins = extendedConfig.plugins || []
// Find the index of the plugin with name 'vite:vue'
const index = plugins.findIndex(
(plugin) => plugin && 'name' in plugin && plugin.name === 'vite:vue',
)
// Check if the plugin was found
if (index !== -1) {
// Replace the plugin with the new one using vuePlugin()
plugins[index] = vuePlugin()
} else {
// Vue plugin should be the first registered user plugin so that it will be added directly after Vite's core plugins
// and transforms global vue components before nuxt:components:imports.
plugins.unshift(vuePlugin())
}
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 || {}
extendedConfig.optimizeDeps.include =
extendedConfig.optimizeDeps.include || []
extendedConfig.optimizeDeps.include =
extendedConfig.optimizeDeps.include.filter(
(dep) => !extendedConfig.optimizeDeps?.exclude?.includes(dep),
)
// Add lodash/kebabCase, since it is still a cjs module
// Imported in https://github.com/storybookjs/storybook/blob/480359d5e340d97476131781c69b4b5e3b724f57/code/renderers/vue3/src/docs/sourceDecorator.ts#L18
extendedConfig.optimizeDeps.include.push(
'@nuxtjs/storybook > @storybook-vue/nuxt > @storybook/vue3 > lodash/kebabCase',
)
return mergeConfig(extendedConfig, {
// build: { rollupOptions: { external: ['vue', 'vue-demi'] } },
define: {
'import.meta.client': 'true',
},
plugins: [
replace({
values: {
'import.meta.server': 'false',
'import.meta.client': 'true',
},
preventAssignment: true,
}),
nuxtRuntimeConfigPlugin(nuxt.options.runtimeConfig),
],
server: {
cors: true,
proxy: {
...getPreviewProxy(),
...getNuxtProxyConfig(nuxt).proxy,
},
fs: { allow: [searchForWorkspaceRoot(process.cwd()), ...dirs] },
},
envPrefix: ['NUXT_'],
})
}
export const core: PresetProperty<'core', StorybookConfig> = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any,
) => {
return {
...config,
builder: await getPackageDir('@storybook/builder-vite'),
renderer: await getPackageDir('@storybook/vue3'),
}
}
export interface Resolver {
/**
* Resolves the given path segments to an absolute path, using the provided base path.
*
* The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory.
*
* @param path A sequence of paths or path segments.
* @throws {TypeError} if any of the arguments is not a string.
*/
resolve(...path: string[]): string
/**
* Asynchronously resolves a module path to a local file path, using the provided base path.
*
* @param id - The identifier or path of the module to resolve.
* @returns A promise to resolve to the file path, or `null` if the module could not be resolved.
*/
resolveModule(
id: string,
options?: { paths?: string[] },
): Promise<string | null>
}
/**
* Creates a resolver that can resolve paths and modules relative to a base path.
*
* @example
* ```js
* const resolver = createResolver(import.meta.url)
* const path = resolver.resolve('preview')
* const modulePath = await resolver.resolveModule('module-name')
* ```
*
* @param base - The base path to resolve paths and modules relative to.
* @returns A resolver object.
*/
function createResolver(base: string | URL): Resolver {
if (!base) {
throw new Error('`base` argument is missing for createResolver(base)!')
}
base = base.toString()
if (base.startsWith('file://')) {
base = dirname(fileURLToPath(base))
}
return {
resolve: (...path) => resolve(base, ...path),
async resolveModule(id, options) {
const paths = options?.paths ?? [base]
paths.concat([base as string])
return await resolvePath(id, { url: paths }).catch(() => null)
},
}
}
/**
* This is needed to correctly load the `preview.js` file,
* see https://github.com/storybookjs/storybook/blob/main/docs/contribute/framework.md#4-author-the-framework-itself
*/
export const previewAnnotations = async (
entry: PreviewAnnotation[] = [],
): Promise<PreviewAnnotation[]> => {
const resolver = createResolver(import.meta.url)
// Problem: Storybook does not correctly resolve some modules to an absolute path to the correct deep path in node_modules.
// Solution:
// We need to make sure that they are resolved as dependencies of this package, since they are not installed in the root.
// We need to use bare here otherwise storybook will strip the absolute path, leading to a wrong import
// https://github.com/storybookjs/storybook/blob/3590a1cade2fe24608b3ce0246d5d58692c89883/code/builders/builder-vite/src/utils/process-preview-annotation.ts#L30-L35
return [
...entry.map((entry) => {
// Handle @storybook/vue3
if (typeof entry === 'string' && entry.includes('vue3')) {
return {
bare: normalize(entry),
absolute: '',
}
} else {
return entry
}
}),
{
bare: resolver.resolve('preview'),
absolute: '',
},
]
}
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,
) => {
// 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)
if (!ViteFile) throw new Error('ViteFile not found')
return ViteFile(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: ' ' }),
)
return finalViteConfig
}
async function getPackageDir(packageName: string) {
try {
const require = createRequire(import.meta.url)
return dirname(require.resolve(join(packageName, 'package.json')))
} catch (e) {
throw new Error(`Cannot find ${packageName}`, { cause: e })
}
}
export function getNuxtProxyConfig(nuxt: Nuxt) {
const port = nuxt.options.runtimeConfig.app.port ?? 3000
const route = '^/(_nuxt|_ipx|_icon|__nuxt_devtools__)'
const proxy = {
[route]: {
target: `http://localhost:${port}`,
changeOrigin: true,
secure: false,
ws: true,
},
}
return {
port,
route,
proxy,
}
}
function getPreviewProxy() {
return {
'/__storybook_preview__': {
target: '/',
changeOrigin: false,
secure: false,
rewrite: (path: string) => path.replace('/__storybook_preview__', ''),
ws: true,
},
}
}