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: Add ssr.noExternal = true option #4490

Merged
merged 10 commits into from Aug 16, 2021
Merged

feat: Add ssr.noExternal = true option #4490

merged 10 commits into from Aug 16, 2021

Conversation

jplhomer
Copy link
Contributor

@jplhomer jplhomer commented Aug 3, 2021

Description

This PR adds a ssr.noExternal = true option which disables all externals during the SSR build process. This is effectively the same as manually adding all dependencies of a package to ssr.noExternal.

This is helpful when building for Workers environments like Cloudflare Workers, where require is not supported at all.

This PR also adds a check to the resolve plugin to ensure the user doesn't attempt to import any Node.js built-ins, as those wouldn't be supported in the Workers environment.

Additional context

This functionality was discussed in a comment thread here: #4230 (comment)

Another option I considered was to imply this behavior from ssr.target = 'webworker'. However, that would be a breaking change, and I think it's better to be explicit in this case (maybe somebody does build a webworker runtime someday that supports require - who knows?)

Tests: I'd love to know the most effective way to test this behavior. Since it's exclusive to Worker environments, and it's only related to builds, it's tough to use the playground tests. I'd also want to test the output of the file, meaning unit tests aren't really the right way to go (unless someone can point me to an example).

UPDATE: Tests have been added. See #4490 (comment)

I also changed the name from bundle to bundleAll.

UPDATE 2: Changed to ssr.noExternal = true per feedback.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@jplhomer jplhomer marked this pull request as ready for review August 3, 2021 15:02
@benmccann
Copy link
Collaborator

benmccann commented Aug 3, 2021

Maybe we should call this bundleAll?

I had been thinking that it could be really nice to rename ssr.noExternal to ssr.bundle at some point. If @brillout's plan to make everything external at some point comes to fruition we wouldn't need the ssr.external option anymore - just an option to bundle certain libraries. But even if we never do this rename, I think that bundleAll is more descriptive of what the option does since Vite is already bundling but just isn't bundling everything

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Aug 3, 2021
@brillout
Copy link
Contributor

brillout commented Aug 3, 2021

@benmccann You're right naming it bundleAll is probably better.

@frandiox
Copy link
Contributor

frandiox commented Aug 3, 2021

I think that bundleAll is more descriptive of what the option does since Vite is already bundling but just isn't bundling everything

That was my original reasoning of having bundle-all vs bundle. But I guess both work well as long as it's documented.

@jplhomer
Copy link
Contributor Author

jplhomer commented Aug 4, 2021

bundleAll it is! 🎉

I've also added a new playground test, ssr-webworker. It boots a custom worker server using miniflare. Only a couple assertions have been added, but it could be a useful place to debug worker-related issues in the future.

@jplhomer jplhomer changed the title feat: Add ssr.bundle option feat: Add ssr.bundleAll option Aug 4, 2021
@brillout
Copy link
Contributor

brillout commented Aug 4, 2021

Neat 👌. From a high-level perspective, the code LGTM.

brillout
brillout previously approved these changes Aug 4, 2021
Shinigami92
Shinigami92 previously approved these changes Aug 4, 2021
@patak-dev
Copy link
Member

Looks good to me 👍🏼
We'll discuss this new feature with the team next week.

@Shinigami92
Copy link
Member

Today we discussed with the team that we want to prevent more and more options and would like to reuse noExternal instead of bundleAll.
So we could do something like noExternal: true or noExternal: '*'.

@patak-dev
Copy link
Member

Let's go with noExternal: true, as it seems to properly imply that there isn't anything externalized. Just a detail, but I think that this good because these APIs may change in the future (example of a proposal to do so). Since bundling everything is part of this API, it seems better to me to avoid new options that we may need to deprecate in a month.

@antfu antfu marked this pull request as draft August 14, 2021 01:41
@antfu antfu requested a review from Shinigami92 August 14, 2021 01:41
@jplhomer jplhomer dismissed stale reviews from Shinigami92 and brillout via f7d1c36 August 16, 2021 15:35
@jplhomer jplhomer marked this pull request as ready for review August 16, 2021 15:35
@jplhomer
Copy link
Contributor Author

Good call on the option usage. I've updated this PR to use ssr.noExternal = true.

@Shinigami92
Copy link
Member

Could you revert your yarn.lock? I think it has nothing todo with the PR.

@Shinigami92
Copy link
Member

Still changes in yarn.lock 🤔 Maybe you need a rebase?

@jplhomer jplhomer changed the title feat: Add ssr.bundleAll option feat: Add ssr.noExternal = true option Aug 16, 2021
Shinigami92
Shinigami92 previously approved these changes Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants