solidjs / solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
https://solidjs.com
MIT License
32.53k stars 931 forks source link

Treeshake hydration checks #1537

Closed thetarnav closed 1 year ago

thetarnav commented 1 year ago

Describe the bug

From what I understand, the current way to check if the code runs during hydration would be to check if sharedConfig.context exists. This - together with isServer - could allow library authors to handle SSR / CRS / CRS + hydration cases differently. (example: https://stackblitz.com/edit/github-tzjxcu-xofarw?file=src%2Fcomponents%2FTest.tsx prompted by this issue)

But this way of checking it is not tree-shakeable. If we build a SPA that doesn't hydrate, the code for the hydration path will still remain. I know this is not as simple as having a const isServer variable, but if there is nothing to set the hydration context, anything that relies on it could be tree-shaken, right?

image

Also I'm noticing that code from solid-js that uses it remains too, so this could be improved as well

Your Example Website or App

https://stackblitz.com/edit/solid-hydration-treeshaking?file=dist%2Fassets%2Findex-f7669aee.js&file=src%2FApp.tsx

Steps to Reproduce the Bug or Issue

  1. run pnpm build
  2. open bundled js fine in /dist
  3. check if the if (sharedConfig.context) {} remains

Expected behavior

code paths that won't ever run should be removed from the end bundle

ryansolid commented 1 year ago

Hmm.. yeah Rollup has changed over the years as it used to work and stopped. We do have something we use internally for this maybe I should expose it. I was probably thinking this was only an internal concern but yeah it could affect reusable libraries.

https://github.com/solidjs/solid/blob/main/packages/solid/src/render/component.ts#L14-L17