-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
feat: static template expression evaluation #15374
base: main
Are you sure you want to change the base?
feat: static template expression evaluation #15374
Conversation
🦋 Changeset detectedLatest commit: 9907cbc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
ok there appear to be some tests that need to be changed as well... |
once tests are implemented for the client, I will work on adding this to the server version. |
We tried inlining constant values before and it ended up in such a hassle that we reverted it...there are a thousands things to keep track of for every non trivial example. If you succeed big props to you, just wanted to warn you 😄 |
If I'm correct in assuming you're referring to #10015, this should work, since that appears to hoist constant variables for optimization, so for example: <script>
let name = 'world';
</script>
<h1>Hello, {name}!</h1> would become something like this: let name = 'world';
let root = $.template(`<h1>Hello, ${name}!</h1>`);
function Component() {
//...
} This instead takes a more direct approach, by replacing constant primitive variables with their value, like so: //...
function Component() {
//...
let name = 'world';
h1.textContent = `Hello, world!`;
//...
} Additionally, this PR is not only limited to variables; most expressions consisting of literal primitives (and/or those variables) will be evaluated as well. |
This optimizes the compiled output by using static analysis to evaluate some nonreactive template expressions.
For example, this component
would have its template expression compiled to:
However, this:
would get compiled to:
I think the end goal (that would be achieved either here or another PR) would be inlining this in the template, so that the top example would end up having its template look like this:
...which removes the (albeit small) overhead of locating the
<p>
element and creating an effect to update it.Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint