#Trying to understand Solid "under the hood"

15 messages · Page 1 of 1 (latest)

cyan bridge
#

I want to get a better understanding of how solid exactly works under the hood, so I've been doing some small tests.

I have a pretty basic button increment component (same one everyone has seen 100+ times):

import { render } from 'solid-js/web'
import { createSignal } from 'solid-js'

function Counter() {
  const [count, setCount] = createSignal(0)

  return (
    <button type='button' onClick={() => {setCount(count() + 1)})}>
      {count()}
    </button>
  )
}

render(() => <Counter />, document.getElementById('app')!)

and I compiled it with esbuild. Output is as you'd expect (un-minified):

import { template as _$template } from "solid-js/web";
import { delegateEvents as _$delegateEvents } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { insert as _$insert } from "solid-js/web";
const _tmpl$ = /*#__PURE__*/_$template(`<button type="button"></button>`, 2);
import { render } from "solid-js/web";
import { createSignal } from "solid-js";
function Counter() {
  const [count, setCount] = createSignal(1);
  const increment = () => setCount(count() + 1);
  return (() => {
    const _el$ = _tmpl$.cloneNode(true);
    _el$.$$click = increment;
    _$insert(_el$, count);
    return _el$;
  })();
}
render(() => _$createComponent(Counter, {}), document.getElementById("app"));
_$delegateEvents(["click"]);

What exactly does solid do with this stuff? I'd assume it just stuffs it into a *.js file and includes it in a script tag in the html prior to serving it (obviously after bundling), but I feel like this is wrong. Could someone shed some light on what actually happens?

dreamy wave
#

Are you asking about what the custom transform does, or how the bundler should handle it?

I'd assume it just stuffs it into a *.js file and includes it in a script tag in the html prior to serving it
I mean that's pretty much what's happening

in the vite templates you could see the entry script getting imported

cyan bridge
#

probably this

dreamy wave
#

I guess that's the question to vite not solid then
I don't know much about it, I care only about the runtime part
But it seems you got that part right, you just need to have a script tag that will run this js
There is not much going on for a CSR app
I think SSR is much more complicated

#

but I feel like this is wrong
what feels wrong about it?

cyan bridge
dreamy wave
#

what were the errors?

#

also why not just use one of the starter vite templates, they have this setup
is this just to learn what's happening?

cyan bridge
cyan bridge
dreamy wave
#

hmm

try using on:click={callback} instead, it doens't use delegated events

#

but not sure why that would break

cyan bridge
dreamy wave
#

yeah I really couldn't tell you
the lack of errors in weird
not sure if this should be even tested with vite - it might do something weird to the bundling process
if you are using esbuild to build the app, just plug it into regular html file and open it without vite
then you could not-minify, see the code, maybe even run the debugger