Description
I appreciate the attention that's gone into making Svelte output readable code. It makes it easier to debug and understand what is happening at a glance. However, one part which I feel stands in contrast to this is the svelte-[hash]
attributes. They're pretty verbose, making the rendered DOM harder to inspect and consume visually, while offering no human-friendly clue as to where they belong.
Under the assumption that the purpose of the hash is only to serve as a unique identifier for a component, I believe these concerns could be resolved by replacing the hash with the name of a component instead. In the event of two components having the same name, they can be deconflicted by appending a number as is done in the script.
Having made a few modifications to the compiler based on this idea, I compiled the templates for svelte.technology
. Here's a compact example showing before and after:
<div class="secondary" svelte-3279509091>
<ul class="guide-toc" svelte-2365569553>
<li svelte-2365569553><a class="section " href="/guide#introduction" svelte-2365569553>Introduction</a></li>
</ul>
</div>
https://gist.github.com/Zirro/79e016d2e84ca98976c2fa5a5ab7b9b9
<div class="secondary svelte•nav">
<ul class="guide-toc svelte•guidecontents">
<li class="svelte•guidecontents"><a class="section svelte•guidecontents" href="/guide#introduction">Introduction</a></li>
</ul>
</div>
https://gist.github.com/Zirro/708b96d3046b72af98971477fa9282ea
You'll note that there's another change as well - it's using classes instead. I decided to try this for two reasons - one of fairly little importance, which is that Firefox's view-source
renders attributes in bold black letters, making them quite a distraction.
More importantly is that selecting elements by attributes is measurably slower than doing so with classes. I figured that a class can do the job just as well, provided that it uses a name with virtually no risk of clashing with ones that site developers would add.
For this, I conveniently came across • in the <title>
of the Svelte homepage. I believe it strikes a good balance, showing that the class is special without being a distraction. It can also be set together with other classes in a single .className
instead of a separate setAttribute()
.
I suppose this is two suggestions in one. The hash -> name and attribute -> class changes can both be discussed separately.
What do you think? Is there something which I've overlooked that would make this impractical?