Open lithdew opened 7 months ago
Is this something Vinxi needs to improve on @nksaraf? Or is this solely my fault? :)
As an update, the source of this issue appears to be due to verbatimModuleSyntax not playing well with Vinxi. https://github.com/nksaraf/vinxi/issues/308
Facing the same and is a blocker for trying out Tanstack Start. verbatimModuleSyntax: false, doesn't seem to be a workaround for us. vinxi dev fails, vinxi build works.
Describe the bug
problem
Both
client-runtime
andserver-runtime
for TanStack Router server functions reference the samefetcher()
function implementation inserver-fns/fetcher.tsx
.This confuses Vite as
fetcher()
is mixed into both server-side and client-side (browser) code. Vite therefore assumes that server-only variables referenced inside of server functions are actually utilized client-side as well.This causes variables referenced outside* of the "use server" pragma that are NOT referenced client-side to not be treeshaken away in the browser bundle.
how i stumbled upon it
I was trying to use TanStack router server functions and wanted to define a few variables outside of the "use server" pragma.
I had a PostgreSQL connection defined in a separate file that was used only inside of server functions, and noticed that the PostgreSQL connection library was included in the browser bundle when it shouldn't have been.
an alternative
I isolated the server function runtime from Solid Start to use for my own project. Solid Start's runtime does not face this same issue. For a second I thought it was an issue with Vinxi, though it did not turn out to be the case.
I included the isolated code at the bottom of this issue.
I found out why server functions included server-only referenced variables into the browser bundle while isolating the server function runtime from Solid Start as I noticed that the difference between Solid Start and TanStack Router's runtime implementation is that
fetcher()
imported fromserver-fns/fetcher.tsx
is referenced in both server-side and client-side runtimes.solution found but no pr filed
I was hoping to file a PR to solve this issue as all that needs to be done is to independently isolate
fetcher()
for both client-side and server-side runtime.I can't really think of a clear/clean way to do this though and wanted to get some advice on this.
solid start isolated server function runtime
types.ts
:fetchEvent.ts
:server-fns-runtime.ts
(handles server-side server function invocations):server-runtime.ts
(handles client-side server function invocations):server-handler.ts
(the server function HTTP handler):Your Example Website or App
N/A
Steps to Reproduce the Bug or Issue
Define variables outside of the server function that are used inside of server functions that do not get referenced client-side. They will appear in the client-side bundle.
Expected behavior
Server-only referenced variables should be treeshaken away in the browser bundle.
Screenshots or Videos
No response
Platform
Additional context
No response