Skip to content

fix(v2): Ensure that theme classic require webpack provided by @docusaurus/core - #2795

Merged
yangshun merged 1 commit into
meta:masterfrom
SamChou19815:theme-classic-require-webpack-from-docusaurus-core
May 22, 2020
Merged

yangshun merged 1 commit into
meta:masterfrom
SamChou19815:theme-classic-require-webpack-from-docusaurus-core

Conversation

@SamChou19815

Copy link
Copy Markdown
Contributor

Motivation

This diff continues the progress of making docusaurus v2 work under yarn v2's pnp mode.

The Problem

There is a tricky require inside @docusaurus/theme-classic:

const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');

@docusaurus/theme-classic also declares webpack as a peer dependency. Currently at runtime, webpack is likely to be provided by @docusaurus/core, which declares it as a direct dependency. However, this setup is theoretically not guaranteed to work, and it will certainly break in Yarn v2 pnp's strict mode. To understand why it's the issue, let me draw an layout of the node_modules.

In a package manager doesn't implement any hoisting optimization, the layout will be like

- node_modules
  - @docusaurus/core
    - node_modules
      - webpack
  - @docusaurus/theme-classic
    - index.js (that requires `webpack`)

Therefore, when index.js require webpack it will fail, because it doesn't exist in these paths:

  • project-root/@docusaurus/theme-classic/node_modules
  • project-root/@docusaurus/node_modules
  • project-root/node_modules

The current setup happens to work because popular package managers implement hoisting optimization, which results in a layout like

- node_modules
  - webpack
  - @docusaurus/core
  - @docusaurus/theme-classic
    - index.js (that requires `webpack`)

Under yarn's pnp mode, things will break. Although Yarn knows that @docusaurus/theme-classic declares webpack as a peer dependency, it still doesn't know where to find it.

The Fix

I think the code author's intention is to use the webpack provided by @docusaurus/core. Therefore, I decide to make this more explicit by using Module.createRequire, which can be understood by Yarn's pnp runtime. I discussed how createRequire works in #2789.

Have you read the Contributing Guidelines on pull requests?

Yes.

Test Plan

Everything can still be properly loaded, and the preview site still works.

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

@SamChou19815
SamChou19815 requested a review from lex111 as a code owner May 22, 2020 19:28
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 22, 2020
@docusaurus-bot

Copy link
Copy Markdown
Contributor

@yangshun
yangshun merged commit 85a6944 into meta:master May 22, 2020
@yangshun

yangshun commented May 22, 2020 •

Copy link
Copy Markdown
Contributor

@SamChou19815 I'd like to have most of the Yarn v2 pnp compatibility completed before we launch the next alpha version. Do you know what else is needed?

@SamChou19815
SamChou19815 deleted the theme-classic-require-webpack-from-docusaurus-core branch May 22, 2020 20:59
@SamChou19815

Copy link
Copy Markdown
Contributor Author

@SamChou19815 I'd like to have most of the Yarn v2 pnp compatibility completed before we launch the next alpha version. Do you know what else is needed?

All the changes that need to happen in docusaurus core is done. There is one remaining piece: webpack module resolution. Webpack v4 doesn't support pnp natively, but can be made to support it via a plugin. (See https://yarnpkg.com/features/pnp#support-via-plugins).

Fortunately, this can be already done in the user land via docusaurus plugins, like I did for my personal repo: https://github.com/SamChou19815/website/pull/528/files.

@yangshun I wonder what's your opinion on this? There are two good options available:

@yangshun

Copy link
Copy Markdown
Contributor

Thanks for the explanation! I'd go for adding to @docusaurus/core if there are negligible costs to non-pnp users in doing so. What do you recommend?

@SamChou19815

Copy link
Copy Markdown
Contributor Author

Thanks for the explanation! I'd go for adding to @docusaurus/core if there are negligible costs to non-pnp users in doing so. What do you recommend?

I recommend adding it to core. The size of pnp-webpack-plugin is very small.

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