Skip to content

fix(v2): Resolve plugins relative to siteDir - #2789

Merged
yangshun merged 1 commit into
meta:masterfrom
SamChou19815:resolve-plugin-relative-to-siteDir
May 22, 2020
Merged

yangshun merged 1 commit into
meta:masterfrom
SamChou19815:resolve-plugin-relative-to-siteDir

Conversation

@SamChou19815

Copy link
Copy Markdown
Contributor

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 where node_modules are laid out by package managers like this:

- website
  - node_modules
    - @docusaurus/core
      - index.js
    - docusaurus-plugin-abc
      - index.js
  - package.json
    { dependencies: ['@docusaurus/core', 'docusaurus-plugin-abc' }

In this case, when code in @docusaurus/core/index.js runs require('docusaurus-plugin-abc''), it can be correctly resolved to docusaurus-plugin-abc/index.js due 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 the package.json during yarn install, it doesn't see docusaurus-plugin-abc in the dependency list of @docusaurus/core so 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 require in the path that declares those dependencies, instead of running the require by @docusaurus/core. The path that declares the dependencies happens to be the siteDir appear in loadContext.

Fortunately, we don't have to do some extensive hacking to achieve that. Since node 10, createRequireFromPath is available that does exactly what we want. (Node 12 deprecates it and replaced it with createRequire, 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) to

require(Module.createRequire(context.siteDir).resolve(pluginToLoad))

Have 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.)

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 22, 2020
@docusaurus-bot

docusaurus-bot commented May 22, 2020 •

Copy link
Copy Markdown
Contributor

@SamChou19815
SamChou19815 marked this pull request as ready for review May 22, 2020 08:33
@SamChou19815
SamChou19815 requested a review from yangshun as a code owner May 22, 2020 08:33

@yangshun yangshun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This doesn't require users to do anything on their end right?

TIL about the module module. Nice one!

@yangshun
yangshun merged commit faf48e9 into meta:master May 22, 2020
@SamChou19815

Copy link
Copy Markdown
Contributor Author

Thanks! This doesn't require users to do anything on their end right?

TIL about the module module. Nice one!

End users don't need to do anything special.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants