Skip to content

Commit 8df8629

Browse files
committed
fix: throw if no #build
1 parent 11651c8 commit 8df8629

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

.storybook/main.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,33 @@ const config = {
5454
},
5555
load(id) {
5656
if (id !== '\0virtual:#components') return
57-
if (!buildDir) return 'export {}'
57+
if (!buildDir) {
58+
throw new Error('[storybook-nuxt-components] Could not resolve the `#build` alias.')
59+
}
5860
const dtsPath = resolve(buildDir, 'components.d.ts')
5961
// Wire the generated declaration file into Vite's file-watch graph so
6062
// that the virtual module is invalidated when Nuxt regenerates it.
6163
this.addWatchFile(dtsPath)
62-
try {
63-
const dts = readFileSync(dtsPath, 'utf-8')
64-
const lines: string[] = []
65-
// Match only the direct `typeof import("…").default` form.
66-
// Lazy/island wrappers (LazyComponent<T>, IslandComponent<T>) are
67-
// excluded intentionally — Storybook only needs the concrete type.
68-
// The format has been stable across all Nuxt 3 releases; if it ever
69-
// changes, the exports array will simply be empty and Storybook will
70-
// fall back to direct imports from `~/components`.
71-
const re = /^export const (\w+): typeof import\("([^"]+)"\)\.default$/gm
72-
let match: RegExpExecArray | null
73-
while ((match = re.exec(dts)) !== null) {
74-
const [, name, rel] = match
75-
if (!name || !rel) continue
76-
const abs = resolve(buildDir, rel).replaceAll('\\', '/')
77-
lines.push(`export { default as ${name} } from ${JSON.stringify(abs)}`)
78-
}
79-
return lines.join('\n') || 'export {}'
80-
} catch (err) {
81-
// oxlint-disable-next-line no-console -- Log and swallow errors to avoid breaking the Storybook build when components.d.ts is missing or malformed.
82-
console.warn(
83-
'[storybook-nuxt-components] Failed to build #components virtual module:',
84-
err,
64+
const dts = readFileSync(dtsPath, 'utf-8')
65+
const lines: string[] = []
66+
// Match only the direct `typeof import("…").default` form.
67+
// Lazy/island wrappers (LazyComponent<T>, IslandComponent<T>) are
68+
// excluded intentionally — Storybook only needs the concrete type.
69+
// The format has been stable across all Nuxt 3 releases.
70+
const re = /^export const (\w+): typeof import\("([^"]+)"\)\.default$/gm
71+
let match: RegExpExecArray | null
72+
while ((match = re.exec(dts)) !== null) {
73+
const [, name, rel] = match
74+
if (!name || !rel) continue
75+
const abs = resolve(buildDir, rel).replaceAll('\\', '/')
76+
lines.push(`export { default as ${name} } from ${JSON.stringify(abs)}`)
77+
}
78+
if (lines.length === 0) {
79+
throw new Error(
80+
`[storybook-nuxt-components] No component exports were found in ${dtsPath}.`,
8581
)
86-
return 'export {}'
8782
}
83+
return lines.join('\n')
8884
},
8985
})
9086

0 commit comments

Comments
 (0)