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

feat(core): allow plugin lifecycles to return relative paths #6921

Merged
merged 5 commits into from
Mar 16, 2022

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Mar 16, 2022

Motivation

This is yet another effort to migrate to ES modules. #6520

Being able to resolve relative paths means we no longer rely on the __dirname variable, which doesn't exist in ESM contexts. It also gives plugin authors more flexibility in how they write their code.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Converted all plugins to return relative paths

@Josh-Cena Josh-Cena added the pr: new feature This PR adds a new API or behavior. label Mar 16, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 16, 2022
@Josh-Cena Josh-Cena mentioned this pull request Mar 16, 2022
1 task
@Josh-Cena Josh-Cena changed the title feat(core): resolve plugin lifecycles returning relative paths feat(core): allow plugin lifecycles to return relative paths Mar 16, 2022
@netlify
Copy link

netlify bot commented Mar 16, 2022

✔️ [V2]

🔨 Explore the source changes: ca2e6c9

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

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

@github-actions
Copy link

github-actions bot commented Mar 16, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 61
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

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

@github-actions
Copy link

github-actions bot commented Mar 16, 2022

Size Change: 0 B

Total Size: 792 kB

ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 49.9 kB
website/build/assets/css/styles.********.css 105 kB
website/build/assets/js/main.********.js 599 kB
website/build/index.html 38.7 kB

compressed-size-action

@netlify
Copy link

netlify bot commented Mar 16, 2022

✔️ [V2]

🔨 Explore the source changes: f20cba2

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

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

@slorber
Copy link
Collaborator

slorber commented Mar 16, 2022

Being able to resolve relative paths means we no longer rely on the __dirname variable, which doesn't exist in ESM contexts.

Does this mean that all third-party plugin authors would have to do the same too?

Or is it just for our own conversion to ESM?

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.

👍 agree to do this but I think there's value ensuring retrocompatibility so that it doesn't break our plugin ecosystem

What about adding a dogfood plugin that loads some client modules using an absolute path for example?

/**
* The absolute path to the folder containing the entry point file.
*/
readonly path: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe having a more explicit name would be better than a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's explicit enough. The only reason I added the comment is because I would add a comment to all path-related variables in the future. Paths are always ambiguous and it's not really worth making the name super long.

@@ -8,5 +8,6 @@
module.exports = function() {
return {
name: 'plugin-empty',
path: __dirname,
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure to understand why it's needed in the fixtures?

Fixtures should rather look like userland code and I don't think we want to ask users to provide this path?

Maybe something like baseResolvePath or something? 🤷‍♂️ not easy

Copy link
Collaborator Author

@Josh-Cena Josh-Cena Mar 16, 2022

Choose a reason for hiding this comment

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

The fixture returns a InitializedPlugin, not a raw plugin. It's not strictly a userland fixture, just something that mocks an intermediate value in our loading process 😅 Legacy test, don't want to touch much. I would probably refactor the core code and tests later

* Different from pluginModule.path, this one is always an absolute path used
* to resolve relative paths returned from lifecycles
*/
path: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

also find a better attribute name?

@Josh-Cena
Copy link
Collaborator Author

Or is it just for our own conversion to ESM?

It's primarily for us, but it also means community plugins can migrate to ESM more easily. They don't have to, though.

@Josh-Cena Josh-Cena merged commit 68aaf92 into main Mar 16, 2022
@Josh-Cena Josh-Cena deleted the jc/relative-theme-path branch March 16, 2022 12:47
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: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants