Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make Docusaurus PnP strict mode compatible #6047

Merged
merged 28 commits into from
Dec 8, 2021
Merged

fix: make Docusaurus PnP strict mode compatible #6047

merged 28 commits into from
Dec 8, 2021

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Dec 4, 2021

Motivation

Close #2576. This PR did the following changes

  • Moved the getFileLoaderUtils function from core/webpack/utils to @docusaurus/utils so that the mdx loader doesn't depend on the core which will cause circular dependencies otherwise.
  • Made all Webpack loaders imported with require.resolve so that all the extra loader dependencies can be safely removed from the init templates.
  • Moved core/constants to @docusaurus/utils so that constants are not duplicated.
  • Made @docusaurus/core the peer dependency instead of hard dependency of any plugin.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Tested locally & updated the E2E test script

@Josh-Cena Josh-Cena added the pr: bug fix This PR fixes a bug in a past release. label Dec 4, 2021
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Dec 4, 2021
@netlify
Copy link

netlify bot commented Dec 4, 2021

✔️ [V2]

🔨 Explore the source changes: a700cf1

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/61ab35a4c5711b000749956d

😎 Browse the preview: https://deploy-preview-6047--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Dec 4, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟢 Performance 97
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-6047--docusaurus-2.netlify.app/

@github-actions

This comment has been minimized.

@facebook facebook deleted a comment from netlify bot Dec 4, 2021
@Josh-Cena Josh-Cena added the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Dec 4, 2021
@Josh-Cena Josh-Cena changed the title fix: make Docusaurus pnp strict mode compatible fix: make Docusaurus PnP strict mode compatible Dec 4, 2021
Josh-Cena and others added 2 commits December 6, 2021 09:47
Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com>
@Josh-Cena Josh-Cena linked an issue Dec 7, 2021 that may be closed by this pull request
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, that's really great to get rid of these cycles!

Just asked a few questions to be sure

.github/workflows/tests-e2e.yml Outdated Show resolved Hide resolved
@@ -12,17 +12,17 @@ exports[`transformImage plugin pathname protocol 1`] = `
exports[`transformImage plugin transform md images to <img /> 1`] = `
"![img](https://example.com/img.png)

<img src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./static/img.png\\").default} />
<img src={require(\\"![CWD]/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=[CWD]/node_modules/file-loader/dist/cjs.js!./static/img.png\\").default} />
Copy link
Collaborator

Choose a reason for hiding this comment

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

wonder if this snapshot is normal? What is this new syntax?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's just require.resolve('url-loader') + replacing CWD with placeholder so the absolute path isn't hardcoded in the snapshot

return result.toString();
return result
.toString()
.replace(new RegExp(posixPath(process.cwd()), 'g'), '[CWD]');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand that part.
Is the goal to make tests insensitive to local FS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, indeed

@@ -7,7 +7,7 @@

import {PluginOptions, RedirectOption, UserPluginOptions} from './types';
import {Joi, PathnameSchema} from '@docusaurus/utils-validation';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was wondering if we shouldn't have a dedicated @docusaurus/constants package? any thought 🤷‍♂️ ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmmm, I don't like little packages everywhere... @docusaurus/utils serves a very clear purpose as a general util to be consumed by any Node code, so I think it makes sense to put server-side constants here

@@ -29,10 +28,12 @@
"react-live": "2.2.3"
},
"devDependencies": {
"@docusaurus/core": "2.0.0-beta.9",
Copy link
Collaborator

Choose a reason for hiding this comment

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

how do you choose if it's needed in devDependencies? If there are tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a hack so that lerna correctly figures out the dependency graph and builds the core before building those that still implicitly depend on the core's internal APIs (that's still bad, we need to further refactor that)

@slorber
Copy link
Collaborator

slorber commented Dec 8, 2021

LGTM 👍

@slorber slorber merged commit e07ebad into main Dec 8, 2021
@slorber slorber deleted the jc/yarn-berry branch December 8, 2021 13:26
@Josh-Cena Josh-Cena removed the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Dec 8, 2021
@Josh-Cena
Copy link
Collaborator Author

Okay, this is slightly problematic 😅 @slorber lerna doesn't seem to bump peerDeps during publish: lerna/lerna#955 this doesn't cause installation errors but still triggers some warnings. Maybe we want to use caret ranges for peerDeps?

@slorber
Copy link
Collaborator

slorber commented Dec 9, 2021

I'm not sure to know what to do here

What kind of warning will users have? Will all package managers work?

I've seen some tools using '*' for peerDeps, and we could use a semver range for sure, but maybe fixed versions are better to ensure correct version resolution in all cases, instead of leading to an error when trying to run Docusaurus?

React seems to use a fixed version like us for react/react-dom: https://github.com/facebook/react/blob/main/packages/react-dom/package.json

🤷‍♂️

What is the advantages of using peerDeps in this case compared to what we had before? Does reverting this change prevent the usage of PnP strict mode?

(You see, this is exactly why changes like upgrading to Yarn 3 can unexpectedly become a time sink 😄)

@Josh-Cena
Copy link
Collaborator Author

What kind of warning will users have? Will all package managers work?

➤ YN0060: │ website@workspace:. provides @docusaurus/core (pade89) with version 2.0.0-beta.10, which doesn't satisfy what @docusaurus/preset-classic and some of its descendants request

This is for a project on Yarn v3.

warning " > @docusaurus/theme-live-codeblock@2.0.0-beta.10" has incorrect peer dependency "@docusaurus/core@2.0.0-beta.9".

This is in our own repo. (The only Docusaurus project on Yarn v1 that I have.)

It doesn't prevent PnP; it just makes package managers frown. I haven't seen any of them error. But I know that pnpm is stricter about peer deps and they may be unhappy.

I've seen some tools using '*' for peerDeps, and we could use a semver range for sure, but maybe fixed versions are better to ensure correct version resolution in all cases, instead of leading to an error when trying to run Docusaurus?

Yes, a fixed peer dependency does make sense; the only problem being that lerna refuses to bump peer dependencies, which I can also sympathize with. Still, we have runtime checks so it's not strictly necessary.

What is the advantages of using peerDeps in this case compared to what we had before? Does reverting this change prevent the usage of PnP strict mode?

No; I'm fine with reverting back to hard dep. As I said, it's just semantically better because plugins should work in isolation from the core.

(You see, this is exactly why changes like upgrading to Yarn 3 can unexpectedly become a time sink 😄)

It's not about Yarn v3, but exactly about lerna which we hope to ditch in the Yarn v3 migration. Basically, what package manager we use should minimally affect the end users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
4 participants