You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Svelte 5 and SvelteKit, there's an error thrown for any story that uses a +page.svelte route as the meta component:
ReferenceError: Page is not defined
at http://localhost:6007/src/routes/+page.svelte:33:27
This happens because the Svelte Docgen plugin calculates the component name wrong, based on Svelte 4's component name algorithm, which has changed slightly in Svelte 5, especially for filenames with special characters like +.
If we add the Vite Inspect plugin to the reproduction, we'll see that the .__docgen property is added to the wrong function name:
The current (broken) algorithm for calculating the component name is here:
If you combine that whole flow into a single function it would naively be something like this:
functionget_component_name(filename){constparts=filename.split(/[/\\]/);constbasename=/** @type {string} */(parts.pop());constlast_dir=/** @type {string} */(parts.at(-1));letname=basename.replace('.svelte','');if(name==='index'&&last_dir&&last_dir!=='src'){name=last_dir;}name=name[0].toUpperCase()+name.slice(1);// 👇 this is from generate()returnname.replace(/[^a-zA-Z0-9_$]/g,'_').replace(/^[0-9]/,'_');}
That last line (from generate) replaces special characters like + with _. So it turns src/routes/+page.svelte into _page, while we turn it into Page.
We probably want a condition on the Svelte version, that uses the new algorithm when Svelte 5 is detected. In the perfect world we'd not try to generate the component name, but instead find it from the default export, either via a smart regex or AST parsing.
The text was updated successfully, but these errors were encountered:
Describe the bug
When using Svelte 5 and SvelteKit, there's an error thrown for any story that uses a
+page.svelte
route as the metacomponent
:This happens because the Svelte Docgen plugin calculates the component name wrong, based on Svelte 4's component name algorithm, which has changed slightly in Svelte 5, especially for filenames with special characters like
+
.If we add the Vite Inspect plugin to the reproduction, we'll see that the
.__docgen
property is added to the wrong function name:The current (broken) algorithm for calculating the component name is here:
storybook/code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts
Lines 41 to 71 in 7e43ecd
But what Svelte 5 does is slightly different:
get_component_name
https://github.com/sveltejs/svelte/blob/396ea2ef370e7ea5b5d4571c4e5e14384bac3ca6/packages/svelte/src/compiler/phases/2-analyze/index.js#L208-L217
Which is passed through
module.scope.generate
:https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/compiler/phases/2-analyze/index.js#L399
And that
generate
does:https://github.com/sveltejs/svelte/blob/396ea2ef370e7ea5b5d4571c4e5e14384bac3ca6/packages/svelte/src/compiler/phases/scope.js#L129-L150
If you combine that whole flow into a single function it would naively be something like this:
That last line (from
generate
) replaces special characters like+
with_
. So it turnssrc/routes/+page.svelte
into_page
, while we turn it intoPage
.Reproduction link
https://github.com/simonhackler/Storybook-reproduction
Reproduction steps
System
Additional context
Originally reported here: storybookjs/addon-svelte-csf#231 (reply in thread)
We probably want a condition on the Svelte version, that uses the new algorithm when Svelte 5 is detected. In the perfect world we'd not try to generate the component name, but instead find it from the default export, either via a smart regex or AST parsing.
The text was updated successfully, but these errors were encountered: