Add "resolveDependencies" option to "this.load" #4358
Merged
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.
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Description
This is the third important addition I want to add to the plugin system around
this.load
. This will make it possible to load and analyze entire dependency sub-graphs from within a single plugin hook.This PR adds a flag
resolveDependencies
tothis.load
that will makethis.load
wait untilimportedIds
anddynamicallyImportedIds
for the module in question have been resolved. Thus it is no longer necessary to wait formoduleParsed
to get this information.Note that I still think that the previous restriction of NOT waiting for this information is still a good default IMO because when waiting for this hook in
resolveId
, it is very easy to accidentally create a dead-lock where the hook waits for its own completion.Here is a slightly artificial example (that I also want to put into the docs), which uses this to find out which modules will be loaded by a dynamic import and inject a proxy module that just logs all those modules when the dynamic import is triggered. More useful examples could involve scanning for CSS dependencies in a sub-graph to e.g. rewrite a dynamic import in the
transform
hook (just make sure to not wait for loading the module that contains the dynamic import if there is a cycle, or create a dead-lock between two modules dynamically importing each other. For those cases, you probably need to create a registry which modules are currently waiting for dependency graphs to be resolved and for those modules, manually resolve the non-dynamic imports yourself instead of waiting for them.).cc @yyx990803, @patak-dev, @antfu, @marvinhagemeister for implementation in Vite and WMR