[feat] no invalidate reactive vars if dependencies are all static
Reopen of https://github.com/sveltejs/svelte/pull/4066
this does output optimisation for a simple edge case of:
<script>
let name = 'world';
$: two_name = name + name;
</script>
{two_name} {name}
Here even though two_name is declared using reactive statements, but since name are static, it should not need $$invalidate to wrap around it
Before submitting the PR, please make sure you do the following
- [x] 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
- [x] Prefix your PR title with
[feat],[fix],[chore], or[docs]. - [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
npm testand lint the project withnpm run lint
I tried to run the compiler and I have 1 question.
two_name is static, so can we remove update lifecycle process also?
p(ctx, [dirty]) {
if (dirty & /*two_name*/ 1) set_data(t0, /*two_name*/ ctx[0]);
},
I just copied my comment from https://github.com/sveltejs/svelte/pull/7938#issuecomment-1279114630
We discussed with maintainers and concution is that we should not implement warnings because compiler can not decide that reactive statement is essentially unnecessary or not.
e.g.
<script>
export const foo = 1;
export const bar = 1;
export const baz = 1;
// For now, all dependencies are static, but maybe in the future, one or more variable will be dynamic.
// So Intentionally, I used `$:` for here for preventing to make a bug.
$: qux = foo + bar + baz;
</script>
@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel.
A member of the Team first needs to authorize it.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated |
|---|---|---|---|---|
| svelte-dev-2 | ❌ Failed (Inspect) | Feb 22, 2023 at 5:01PM (UTC) |
@baseballyama that seems like an edge case, i think if you want to do that you should just svelte-ignore the warning
From my perspective, it sounds like semantics matter. (e.g. For now, this is static, but according to domain knowledge, this is not truly static.) So Svelte compiler doesn't know such semantics, so IMO it should be completely configurable. Already you created an issue on eslint-plugin-svelte, and I agree to add this rule on ESLint, so I or someone will implement this.👍
This has been released in 3.56.0.
It seems that this causes the value to be undefined on first render. https://github.com/TanStack/query/issues/5120