> <script> blocks are not written in JavaScript, but rather a language with the same general syntax but different semantics
One could argue that React's hooks are the same: they look like JS but change JS semantics.
However, your point is still valid.
Re:Virtual DOM is pure overhead (2018)
by chrismorgan on Thursday July 01, 2021 @05:38PM
(#27705193)
Nah, React hooks are still JavaScript—they’re library functionality, executed directly as JavaScript.
Svelte’s approach requires detailed knowledge of the structure of state, and requires compilation: components’ <script> blocks are not written in JavaScript, but rather a language with the same general syntax but different semantics, and some places where JavaScript is too flexible to be tractable get replaced with special template syntax (like {#each} instead of for-loops or Array.prototype.map). Svelte cannot be implemented as a JavaScript library (I disqualify eval()). Svelte is also deliberately severely limited in what it can express in various places, whereas React gives you the full power of JavaScript (for better and for worse).
You could perhaps implement an optimising compiler for a small subset of React components that avoid problematic patterns and are written in TypeScript with proper specifications of the types of state and props; but if you considered it unacceptable for this compiler to change the component’s semantics, I think you’d be surprised at how little serious React code in the wild could actually be supported. Even simple loops might be out of reach. The Svelte approach can’t be a progressive enhancement, it’s an all-or-nothing (at the component level).