solidjs / solid-refresh

MIT License
86 stars 18 forks source link

[Bug?]: solid-start's dev mode oversees that `function` statements are hoisted, leads to errors #51

Closed danieltroger closed 10 months ago

danieltroger commented 10 months ago

Duplicates

Latest version

Current behavior 😯

Right now, solid-start in dev mode transforms function statements to var statements, without considering function hoisting. This can break code, as can be seen in the reproduction steps.

Screenshot 2024-01-08 at 17 42 36 Screenshot 2024-01-08 at 17 42 46

Expected behavior 🤔

Valid javascript code should still run in dev mode

Steps to reproduce 🕹

Steps:

  1. Clone https://github.com/danieltroger/solid-start-hoisting-issue
  2. yarn dev
  3. visit localhost:3000
  4. observer error in dev console

Context 🔦

I prefer having the exported thing at the top of modules. Like in the attached example I have a case where I want to add a symbol to a function, however I have to do it in an IIFE in the export so that I can add a pure comment and the module still can be tree-shaken.

Your environment 🌎

M1 Max 16" MBP, node v21.5.0 on macOS 14.2.1 (23C71)
ryansolid commented 10 months ago

I think this is a SolidRefresh issue, specifically to do with HMR wrappers. I don't know how easy it is to fix given we have to wrap every exported function in a wrapper. I'm gathering we have to do the old CoffeeScript trick of doing all the var declarations up top. I will move it to the right repo.

lxsmnsyc commented 10 months ago

I'm still not sure what would be the fix for this. I'm thinking if Solid Refresh should move all FunctionDeclaration at the top and maybe that would fix the issue.

edit: I just noticed Ryan had the same idea as I didn't read it lol.