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: webworker ssr target #3151

Merged
merged 2 commits into from
May 7, 2021
Merged

feat: webworker ssr target #3151

merged 2 commits into from
May 7, 2021

Conversation

patak-dev
Copy link
Member

Description

Reworked #3121 as a feat.

#2996 and #3039 reworked the package resolution to ignore the browser field if the plugin is running in ssr mode.

See @frandiox comment, if the target is webworker (ie cloudflare workers) then the browser field should be respected even in ssr mode.

There is a new config option ssr.target:

The default target for the SSR build is a node environment, but you can also run the server in a Web Worker. Packages entry resolution is different for each platform. You can configure the target to be Web Worker using the ssr.target set to 'webworker'.

We already have a build.target but if we reuse it, the users will be forced to modify this option depending on building for the web or using build --ssr. I think it makes sense to be able to define both in the config file.

A targetWeb param to the resolve methods instead of the previous ssr one (I think that targetWeb is better here than targetNode to avoid the need to negate it when using it). In the resolve plugin:

const targetWeb = !ssr || ssrTarget === 'webworker'

#2996 #3039 didn't introduce a regression for Web Workers, because the target was implicitly Node until this point. So they are both fixes for the Node ssr server.

This PR allows ecosystem solutions like vitedge to resolve the browser field as before. There may be other differences that we need to take into account when ssr.target is webworker, but for @frandiox Vite was working without issues before these changes. I think there is the need for extra configuration that may now be included as default when targeting webworkers. For example the resolve.mainFields default. I think these could be added in future PRs. As the SSR feature is still labeled experimental, ssr.target should also be considered experimental.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@Shinigami92 Shinigami92 added the p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 26, 2021
@patak-dev
Copy link
Member Author

@frandiox an update on this, we discussed it with Evan and he approves this feature. The ssr.target: 'webworker' will also be handy to define better defaults for things like the ssr.format (we are adding this option in another PR). When it is webworker, the default output format will be es instead of cjs. Once this lands, you could review your setup for vitedge and see if there are some other options defaults that could be pushed upstream.

@frandiox
Copy link
Contributor

frandiox commented May 4, 2021

@patak-js That's great news! Right now I'm setting the format: 'es' manually but it would be great if Vite takes care of it.

I hope mainFields option will still be available, though, since it needs to be adapted in some scenarios. For example, Vue internals seems to break with ['browser', 'module', 'main'] (Webpack default for webworker target) but work with ['browser', 'main', 'module'].

Thanks for all!

@antfu antfu merged commit 1c59ef1 into vitejs:main May 7, 2021
antfu added a commit to antfu/vite that referenced this pull request May 7, 2021
@antfu antfu mentioned this pull request May 7, 2021
9 tasks
antfu added a commit that referenced this pull request May 7, 2021
fi3ework pushed a commit to fi3ework/vite that referenced this pull request May 22, 2021
fi3ework pushed a commit to fi3ework/vite that referenced this pull request May 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants