Replies: 26 comments 64 replies
-
I started to experimenting with workerd/miniflare in Overall approach and code doesn't look so different from my previous one with vite runtime https://github.com/hi-ogawa/vite-plugins/tree/main/packages/vite-node-miniflare, but regarding the usability of current environment API, I have a few comments:
I haven't tested server hmr yet, but I plan to continue experiment on that later. |
Beta Was this translation helpful? Give feedback.
-
This is a feedback about a minor cosmetics of logging. Currently, simple SSR setup (like the one in a playground) shows somewhat puzzling logs: pnpm -C playground/environment-react-ssr dev
# after changing a shared file src/root.tsx
3:03:13 PM [vite] page reload src/root.tsx
3:03:13 PM [vite] page reload src/root.tsx
[vite] program reload On my react-server example with client hmr plugin, it gets more complex: pnpm -C examples/react-server dev
# initial log from two server environments
[vite] connected.
[vite] connected.
# after changing a shared file
3:13:02 PM [vite] hmr update /home/hiroshi/code/personal/vite-environment-examples/examples/react-server/src/routes/_client.tsx
3:13:02 PM [vite] page reload src/routes/_client.tsx
[vite] program reload In "two Vite servers" demo, I have {
customLogger: createLogger(undefined, {
prefix: "[react-server]",
allowClearScreen: false,
}),
} 3:13:02 PM [react-server] page reload src/routes/test/page.tsx Is there a way to add log prefix like this for new environments? or maybe can each log have a prefix of |
Beta Was this translation helpful? Give feedback.
-
When running
I haven't dive into the code yet. For now, I made a reproduction here #16426 since you might know something. |
Beta Was this translation helpful? Give feedback.
-
Can service workers be a supported environment too? I'd like to build an offline web app almost the same as an online one. If server side rendering can be bundled to be used as service worker side rendering then an offline web app doesn't need to bundle and run most (if any) JavaScript in the document scope. |
Beta Was this translation helpful? Give feedback.
-
This is awesome to see. Wanted to ask about how something like injecting new globals or CSPs would work to emulate other environments, like extensions or a Kiosk mode. It'd be great to be able to add APIs not normally exposed in-browser and that may required server-side backing (like |
Beta Was this translation helpful? Give feedback.
-
hey! 👋 i was building a really simple spa with solid, and i wanted to change the but i wasn't really able to find an easy and simple way to do this at all!! perhaps customizing the behavior of vite's dev and build feel so difficult because the module resolution is so tightly coupled to your entry file, and perhaps this coupling is exactly what makes implementing ssr with vite so complicated in the first place. I am not quite settled on the correct way to solve this, but one of my ideas is perhaps the ability to set a jsx file (with exports) as your entry file, then write a plugin that has access to those exports and returns an html string? not really the most happy with that though, im curious what ideas other people have! At the end of the day, I would like basic ssg functionality (at least in solid) to be easily implementable by end users in your own 5-line vite plugin. |
Beta Was this translation helpful? Give feedback.
-
Seems like the The Vite Env API docs don't mention assets, so I'm not sure whether something is supposed to change in that regard. (Very excited and looking forward to the Vite Env API 👀) |
Beta Was this translation helpful? Give feedback.
-
hi! I'm experimenting with the Vite Env API in @lazarv/react-server and while I had some issues about using different root for envs, I had much more benefit from using the Env API vs using multiple instances of Vite servers. My feedback on Env API:
All-in-all, even in it's current state, Env API is the way to go, I'll continue to enhance my configuration and keep experimenting with it. Nice work! |
Beta Was this translation helpful? Give feedback.
-
I'm interested what developer experience the Environment API can unlock, it seems like this could be used to provide HMR with Cloudflare wrangler/workerd ? That is something I've wanted when using CF workers to build SPAs. cc/ @IgorMinar |
Beta Was this translation helpful? Give feedback.
-
The Environments API doc claims there are no breaking changes from v5.
However, this doesn't appear to be true. It appears the runtime API has been removed and replaced by the import {
createServer,
- createViteRuntime
+ createServerModuleRunner
} from "vite";
const server = await createViteServer();
- const runtime = await createViteRuntime(server);
+ const runner = createServerModuleRunner(server.environments.ssr);
- const foo = await runtime.executeUrl<Foo>("./Foo.ts");
+ const foo = await runner.import<Foo>("./Foo.ts"); |
Beta Was this translation helpful? Give feedback.
-
Will this API enable the use of the server during build? I have a vite plugin where I need to import |
Beta Was this translation helpful? Give feedback.
-
I'm trying out the runner feature, I'm trying to achieve something like vite-node (without creating the actual CLI). I want to run a TS script in watch mode and also have HMR, but I can't figure out the HMR part. I never learned how HMR works internally, I only learned how to consume it, so I didn't know what to put in the vite/packages/vite/src/node/server/hmr.ts Line 904 in d7f46e9 so I tried to use that, but even though I didn't have
|
Beta Was this translation helpful? Give feedback.
-
Currently runners seem to be intended to be used for finite scripts, but in my case I'm using them for a long-running process because my Express code is written in TypeScript, so I'm running the code that starts the Express server with a module runner. This is working pretty ok as hinted in #16358 (comment), but whenever I change the Vite config the runner doesn't restart, I have to do it manually. I didn't really expect this to work, but I'm wondering what the recommended workflow here is 👀 I tried building a development-only plugin that turns on middleware mode and adds Vite's middleware to the Express server in
|
Beta Was this translation helpful? Give feedback.
-
I apologize for my ignorance, I'm new to Vite, and I'm hoping to build a plugin library that essentially provides you an API router. I'd like people to be able to use the plugin with their choice of runtime in their project - node, bun, deno, etc (including runtimes on the edge). Routes are declared with directories and route.js files (or .ts). I have a POC working with the Bun runtime, using a Bun plugin; but it seems to have some limitations that Vite's Environment API might be able to address. I've read the basic Rollup plugin docs, gone through the Environment API docs a couple of times, but I'm not understanding if the new API is a good use-case for me to try. Everything is a bit overwhelming for a newbie - at least for me. I figured I could create a bun environment, which has some type of module runner; but, in the flow graphic at the top of the docs, I'm not understanding how the runtime (workerd in the graphic) is tied to the module runner and Vite dev server. I assumed I'd be able to somehow determine that a dev is wanting to use the Bun runtime, then use Bun.serve to serve their routes during dev; then, of course, bundle everything at buildtime. Any insights? |
Beta Was this translation helpful? Give feedback.
-
I noticed that we have a same problem with #12734 if a different server (e.g. miniflare) is used in DevEnvironment. |
Beta Was this translation helpful? Give feedback.
-
I seem to have run into an incompatibility with the module runner and the client bundler, they're processing the https://stackblitz.com/edit/vitejs-vite-3bjmc7?file=index.html In the browser console you can see the error that Is there something specific about this module that's causing this incompatibility? |
Beta Was this translation helpful? Give feedback.
-
Hi, I noticed one "quirk": |
Beta Was this translation helpful? Give feedback.
-
Unrelated feedback to the above. I'm confused by the intention behind {
build: {
createEnvironment(name, config) {
// this doesn't work 😕 at least not in "vite build --app"
return new BuildEnvironment(name, config, { options: config.environments[name] });
},
},
environments: {
ssr: {
build: {
target: 'node20',
},
},
},
builder: {
sharedConfigBuild: true,
},
} Nor does varying config passed to |
Beta Was this translation helpful? Give feedback.
-
I noticed that Is this behavior deliberate? I realize that the API could in that case be tricky because what would it mean if |
Beta Was this translation helpful? Give feedback.
-
I have a plugin that is working, but I can't figure out how to get HMR fully working when I change a route file. Anyone know how I can resolve this? Here is part of my plugin. During my attempt to work around this, I'm declaring async configureServer(server) {
console.log('########## CONFIGURING SERVER ##########')
//const runner = createServerModuleRunner(server.environments[validated_config.environment])
runner = createServerModuleRunner(server.environments.ssr)
await createManifest(runner, validated_config, true)
api = await runner.import(path.join(cwd, 'index.ts'))
server.middlewares.use(async (req, res) => {
console.log('running vite middlewares')
const base = `${server.config.server.https ? 'https' : 'http'}://${req.headers[':authority'] || req.headers.host}`
const request = await getRequest(base, req)
const response = await api['default'].fetch(request)
setResponse(res, response)
})
},
async hotUpdate(context) {
console.log('changed file', context.file)
/* Ensure we only try to import if a route file has changed. */
if (context.file.includes('route.ts')) {
console.log('import route file')
await runner.import(context.file)
console.log('done importing route file')
}
/* Import server entry file. */
api = await runner.import(path.join(cwd, 'index.ts'))
} I experimented with When changing the entry point index.ts file, and saving changes, here is what happens - entry point changes are reflected at runtime.
When changing a route file, and saving changes, here is what happens - route file changes are not reflected at runtime.
|
Beta Was this translation helpful? Give feedback.
-
So, am I supposed to be able to literally use a runtime (Deno, Bun, etc) for the dev server, or do you always have to use Vite's Connect dev server - even with the Environment API? I've just barely been able to glue something together, where my plugin is importing the developer's server module, and then I'm manipulating Vite dev server's Connect.IncomingMessage Beside that, I'm still trying to wrap my head around when you'd wire up a custom environment. I apologize for my ignorance. |
Beta Was this translation helpful? Give feedback.
-
Regarding |
Beta Was this translation helpful? Give feedback.
-
I heard somewhere that Environments can allow skipping the SSR manifest business. If this is true, how is that intended to work? In the default client and SSR environment setup, how can I do a I noticed that builder's What used to work for me in But I understand why that shouldn't work, I guess because there's nothing in those imports that say that those are client environment imports, for all Vite knows I wanted additional SSR copies of those assets. |
Beta Was this translation helpful? Give feedback.
-
Unrelated to the above — can {
apply(_config, { command }) {
this.environment.name === 'client' && command === 'build'
},
} Or is Alternatively, I'm aching for a more compact version of {
applyToEnvironment: 'ssr',
} |
Beta Was this translation helpful? Give feedback.
-
Hello, i'm trying to use the new Environment api to see how it will help with my setup also not to distribute you what i'm trying todo with env api is to build ssr multi tenant app so I have diff css config for each tenant and diff entry point as well |
Beta Was this translation helpful? Give feedback.
-
There is something I'm still struggling to understand about One of the main goal is to be able to mimic production environments during dev. Let's take a Now let's say I'm developing a Vite plugin that adds a middleware to the devServer: devServer.use((req, res) => {
if (req.url === '/my-plugin') {
// Here, I want to load and run user provided code
// Let's say `api/handler.my-plugin.ts`
// ...
}
}); In this example, how do I make sure that my middleware also runs user provided code inside For now, I have the feeling that "my-plugin" must know that What I would like to have as a plugin author would rather be that any code by default runs under If |
Beta Was this translation helpful? Give feedback.
-
We decided to release the Environment API branch as
vite@6.0.0-alpha.x
(we'll continue regularly releasing new alpha patches).The changes started by the Runtime API are now prompting a complete review of the way Vite handles environments (client, SSR, workerd, etc). We were originally going to release this branch as
vite@5.3.0-alpha.0
but the changes are now too big for a minor so we're going to start working on the next major directly with it. We're probably going to have ~2 months of alpha period and once we feel confident with the stability of the new APIs, we'll kick the beta.If you are building a framework or are working in a Runtime (like the Cloudflare team), the next months are going to be very important to get the APIs right and serve as the base for the next iteration. We're thinking on lifting Vite's responsibilities to be able to share more in the ecosystem. For example, we're introducing the concept of a
ViteBuilder
that lets frameworks orchestrate all the environment builds they need to get a singlevite build
command to build the whole app. This means changing our stance on the "framework-as-a-plugin" pattern (SvelteKit, Remix, QwikCity, etc), and making it a first class pattern from Vite 6 forward.Check out the current Environment API docs and give us feedback. This discussion is a good place to gather it, please share your impressions and PoCs as you test.
If you think someone you know would be interested in helping us testing and creating prototypes (Workerd framework-agnostic integrations, RSC, etc), please contact them so they can get involved.
Resources:
Previous discussions:
History:
Check out @antfu's Dev SSR on Nuxt post, explaining @pi0's Dev Bundler idea that led to the creation of vite-node. Anthony used vite-node to as the engine of Vitest. After revamping it, @sheremet-va added a revised version of vite-node in Vite Core in Vite 5.1. The Environment API is a new iteration, deeply integrating the features vite-node provided in Vite's API.
Beta Was this translation helpful? Give feedback.
All reactions