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(v2): allow extend PostCSS config #4185

Merged
merged 2 commits into from
Feb 9, 2021
Merged

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Feb 6, 2021

Motivation

Preliminary implementation of a hook for quickly expanding the PostCSS loader (mainly for adding new PostCSS plugins).

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Tests.

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

@lex111 lex111 added the pr: new feature This PR adds a new API or behavior. label Feb 6, 2021
@lex111 lex111 requested a review from slorber as a code owner February 6, 2021 19:22
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Feb 6, 2021
@netlify
Copy link

netlify bot commented Feb 6, 2021

[V1] Deploy preview success

Built with commit 10e843c

https://deploy-preview-4185--docusaurus-1.netlify.app

@netlify
Copy link

netlify bot commented Feb 6, 2021

Deploy preview for docusaurus-2 ready!

Built with commit 10e843c

https://deploy-preview-4185--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Feb 6, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟢 Performance 92
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

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

@github-actions
Copy link

github-actions bot commented Feb 6, 2021

Size Change: +12 B (0%)

Total Size: 156 kB

ℹ️ View Unchanged
Filename Size Change
website/build/assets/css/styles.********.css 17.6 kB +11 B (0%)
website/build/assets/js/main.********.js 109 kB +15 B (0%)
website/build/blog/2017/12/14/introducing-docusaurus/index.html 21.7 kB -6 B (0%)
website/build/docs/introduction/index.html 180 B 0 B
website/build/index.html 6.94 kB -8 B (0%)

compressed-size-action

@@ -175,6 +176,33 @@ export function applyConfigureWebpack(
return config;
}

export function applyConfigurePostCss(
Copy link
Collaborator

Choose a reason for hiding this comment

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

was wondering if we need a new lifecycle for this case?

Maybe that should be part of configureWebpack? (as the post css loader is part of the webpack config?)

No strong opinions for now, just thinking out loud

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, PostCSS plugins can be added via configureWebpack hook, but for this we need to duplicate a lot Docusaurus core code related to webpack, for example from this comment:

image

(Since this hook should return a valid webpack config, which will be merged with the default config).

Thus, configurePostCss is a handy and useful shortcut that you can use to quickly add the required PostCSS plugins. Or do you know a better way to achieve this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks @lex111 , understand, and it will be useful for the upcoming tailwind theme too

What I see is that Gatsby does something similar with a plugin: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-postcss/src/gatsby-node.js

And next does not really have a plugin system, but allow a local postcss.config.js and will support tailwind postcss enhancements in core soon: vercel/next.js#20030.

That does not look viable for our usecase so the most convenient is indeed a new lifecycle as themes can enhance the existing config.

Going to finish your implementation and merge, I suspect there is a little bug for which I want to have a test, and will also try to make the loader/option lookup more robust

if (isPostCssLoader(loader)) {
loader.options.postcssOptions = configurePostCss(
loader.options.postcssOptions,
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI this configurePostCss should be called inside the loop.

Fixed a bug where having multiple applyConfigurePostCss would override each others (ie if a user would add his own configurePostCss, it would override the configurePostCss provided by the classic theme and RTL would fail)

Also find that looking for the presence of postcssOptions is better than the json stringify

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excellent fix, thanks!

@slorber slorber merged commit 2fb642d into master Feb 9, 2021
@slorber slorber mentioned this pull request Feb 9, 2021
@lex111 lex111 deleted the lex111/extend-postcss branch February 19, 2021 18:20
@lex111 lex111 added this to the v2.0.0-alpha.71 milestone Mar 1, 2021
This was referenced Mar 10, 2021
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