Skip to content

feat: more efficient code generation when referencing globals #12712

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

Merged
merged 2 commits into from
Aug 4, 2024

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Aug 2, 2024

Noticed this while reviewing #12692 — there's some low-hanging fruit here. When referencing built-ins in templates...

<p>{location.href}</p>
<p>{btoa('hello world')}</p>

...we currently create effects (or deriveds, in the case of component props):

$.template_effect(() => $.set_text(text_1, btoa('hello world')));
$.template_effect(() => $.set_text(text, location.href));

If we make the perfectly reasonable assumption that globals don't reference Svelte state, we can do this instead:

text.nodeValue = location.href;
text_1.nodeValue = btoa('hello world');

This assumption would break if someone did something stupid...

globalThis.foo = () => somestate;

...but if people do that they will have their Svelte license revoked.

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 Aug 2, 2024

🦋 Changeset detected

Latest commit: 33bf522

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

@ottomated
Copy link
Contributor

If this only applied to built-in globals, it would still be better in the most common case but prevent the assumption from ever breaking (I agree it's uncommon, but I could see it happening in two cases: a svelte app embedded into another page, or communication with a browser extension)

@Rich-Harris Rich-Harris merged commit e66416b into main Aug 4, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the purity-test branch August 4, 2024 02:18
@Rich-Harris
Copy link
Member Author

It doesn't really — someone could happily do const location = $state(...); globalThis.location = location and it would still break stuff. The trick is to not do that!

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.

3 participants