feat: functional template generation
This swap our template.push calls from bare strings to a series of instructions that looks like this
You can use this playground link if you want to play around with the output.
[
{ "kind": "create_element", "args": ["main"] },
{ "kind": "set_prop", "args": ["style", "background: red"] },
{ "kind": "push_element" },
{ "kind": "create_element", "args": ["div"] },
{ "kind": "push_element" },
{ "kind": "create_text", "args": ["cool "] },
{ "kind": "create_element", "args": ["b"] },
{ "kind": "push_element" },
{ "kind": "create_text", "args": ["stuff"] },
{ "kind": "pop_element" },
{ "kind": "pop_element" },
{ "kind": "create_text", "args": [" "] },
{ "kind": "create_element", "args": ["input"] },
{ "kind": "set_prop", "args": ["checked"] },
{ "kind": "set_prop", "args": ["type", "checkbox"] },
{ "kind": "push_element" },
{ "kind": "pop_element" },
{ "kind": "create_text", "args": [" "] },
{ "kind": "create_anchor" },
{ "kind": "pop_element" }
]
for the moment i've basically written some code that generate the string for the template from this series of instructions but i'll work on a separate transformation to generate a series of document.createElement (and in the future renderer.createElement) from this series of instructions.
There's one thing that i don't properly like and it's the fact that there are those push_element and pop_element instructions that don't translate into anything and just serve the purpose of delimit the children...but this allow us to not pass around variables and just refer to "the last element"
Update
I've also included the transformer and the option to convert with string template or with functional template and updated the test suite to run with both modes.
The new option will look like this
// svelte.config.js
export default {
templatingMode: "string" // or "functional",
}
and the code produced looks like this.
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';
var root = $.template_fn(() => {
var main = document.createElement('main');
main.setAttribute('style', 'background: red')
var div = document.createElement('div');
main.insertBefore(div, undefined)
var text = document.createTextNode('cool');
div.insertBefore(text, undefined)
var b = document.createElement('b');
div.insertBefore(b, undefined)
var text_1 = document.createTextNode('stuff');
b.insertBefore(text_1, undefined)
var text_2 = document.createTextNode(' ');
div.insertBefore(text_2, undefined)
var input = document.createElement('input');
div.insertBefore(input, undefined)
input.setAttribute('checked', '')
input.setAttribute('type', 'checkbox')
var text_3 = document.createTextNode(' ');
div.insertBefore(text_3, undefined)
var comment = document.createComment('');
div.insertBefore(comment, undefined)
var fragment = document.createDocumentFragment();
fragment.append(main)
return fragment;
});
export default function App($$anchor) {
var main = root();
var div = $.child(main);
var input = $.sibling($.child(div), 3);
$.remove_input_defaults(input);
var node = $.sibling(input, 2);
{
var consequent = ($$anchor) => {};
$.if(node, ($$render) => {
if (true) $$render(consequent);
});
}
$.reset(div);
$.reset(main);
$.append($$anchor, main);
}
If you want to play around with it here's a stackblitz with everything setup.
Also since this is quite a big PR if you want i can split it in two (even tho it's already separated by commits well enough)
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:, ordocs:. - This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
- If this PR changes code within
packages/svelte/src, add a changeset (npx changeset).
Tests and linting
- Run the tests with
pnpm testand lint the project withpnpm lint