-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svelte: Improve argTypes inference with svelte2tsx
- support runes
#28492
Conversation
88733ed
to
700862a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
- Updated
code/frameworks/svelte-vite/package.json
to addsvelte2tsx
and updatetypescript
dependency. - Modified
code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts
to switch fromsveltedoc-parser
tosvelte2tsx
and TypeScript. - Added
code/frameworks/svelte-vite/src/plugins/ts2doc.ts
to convert Svelte files to TypeScript documentation. - Introduced
code/frameworks/sveltekit/template/stories_svelte-kit-prerelease-ts/ButtonTypeScriptRunes.svelte
for Svelte 5 'runes mode' with TypeScript support. - Added
code/frameworks/sveltekit/template/stories_svelte-kit-prerelease-ts/ts-runes-docs.stories.js
for Storybook story ofButtonTypeScriptRunes
component.
5 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings
.../frameworks/sveltekit/template/stories_svelte-kit-prerelease-ts/ButtonTypeScriptRunes.svelte
Outdated
Show resolved
Hide resolved
700862a
to
9d05996
Compare
9d05996
to
c7a619d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very interesting work! 🎉
As you can see from my comments, I'm eager to know if we can just switch completely to using svelte2tsx
for all cases and not just in runes mode? Even for Svelte 4 and 5?
I haven't been able to dive into the details of the node visiting code yet.
I also would like to see the test stories test out more cases:
- a prop being typed by an imported type
- another component that doesn't have
lang="ts"
but instead only uses JSDoc comments only.
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 08d3f4f. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
f1a2912
to
a4f4a6a
Compare
Hi. Another problem with the approach is that the |
95d5665
to
43bc7d5
Compare
Thanks for your valuable input! ❤️ Then I think we should just keep typescript in, with the widest possible version range.
@shilman @kasperpeulen feel free to argue against me here, given we are very actively working on reducing Storybook's dependency graph where possible. @jasonluy given you're a |
@jasonlyu123, thank you so much! After rewriting to use @babel/parser, I realized that
I just realized this myself a moment ago :)
AFAIK, |
Compared to |
I checked to make sure we're doing the right thing in |
Sorry. I was confused by the Yarn used by Storybook. Yarn does not automatically install peerDependencies. However, if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not quite familiar with storybook so I didn't dive deep into the integration. But I can give some general suggestions with TypeScript and svelte2tsx
API you can explore.
} | ||
let propsRuneUsed = false; | ||
|
||
const options = loadCompilerOptions(targetFileName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse tsconfig also scans the file system for the files included in the tsconfig include
config. It's returned in the ParsedCommandLine.fileNames
. It might be quite heavy. It might be worth watching the file and cache the result.
The createProgram
is also heavy so might be better in a larger codebase to reuse the problem. You can probably try creating the program with ParsedCommandLine.fileNames
but maybe only the svelte files since the ts/js file might never be used. When the targetFileName
is not in the list or file content changes then recreate the program. It's also worth exploring passing the old program to createProgram
and TypeScript will try to reuse the last program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your suggestions! I added some caching mechanisms:
- If the previously loaded
ParsedCommandLine.fileNames
includes the target .svelte file, it doesn't reload the tsconfig. - Passing the previous program as
oldProgram
to createProgram. Subsequent calls to createProgram take less than 10ms for the Storybooksveltekit/prerelease-ts
example on my machine.
I believe these measures ensure adequate performance. Watching for file changes seems difficult for me and might be too heavy to implement in this PR. What do you think? @jasonlyu123 @JReinhold
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reference the file watching here.
https://github.com/vitejs/vite/blob/8d0a9c1ab8ddd26973509ca230b29604e872e2cd/packages/vite/src/node/plugins/esbuild.ts#L110
One thing I am also curious about is that sometimes the user starts the dev server when svelte-kit hasn't generated the ./svelte-kit/tsconfig.json
file. Does that affect the amount of svelte files being loaded? or maybe the plugin will be loaded after the svelte-kit plugin so it's fine? If it does, maybe the file watch is necessary.
ca36a55
to
38a16a5
Compare
6654d3c
to
7e2d0f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Added a few minor comments, and there's still some left from previous reviews.
stories look great!
code/frameworks/sveltekit/template/stories_svelte-kit-prerelease-ts/DocsTSRunes2.svelte
Outdated
Show resolved
Hide resolved
code/frameworks/sveltekit/template/stories_svelte-kit-prerelease-ts/DocsJSDocRunes.svelte
Outdated
Show resolved
Hide resolved
@JReinhold Sorry for the delay. I have reflected your review! |
973702d
to
7d5d81e
Compare
7d5d81e
to
08d3f4f
Compare
If |
Unfortunately that would be a breaking change because sveltedoc-parser is slightly better when it comes to slots, see #28492 (comment) I don't know how many actually use that feature (if any), but I have a hard time justifying dropping the support in a minor, even though I want to. We should definitely remove it in 9.0 though. |
I wonder if we need the
I expect that this is perhaps only possible with Svelte 5. Either way, this is probably an improvement as it is, but if we could remove the need for those dependencies as well longer-term certainly that would be nice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's actually a regression, see the Chromatic snapshot for argtypes inference:
https://www.chromatic.com/test?appId=6308736456ad2046275c0ae7&id=66dbf220a2751a07f5d4fdc2
It's not easy to spot, but for objects, it changes slightly from:
"style": {
"control": {
"type": "object"
},
"name": "style",
"description": "Styles to use for this component",
"type": {
"required": false,
"name": "object" 👈 correct
},
"table": {
"type": {
"summary": "object"
},
"defaultValue": {},
"category": "properties"
}
},
to:
"style": {
"control": {
"type": "object"
},
"name": "style",
"description": "Styles to use for this component",
"type": {
"required": false,
"name": "{}" 👈 incorrect
},
"table": {
"type": {
"summary": "{}"
},
"defaultValue": {
"summary": "{}"
},
"category": "properties"
}
},
The rest of the changes looks good, they just appear to be more correct than what we had before, but that name
change is wrong because the Storybook API expects name: 'object'
:
https://storybook.js.org/docs/api/arg-types#type
I think we can just special case it here:
With something like
name: item.type?.text === '{}' ? 'object' : item.type?.text as SBScalarType['name'],
If you give me edit access @ciscorn I can help get this PR over the finish line, I'd love to merge it in the coming days. 🙏
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork
I've fixed the issue reported above and fixed merge conflicts locally, now I just need permission to push to the branch. 🙏 The feature freeze for 8.4.0 goes into effect on Wednesday, Oct 23, so we need to get it in before then. |
GitHub UI is a bit weird here, this PR didn't actually get "merged", but the other PR #29423 that I created included all the commits from here, and that got merged, which confuses GitHub slightly. |
Failed to publish canary version of this pull request, triggered by @JReinhold. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/11980658402 |
What I did
To support Svelte 5 'runes mode', this PR modifies @storybook/svelte-vite to utilize Svelte’s official svelte2tsx +
typescript
instead of sveltedoc-parser for generating props documentation data.I found that others have mentioned the same idea as well:
Rationale:
sveltedoc-parser
cannot parse Svelte 5 ‘runes mode’ files at all, and it has not been maintained for 3 years.svelte2tsx
is an official component used by Svelte in its language server.typescript
, it provides perfect type resolution and can also handle type hints written in JavaScript + JSDoc as well as TypeScript.export let
style.Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
svelte-kit/skeleton-ts
) and Svelte 5 (svelte-kit/prerelease-ts
) sandboxes (feel free to use Chromatic)/?path=/docs/stories-renderers-svelte-docs-ts--docs
/?path=/docs/stories-renderers-svelte-docs-jsdoc--docs
/?path=/docs/stories-renderers-svelte-docs-ts-inline-prop-types--docs
/?path=/docs/stories-renderers-svelte-docs-ts-referenced-prop-types--docs
/?path=/docs/stories-renderers-svelte-docs-jsdoc-runes--docs
example screenshot:
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli/src/sandbox-templates.ts
Make sure this PR contains one of the labels below:
Available labels
bug
: Internal changes that fixes incorrect behavior.maintenance
: User-facing maintenance tasks.dependencies
: Upgrading (sometimes downgrading) dependencies.build
: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup
: Minor cleanup style change. Will not show up in release changelog.documentation
: Documentation only changes. Will not show up in release changelog.feature request
: Introducing a new feature.BREAKING CHANGE
: Changes that break compatibility in some way with current major version.other
: Changes that don't fit in the above categories.🦋 Canary release
This pull request has been released as version
0.0.0-pr-28492-sha-08d3f4f1
. Try it out in a new sandbox by runningnpx [email protected] sandbox
or in an existing project withnpx [email protected] upgrade
.More information
0.0.0-pr-28492-sha-08d3f4f1
svelte-autodoc-ts
08d3f4f1
1729192264
)To request a new release of this pull request, mention the
@storybookjs/core
team.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=28492