Repository navigation
fix(v2): Resolve plugins relative to siteDir - #2789
Merged
yangshun merged 1 commit intoMay 22, 2020
Merged
Conversation
Contributor
|
Deploy preview for docusaurus-2 ready! Built with commit b335a06 |
SamChou19815
marked this pull request as ready for review
May 22, 2020 08:33
yangshun
approved these changes
May 22, 2020
yangshun
left a comment
Contributor
There was a problem hiding this comment.
Thanks! This doesn't require users to do anything on their end right?
TIL about the module module. Nice one!
Contributor
Author
End users don't need to do anything special. |
This was referenced May 22, 2020
mrizwanashiq
pushed a commit
to mrizwanashiq/docusaurus
that referenced
this pull request
Jun 25, 2026
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.
Motivation
This diff tries to address the plugin loading issue in #2576. Even after this diff, docusaurus v2 still won't be completely compatible with Yarn v2's strict pnp mode, nevertheless it's a good step to start.
The current plugin resolution system basically does
require(whateverStringUserGiveInPlugins). This strategy will work in normal settings wherenode_modulesare laid out by package managers like this:In this case, when code in
@docusaurus/core/index.jsrunsrequire('docusaurus-plugin-abc''), it can be correctly resolved todocusaurus-plugin-abc/index.jsdue to how node module resolution algorithm works.However, with Yarn v2 pnp, the pnp runtime will reject this
require. From the perspective of pnp runtime after reading in all thepackage.jsonduringyarn install, it doesn't seedocusaurus-plugin-abcin the dependency list of@docusaurus/coreso it doesn't know which module it can be resolved to.Since docusaurus v2 uses a plugin system, it is impossible to declare every possible plugins as dependency in core's package.json. The solution is to run the
requirein the path that declares those dependencies, instead of running therequireby@docusaurus/core. The path that declares the dependencies happens to be thesiteDirappear inloadContext.Fortunately, we don't have to do some extensive hacking to achieve that. Since node 10,
createRequireFromPathis available that does exactly what we want. (Node 12 deprecates it and replaced it withcreateRequire, that's why there is a fallback in my code. See https://nodejs.org/api/modules.html#modules_module_createrequire_filename)In summary, this diff changes the plugin loading from
require(pluginToLoad)toHave you read the Contributing Guidelines on pull requests?
(Write your answer here.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)
Preview can still work. Test passes.
Related PRs
(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)