feat(ssr): support ESM-only dependencies #4450
Closed
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:
require
resolution being done in fix(ssr): usetryNodeResolve
instead ofresolveFrom
#3951 is incompatible with Node's ESM loader, so ESM-only dependencies are unaware ofresolve.dedupe
andmode
options. 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-esm
playground, 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-package
dependency, 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
).