You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Svelte's toolbox for DOM traversal is sweet and simple, primarily composed of $.next and $.child calls to access siblings and children respectively. This method works well for small components, but can become rather unwieldy for larger components. In larger components, reactive elements are often nested, which means that to access one element, your compiled code could have 3+ lines of:
This is not only repetitive, but hard to minify, as the best minification is suboptimal:
letc=a.child(a.child(a.child(b)));
Describe the proposed solution
I think that an encoded DOM traversal sequence could be useful here. Essentially, the compiler would take the sequence of nodes accessed in the component fragment and serialize it as a string. Marko does something similar to this, and I think it'd fit well for Svelte. Here's a quick look at how this could work:
letsequence=$.sequence(`|>|^|`);letroot=$.template(`<h1> </h1><button> </button>`,1);leton_click=(_,count)=>$.update(count);exportdefaultfunctionComponent($$anchor){letfragment=root();letnext=sequence(fragment);letname='world';letcount=$.state(0);leth1=next();h1.textContent=`Hello, ${name??''}`;letbutton=next();button.__click=[on_click,count];lettext=next();$.reset(button);$.template_effect(()=>$.set_text(text,`Count is ${$.get(count)??''}`));$.append($$anchor,fragment);}
In the above example, $.sequence takes an encoded sequence for DOM traversal. The encoding uses characters to represent different operations:
>: nextSibling
^: firstChild
|: stop and return current node
So, for the example above, the sequence could be interpreted as:
Get the first child (<h1>)
Get the sibling of the h1 (<button>)
Get the child of the button (the text)
$.sequence's definition could look something like this:
The encoding could also be shortened for repeated operations (for example, >3 could mean going to the third sibling). However, I think that the encoding should be relatively simple, because I, like probably a lot of other contributors, learned how Svelte's compiler and internals work by reading and analyzing the compiled output, and I wouldn't want this feature to make the compiled output too difficult for possible contributors to understand.
Additional Implementation Details
This would rewrite a bit of thesefiles, but the changes shouldn't be too complex; instead of generating $.next, and $.child (and variables for each interim node), it would just push characters to something like context.state.sequence and generate calls to next. Additionally, since each sequence is unique to its template, I think it'd be best if the sequence were to be included as arguments to $.template, and fragment and next could be retrieved via destructuring:
This is generally a non issue with gzip tho since all those instructions gets compressed pretty well. We also have $.next(3); when we need to skip multiple...I wonder if we could ro the same for $.child
I tried the encoding approach – it didn't really make the total size much better when it comes to gzip and made the code and logic harder to follow. Furthermore, it had an impact on runtime performance too – as we have other operations other than child and and next, especially when it comes to hydration as we'd now have to ship all the code for each of the functions because of the switch statement vs dead code eliminating them as we do now if they're not used.
Describe the problem
Svelte's toolbox for DOM traversal is sweet and simple, primarily composed of
$.next
and$.child
calls to access siblings and children respectively. This method works well for small components, but can become rather unwieldy for larger components. In larger components, reactive elements are often nested, which means that to access one element, your compiled code could have 3+ lines of:This is not only repetitive, but hard to minify, as the best minification is suboptimal:
Describe the proposed solution
I think that an encoded DOM traversal sequence could be useful here. Essentially, the compiler would take the sequence of nodes accessed in the component fragment and serialize it as a string. Marko does something similar to this, and I think it'd fit well for Svelte. Here's a quick look at how this could work:
In the above example,
$.sequence
takes an encoded sequence for DOM traversal. The encoding uses characters to represent different operations:>
:nextSibling
^
:firstChild
|
: stop and return current node$.sequence
's definition could look something like this:The encoding could also be shortened for repeated operations (for example,
>3
could mean going to the third sibling). However, I think that the encoding should be relatively simple, because I, like probably a lot of other contributors, learned how Svelte's compiler and internals work by reading and analyzing the compiled output, and I wouldn't want this feature to make the compiled output too difficult for possible contributors to understand.Additional Implementation Details
This would rewrite a bit of these files, but the changes shouldn't be too complex; instead of generating
$.next
, and$.child
(and variables for each interim node), it would just push characters to something likecontext.state.sequence
and generate calls tonext
. Additionally, since each sequence is unique to its template, I think it'd be best if the sequence were to be included as arguments to$.template
, andfragment
andnext
could be retrieved via destructuring:Importance
nice to have
The text was updated successfully, but these errors were encountered: