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

feat: allow state/derived/props to be explicitly exported from components #10523

Merged
merged 11 commits into from
Feb 20, 2024

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Feb 18, 2024

I think it's fine to export state/derived/props from components, which means reverting #10430.

Note that export let is still forbidden in runes mode, since it's much more likely to be unintentional than not. To export state, you must do it in two parts:

let x = $state(...);
export { x };

We could potentially allow export let x = $state(...) in future, once we go runes-only.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Feb 18, 2024

🦋 Changeset detected

Latest commit: 6104318

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris Rich-Harris marked this pull request as ready for review February 18, 2024 16:32
@Rich-Harris Rich-Harris changed the title Revert "fix: stricter validation for component exports (#10430)" feat: allow state/derived/props to be explicitly exported from components Feb 18, 2024
@benmccann
Copy link
Member

Will the exported state be reactive in this case though? That's usually how I see people try to do this and I think it could cause confusion if they were allowed to do it and then it didn't work

@Rich-Harris
Copy link
Member Author

Yes, it will — though if you try and mutate it from outside the component Svelte will yell at you (#10464)

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

This isn't working correctly yet.

@Rich-Harris
Copy link
Member Author

the first of those is fixed, working on the second

@Rich-Harris
Copy link
Member Author

should be all set now

Comment on lines 173 to 174
'invalid-state-export': () => `Cannot export state from a module if it is reassigned`,
'invalid-derived-export': () => `Cannot export derived state from a module`,
Copy link
Member

Choose a reason for hiding this comment

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

Code looks good now, though I slightly prefered the changed error messages because they gave you instructions on how to resolve the error. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

merged the changes from both

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

3 participants