nksaraf / vinxi

The Full Stack JavaScript SDK
https://vinxi.vercel.app
MIT License
1.73k stars 70 forks source link

add /** __PURE__ */ annotation to `createReference` calls for dead code elimination: Error: Cannot access '${IDENTIFIER}' before initialization #104

Open simonzeiger opened 8 months ago

simonzeiger commented 8 months ago

After upgrading my Solid Start project's Vinxi from 0.0.59 -> 0.1.1, when I run pnpm build I get

Cannot access 'gr$1' before initialization

That identifier is coming from a a cache call, like:

export const getOtherPlayers = cache(gOP, "other-players");

gOP is the identifier failing. It is a server function. Perhaps the server function is missing from the client build? Or the server build?

Everything works with pnpm dev. When I build with ssr: false, this error happens during the prerender. stage of pnpm build. When I enable ssr: true, the error happens when I run node .output/server/index.mjs

nksaraf commented 8 months ago

can you give a github repo as a reproduction so I can try and fix it there.

simonzeiger commented 8 months ago

I added you to my repo here: https://github.com/simonzeiger/substring

Sometimes it just fails to get the an env variable from process.env, and sometimes it throws the missing identifier warning.

The failed env variable is coming from here: https://github.com/simonzeiger/substring/blob/main/src/api/database.ts#L9

nksaraf commented 7 months ago

first thing to fix is the top level "use server" in the game.ts file. that will keep the db out of the client. fixing this in #126

simonzeiger commented 7 months ago

Are you saying that I should have one or I shouldn't? For some reason when I put one a the top of that game.ts, everything breaks. It seems a bit fragile of where I can and cannot put "use server" without things getting borked up.

simonzeiger commented 7 months ago

Top level use server in game.ts gives me this error:

H3Error: undefined does not match field "body": BlockStatement of type FunctionExpression

Something going wrong in the transpilation/extraction step. I'm on vinxi 0.1.4

nksaraf commented 7 months ago

You will have to override @vinxi/plugin-directives to 0.1.2. In package JSON you can add an overrides field with this entry

nksaraf commented 7 months ago

Are you saying that I should have one or I shouldn't? For some reason when I put one a the top of that game.ts, everything breaks. It seems a bit fragile of where I can and cannot put "use server" without things getting borked up.

Yeah we are still finding and fixing all the edge cases and your issues are really helpful in that process

nksaraf commented 7 months ago

You will have to override @vinxi/plugin-directives to 0.1.2. In package JSON you can add an overrides field with this entry

This should be bumped in the next solid start version