feat(ssr): support ESM-only dependencies#4450
Closed
aleclarson wants to merge 3 commits into
Closed
Conversation
This was referenced Aug 3, 2021
benmccann
reviewed
Aug 10, 2021
| if (cached) { | ||
| return cached | ||
| try { | ||
| // Resolve the import manually, to avoid the ESM resolver. |
Collaborator
There was a problem hiding this comment.
it could be worth expanding this comment to explain why we want to avoid the ESM resolver
| } else { | ||
| return moduleGraph.urlToModuleMap.get(unwrapId(dep))?.ssrModule | ||
| async function ssrImport(dep: string) { | ||
| if (dep[0] !== '.' && dep[0] !== '/') { |
Collaborator
There was a problem hiding this comment.
I don't understand this part. It seems to be suggesting that all external imports will be nodeRequired. But if it's an ESM-only module, I believe we should import rather than require it
Collaborator
|
Do we want to close this in favor of #5197, which I think would also address the same issue? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This is a proof-of-concept for compatibility with ESM-only dependencies in Vite SSR.
It needs #3950 and #3951 to be merged first. Therefore, I've squashed those two branches and included them in this PR, so you'll want to look at 6a279e80 to avoid all the git-diff noise.
Caveats:
requireresolution being done in fix(ssr): usetryNodeResolveinstead ofresolveFrom#3951 is incompatible with Node's ESM loader, so ESM-only dependencies are unaware ofresolve.dedupeandmodeoptions. The only way around this is to inject an experimental ESM loader through a CLI flag (see here), which is not ideal, since we really only want to affect module resolution while loading an SSR module, rather than all the time, but maybe that issue can be fixed via temporary injection of the Vite config into the ESM loader.Other than that, this approach seems to be bullet-proof.
Playground
I've also included an
ssr-esmplayground, but no automated tests yet. To test it out, do the following:Try setting
mode: "production"in the Vite config to see the ESM-only package load the incorrect module for itscjs-packagedependency, resulting in duplicate instances ofcjs-package(one from./index.js, the other from./index.dev.js).What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).