gracile-web / gracile

A thin, full-stack, web framework — Powered by Vite and Lit SSR. Works with Node's HTTP or WHATWG Fetch.
https://gracile.js.org
ISC License
28 stars 1 forks source link

env.DEV is not ever true #8

Open lschierer opened 1 month ago

lschierer commented 1 month ago

I created a minimal reproduction at https://stackblitz.com/edit/github-z1kfgi-1gya7x?file=src%2Fdocument.ts

that puts a single word on the home page, either "foo" or "bar". It will always be "foo."

You can see that in document.ts, if env.DEV is true, it shoould be set to 'bar.' I tried with both npm run dev and npm build && npm preview, it shows as foo in both cases. I'm unsure under what conditions it would ever be true if not one of those two use cases.

lschierer commented 1 month ago

I need this because when I'm running in dev mode, import.meta.url is a file:/// url, and I need to override it to be localhost so that I can test calling having a page fetch from an API route.

JulianCataldo commented 1 month ago

Hi, in your repro, you are in static mode. With minimal-hono/express starters, the preview command is launching your own, customizable server.js, which is only used in prod/preview.

In server mode, the preview command is launching node -C preview --watch server.js, note the -C for "--condition".

The env helper exposed by Gracile is meant to be used for those scenarios, when you own your node process and want to flip things in your app.

However, you've noted that during build, it should be expected that the static HTML have a "production" environment (and node exports conditions), which happens to not be the case with the vite.ssrLoadModule runtime, responsible for rendering your routes.

I don't think it's possible to switch Vite in a way that enforce a no DEV condition, because it's the condition that Vite is injecting to itself. IDK if I'm still clear at that point -.-

Theoretically, and that's been explored, your project server runtime (your routes) could be pre-bundled and used to run SSG pages rendering. It's still in the radar, because static analysis your "SSG runtime" brings benefits (think replacing "magic string", like new URL, import.meta.url, and all Vite/Rollup stuff).

So yeah, maybe try injecting your own exports conditions, which doesn't overlap with the very crowded namespace (node, require, import, type, default, whatever)?

See esm-env, which Gracile stole and extended from its helper : https://github.com/benmccann/esm-env

I keep this open as a reminder

JulianCataldo commented 1 month ago

Also, have a look at https://vitejs.dev/guide/env-and-mode