Skip to content
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

Generate type declarations with dts-buddy #8702

Merged
merged 16 commits into from
Jun 8, 2023
Merged

Generate type declarations with dts-buddy #8702

merged 16 commits into from
Jun 8, 2023

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jun 6, 2023

This provides us with better type declarations and a smaller package. On the version-4 branch, the types directory takes 300kb (or 1.1MB on disk, because it's 264 separate files). In this PR, it takes 23kb (25kb on disk) with two files.

All the types are put into a single bundle, and treeshaken (so we don't ship internal types that aren't referenced by public APIs).

As well as the size benefits, this provides a much better experience for Svelte users. This is what happens today:

dts-buddy-before.mov

Notice a few things:

  • VSCode auto-imports svelte/internal.js. svelte is available in the auto-import menu (it's confusing that they're both there), but you have to actively select it
  • Cmd-clicking on onMount takes you to the .d.ts file, rather than the implementation. (Even though the implementation isn't all that self-explanatory in the case of onMount, developers overwhelmingly prefer this behaviour.)
  • Typing in:fade doesn't trigger an auto-import for svelte/transition — VSCode simply can't find it. It can find a horrendous list of internal junk like fix_and_destroy_block from svelte/types/runtime/internal/each
  • When you manually import it, it tries to autocomplete to svelte/transition.js, most likely because of my editor configuration. This will not resolve correctly
  • you can cmd-click on fade but, again, it takes you to the type declaration. This time, it is useful to go straight to the implementation, to understand how you'd build your own custom transition

With this PR, things look very different:

dts-buddy-after.mov

Because it uses declare module, nothing relies on the app developer having the right TypeScript/editor configuration (moduleResolution, file extension preferences etc) to get the right imports, and there are no false positives — just the public API.

@vercel
Copy link

vercel bot commented Jun 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-dev-2 ❌ Failed (Inspect) Jun 8, 2023 0:54am

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!!

btw, one thing I noticed is that if you build twice on the sites branch then the types generation fails. might be worth taking a look at while you're in this section of the code

packages/svelte/src/runtime/action/public.d.ts Outdated Show resolved Hide resolved
@Rich-Harris Rich-Harris marked this pull request as ready for review June 6, 2023 23:15
@PuruVJ
Copy link
Collaborator

PuruVJ commented Jun 6, 2023

That's cuz the sites' script expects multiple files it can bundle using roll-up plugin dts. This PR renders the entire logic useless. We'll need to rewrite the script to account for the declare module stuff now, although it should be faaaar more easier now

@Rich-Harris
Copy link
Member Author

ah yeah, that makes sense — had to make a similar change on the kit repo https://github.com/sveltejs/kit/pull/9992/files#diff-a139886ac75fed1841162f1a5ba8fdfa4fca12f24428f065a9f978e41840ba82

@dummdidumm
Copy link
Member

There's one thing I'd like to add back (manually?) somehow or at least make aware of which is the svelte/types/preprocess import that many other packages rely on (including svelte-preprocess and SvelteKit) because for some reason we never publicly exposed the preprocessor types. Removing that type path might be a bit too breaking for v4

@benmccann
Copy link
Member

There's one thing I'd like to add back (manually?) somehow or at least make aware of which is the svelte/types/preprocess import that many other packages rely on (including svelte-preprocess and SvelteKit) because for some reason we never publicly exposed the preprocessor types. Removing that type path might be a bit too breaking for v4

To clarify, it looks like it's actually svelte/types/compiler/preprocess and it's svelte-preprocess and vite-plugin-svelte that depend on it.

@Rich-Harris
Copy link
Member Author

Those types should probably be part of the svelte/compiler module (which I forgot to add to this PR anyway). Perhaps we add them there, and re-export them from svelte/types/compiler/preprocess but also deprecate them, so that in v5 we can safely drop them altogether?

@Rich-Harris
Copy link
Member Author

Ok, I've added svelte/compiler and svelte/types/compiler/preprocess. I propose that we cut major releases of svelte-preprocess and vite-plugin-svelte during 4.x that switch over to importing the relevant types from svelte/compiler, and then we can remove svelte/types/compiler/preprocess in 5.0

I think this PR is probably ready to go, unless there's anything else you can see?

@gtm-nayan
Copy link
Contributor

I propose that we cut major releases of svelte-preprocess and vite-plugin-svelte during 4.x that switch over to importing the relevant types from svelte/compiler

Do those have to be majors?

@gtm-nayan
Copy link
Contributor

Lint is failing btw

@PuruVJ
Copy link
Collaborator

PuruVJ commented Jun 7, 2023

Some folks do use svelte/internal. Does this change mean they won't get any typing from that?

@PuruVJ
Copy link
Collaborator

PuruVJ commented Jun 7, 2023

Many types are missing as well. Is this fine for final usage or something is broken?

CleanShot 2023-06-07 at 19 28 54@2x CleanShot 2023-06-07 at 19 29 13@2x

@Rich-Harris
Copy link
Member Author

Do those have to be majors?

Yeah, because they'd need to drop support for Svelte 3

@Rich-Harris
Copy link
Member Author

Some folks do use svelte/internal. Does this change mean they won't get any typing from that?

They shouldn't be! And yes, they'll lose typing (in fact I think using those things will cause typechecking to fail, which is good)

Many types are missing

Dammit I broke something in dts-buddy. Investigating

@dominikg
Copy link
Member

dominikg commented Jun 7, 2023

v-p-s is likely going to do a major for vite5 / drop node16 in September or early October

@Rich-Harris
Copy link
Member Author

Many types are missing

Should be fixed now

Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>
@Rich-Harris Rich-Harris merged commit 1046dab into version-4 Jun 8, 2023
7 checks passed
@Rich-Harris Rich-Harris deleted the dts-buddy branch June 8, 2023 13:48
@Rich-Harris
Copy link
Member Author

this has a decent effect on package size:

image

@github-actions github-actions bot mentioned this pull request Jun 8, 2023
@PuruVJ
Copy link
Collaborator

PuruVJ commented Jun 8, 2023

learn.svelte.dev also will boot up so much faster now!!! 🔥 Does this release include the dequel change in ARIAQuery yet?

@benmccann
Copy link
Member

Does this release include the dequel change in ARIAQuery yet?

No. That change has been merged, but not released. They said hopefully later this week they can cut a release

@PuruVJ
Copy link
Collaborator

PuruVJ commented Jun 8, 2023

Damn!! So it can down even more 🔥

Rich-Harris added a commit that referenced this pull request Jun 14, 2023
* chore: playground (#8648)

* initialize playground

* pnpm up

* tidy up git ignore

* remove fluff

* format

* rm readme

* fix jsconfig error

* add skip-worktree instructions

* reload hack

* simplify

* use rollup

* ughh

* add flag for SSR

* ...

* simplify further

* configure launch.json

* add debugger info to readme

* remove vm modules flag

* use replaceAll instead of replace

* tidy up

* fix: make it run

* add watch to launch config

* Generate type declarations with `dts-buddy` (#8702)

* use dts-buddy

* remove debug output

* remove existing type generation script

* fix package.json

* update gitignore

* bump dts-buddy

* remove unused action entry point

* add svelte/compiler and svelte/types/compiler/preprocess modules

* bump dts-buddy

* annoying

* changeset

* bump dts-buddy

* get rid of .d.ts files

* another one

* Update packages/svelte/package.json

Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>

---------

Co-authored-by: Rich Harris <git@rich-harris.dev>
Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>

* fix: export ComponentType (#8694)

* fix: export ComponentType

* ughh

* changeset

* fix: derived types (#8700)

* fix: derived store types

* changeset

* Version Packages (next) (#8709)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* update type generation script

* remove unused stuff

* fix: changeset publish script isn't called release anymore (#8711)

* chore: remove prepare script (#8713)

* chore: fix release workflow (#8716)

* More readable, Fix $$_attributes

* Fix types (#8727)

* put comment in right place

* bump dts-buddy

---------

Co-authored-by: Rich Harris <git@rich-harris.dev>

* build types

* add svelte/compiler types

* remove prepare script

* fix

* typo

* squelch errors

* Add svelte and kit to twoslash's types field

* squelch more stuff

* Add errors to account for new types

* Remove deps

* formatting tweak

* fix linting, maybe

* the hell

* gah

* Fix types a bit

* bump dts-buddy

* pnpm generate in dev mode

* Cache again

* reduce index

* bump dts-buddy

* remove comment

---------

Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>
Co-authored-by: Rich Harris <git@rich-harris.dev>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Dominik G <dominik.goepel@gmx.de>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Puru Vijay <devpuruvj@gmail.com>
@gtm-nayan gtm-nayan mentioned this pull request Jun 17, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants