Skip to content
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

feat: use a single transport for fetchModule and HMR support #18362

Merged
merged 51 commits into from
Nov 7, 2024

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Oct 16, 2024

Description

so that env providers not need to implement both fetchModule transport and HMR transport.

I think this PR still needs to polish the naming and probably better to introduce a interface that wraps HotChannel so that env providers does not need to care about internal conversion that is done for send and invoke (done).

image
The upper part is how it was before and the lower part is how it is after.

The diff of the environment implementation looks like sapphi-red/daienjo-architecture@966c60a...feat/try-single-transport (note that this diff includes two environments, service-worker and workerd)

@sapphi-red sapphi-red added the feat: environment API Vite Environment API label Oct 16, 2024
Copy link

stackblitz bot commented Oct 16, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member Author

sapphi-red commented Oct 17, 2024

I polished the interface so that it's more easier to implement for the env providers. I think we need a better name and check if this is not breaking a code that was working in 5.x.

@sheremet-va
Copy link
Member

@bluwy pointed out the inconsistency in the naming of RunnerTransport in #16358 (comment)

Should we also standardise it here?

@sapphi-red

This comment was marked as outdated.

@vite-ecosystem-ci

This comment was marked as outdated.

@sapphi-red
Copy link
Member Author

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 38b50bf: Open

suite result latest scheduled
astro failure failure
remix failure failure
sveltekit failure failure
vike failure failure
vite-environment-examples failure success
vitest failure failure

analogjs, histoire, ladle, laravel, marko, nuxt, previewjs, quasar, qwik, rakkas, redwoodjs, storybook, unocss, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-svelte, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

@sapphi-red sapphi-red added the p3-significant High priority enhancement (priority) label Oct 30, 2024
@jamesopstad
Copy link

Would it be possible to add examples of what the API looks like before and after to the description? That would help with understanding the change.

@sapphi-red
Copy link
Member Author

@jamesopstad Thanks for taking a look! The diff looks like this: sapphi-red/daienjo-architecture@966c60a...feat/try-single-transport (note that this diff includes two environments, service-worker and workerd)
Both of the env used in this example uses the newly exposed createWebSocketModuleRunnerTransport, but you can make a custom ModuleRunnerTransport as before.

@jamesopstad
Copy link

Thanks @sapphi-red. That helps a lot! I really like this proposal.

One thing that I'm wondering is if it might be possible to make it so that the transport can be added lazily? So, rather than providing { hot: true, transport: hot } you could provide { hot: true } and then add the transport by calling environment.setTransport(transport) or something similar.

We have this scenario where the WebSocket connection can't exist before the DevEnvironment constructor is called. We get around it by passing in an object and then mutating it later but it feels a bit hacky (https://github.com/flarelabs-net/vite-plugin-cloudflare/blob/7adf439fbd5e187c7419fe5686ed653191363d87/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts#L82-L85). It seems you do something similar in your example (https://github.com/sapphi-red/daienjo-architecture/blob/577a508368380f728ac0941607c85ecf419c0f71/environment-pkgs/workerd/index.ts#L162). It would be nice if there was a cleaner solution for this.

The only other thing I'm wondering is if it might be possible to avoid the need for setInvokeHandler to make the API a bit cleaner. As far as I can tell setInvokeHandler is part of the public API but invokeModuleHandler is specific to your example. Does something equivalent to invokeModuleHandler need to be part of the public API or does that not make sense? Probably missing some context here so just trying to understand.

@sapphi-red
Copy link
Member Author

One thing that I'm wondering is if it might be possible to make it so that the transport can be added lazily?

I guess that's possible. But I'd like to leave that for this PR as this PR is already quite large.

I'm wondering is if it might be possible to avoid the need for setInvokeHandler to make the API a bit cleaner.

HotChannel is an interface that is implemented by users and all methods in the interface are called by Vite. On the other hand, handleInvoke (a function that removes miniflare specific parts from invokeModuleHandler) is something that users will call. setInvokeHandler inverts that so that it can be in the HotChannel interface. This was a compromise for me so that transport related things are all inside the HotChannel. I would love to change it.

The other ways I can think of is to:

  • expose handleInvoke from NormalizedHotChannel
    • users can use it like:
      const payload = (await request.json()) as HotPayload
      const result = await this.hot.handleInvoke(payload)
      return new MiniflareResponse(JSON.stringify(result))
    • the downside of doing this is that this requires users to know more about the difference of HotChannel and NormalizedHotChannel. Maybe that's fine?
  • expose handleInvoke from DevEnvironment
    • users can use it like:
      const payload = (await request.json()) as HotPayload
      const result = await this.handleInvoke(payload)
      return new MiniflareResponse(JSON.stringify(result))
    • the downside of doing this is that the transport related things are leaked to DevEnvironment

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Nov 1, 2024

  • expose handleInvoke from NormalizedHotChannel

This sounds good to me 👍 It looks like we cannot hide the normalization entirely anyways, so adding this won't hurt hopefully.
(But again, I'm fine to iterate further in a separate PR.)

@sapphi-red
Copy link
Member Author

I implemented NormalizedHotChannel::handleInvoke 👍

@patak-dev patak-dev added this to the 6.0 milestone Nov 5, 2024
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏻

Copy link

pkg-pr-new bot commented Nov 7, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/vite@18362

commit: 65a23d2

Copy link
Collaborator

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good! I just double checked on my repo with pkg.pr.new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: environment API Vite Environment API p3-significant High priority enhancement (priority) trigger: preview
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants