-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[@hono/vite-dev-server] Proposal for Specialized Plugin: Cloudflare Workers/Wrangler Integration #25
Comments
It would be helpful if you could describe your desired workflow. What should be handled by Vite and what should be handled by Wrangler? For example, for a client, I've implemented a Vite plugin that runs a dev server for their specific needs with the following config:
During Vite serve ( |
@navaru As linked in the related issue, I hope that the vite plugin will recognize environment variables and various infrastructure bindings specified in TOML. I plan to use In other words, the suggestions you have provided don't meet the requirements I am looking for 😢 |
I am a bit confused about what you're trying to achieve, as Cloudflare Pages don't use a In the title of this issue, you mention the plugin If you want to build for Cloudflare Pages you need to use If you plan on using Cloudflare Workers for Worker Sites, this is discouraged by Cloudflare:
|
@navaru Yes, I'm planning to use for Workers Site. So I didn't mention about it. I know your opinion but This is the best solution for us. I explain the reason below:
Our Cloudflare Workers are built following this instruction: https://developers.cloudflare.com/workers/wrangler/environments/ |
Hi @Code-Hex! Like @navaru, I believe you're referring to Cloudflare Pages, but it seems you're actually thinking about Cloudflare Workers. There are several approaches to better support Cloudflare Workers:
This issue affects all platforms, including Workers and Pages:
Regarding this, it's worth discussing. However, I think the current pattern is fine, as Hono apps tend to be isomorphic. If we can use Bindings as they are, it should be sufficient. |
I believe it would be better to implement the platform-dependent issues as a plugin structure for the dev server. If feasible, cf bindings by Miniflare could be one of the plugins. However, as mentioned above, I currently don't have a clear idea on how to implement this. |
I am wondering if this implementation can be achieved with reference to the vitest-miniflare-environment implementation. The vitest-miniflare-environment reads the contents of wrangler.toml and automatically sets bindings as the output of I was wondering if this part could be applied to a vite plugin. I looked at how vitest-miniflare-environment gets bindings. The vitest-miniflare-environment internally executes createMiniflareEnvironment of shared-test-environment, which passes the BindingPlugin to The important part is the I ported this getBindigns method to @hono/vite-dev-server and was able to get bindings in the fetch function. MiniflareCore was initialized using the implementation of vitest-environment-miniflare and shared-test-environment as reference, so the code used for test is included and needs to be modified. Code for a plugin that runs |
@naporin0624 Thank you for providing your idea! @yusukebe I've tried to implement it easily in the following PR. I have confirmed that d1 and kv are working correctly. I am copying a lot of code from workers-sdk. This is because some of the code defined in workers-sdk is not provided as a package. I thought it would be necessary to follow along in this regard. |
Thanks! I'm not sure if I fully understand, but here are my thoughts. Your approach referring to https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare With v3, we can use https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare#class-miniflare So, our challenge is how to handle @Code-Hex is working on this, so let's follow their lead. |
OK! |
MEMO: I just noticed that we don't need to use workers-sdk code. |
I was able to allocate some time, so I attempted to implement it using 'wrangler unstable_dev.' The potential benefit of this approach was that I wouldn't have to keep track of updates to 'wrangler' myself. However, the I will proceed with the approach outlined in this PR. |
BTW not sure if you are already aware but this should now be much easier to implement as there’s a new getBindingsProxy function in Miniflare cloudflare/workers-sdk#4523 so could be a great opportunity to simplify and remove a lot of code |
getBindingsProxy has been released to stable version of wrangler. |
I will try to fix this using the API! |
Hey @Code-Hex Please wait a minute! Now I am thinking of creating a "cloudflare plugin" with I am going to create that draft, and I would like @Code-Hex to review it. |
@yusukebe sounds good! |
I need your help, but since this is a Cloudflare feature, let me, a Cloudflare employee, do it:) |
I think we should wait a little longer on this issue. The new version, 5.1 of Vite has been released. https://vitejs.dev/blog/announcing-vite5-1.html With this version, Vite is now experimentally supporting Runtime other than Node.js. This means that Miniflare/workerd environments can be integrated into Vite using Vite's API. We Cloudflare may make Miniflare/wokred runtime official. It is more efficient and preferred than having each framework support Cloudflare individually. So, let's wait and see. |
Hey @Code-Hex and others! Now, we can use the new API import devServer from '@hono/vite-dev-server'
import { defineConfig } from 'vite'
import { getPlatformProxy } from 'wrangler'
export default defineConfig(async () => {
const { env, dispose } = await getPlatformProxy()
return {
plugins: [
devServer({
env,
plugins: [
{
onServerClose: dispose
}
]
})
]
}
}) |
I close this PR once! |
Related issue: #24
@hono/vite-dev-server currently supports development servers for most edge environments. However, as we attempt to address issues specific to platforms like Cloudflare Workers/Wrangler, such concerns become more prominent. Similarly, when it comes to features tailored for other edge environments, this can lead to a situation where, for those who want to utilize the Wrangler environment, this plugin becomes overly complex.
Therefore, I have created this issue with the idea that it would be beneficial to first extract specific functionality for Cloudflare Workers/Wrangler as a dedicated plugin. I would appreciate your input on this matter.
The text was updated successfully, but these errors were encountered: