-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
gatsby-plugin-postcss #6217
gatsby-plugin-postcss #6217
Conversation
mdreizin
commented
Jun 28, 2018
•
edited
Loading
edited
- Add migration guide from v1 to v2
- Add more examples how to use it
- Add @KyleAMathews as an owner on NPM
Deploy preview for using-drupal ready! Built with commit 72060e6 |
Deploy preview for gatsbygram ready! Built with commit 72060e6 |
@KyleAMathews Did you have a chance to look at this PR? Maybe, you have some comments and ideas and I'm open to discussion :) |
Is it reasonable to remove For instance, add migration guide to use this plugin and add missing plugins (flexbugs, autoprefixer). |
This looks good!
This appeals to me, but from a beginner perspective I think we should keep It looks like enabling this plugin will disable any existing PostCSS config? That seems like a good approach too. Enabling |
I don't think this is a good idea, that will remove autoprefixing from folks using sass or less as well. We chatted a bit about this in the PostCSS config PR, Postcss is used in two different capacities, this one is as a preprocesser replacement, and others as tooling around browser compat. It may be fine to remove the flexbugs (its a bit prescriptive) plugin from the default, but i don't think autoprefixer should be removed :) |
const isSSR = stage.includes(`html`) | ||
const originalConfig = getConfig() | ||
|
||
originalConfig.module.rules = originalConfig.module.rules.filter(rule => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be unexpected behavior to me, plguins should be additive here I think, I would really dislike if if this plugin removed my loaders elsewhere, especially since this doesn't always replace it with something else.
e.g. if I have gatsby-plugin-sass
installed with a postcss config meant to augment my sass workflow, this plugin would remove that, and then replace it with a loader that doesn't handle .scss
files.
at the very least this should only target the gatsby css-loader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jquense Great catch, I will fix that.
Are you agree to have I'm just asking, because now we can't use the followings: module.exports = {
plugins: [
{
resolve: `gatsby-plugin-postcss`,
options: {
plugins: () => [require(`postcss-preset-env`)({ stage: 0 })]
},
},
],
} due to limitation of |
maybe |
Only if someone is using both the postcss and sass/less plugin though? In which case it'ss fairly straightforward – if you use the postcss plugin (in any capacity), then you configure your own autoprefixer, doesn't matter what other plugins you also use. |
I agree with removing the default autoprefixer plugin when using the postcss plugin. I’d like to be able to control what version of autoprefixer is being used and what settings are passed to it (e.g. turning grid on/off), so removing the default configuration seems like the cleanest approach. As @seaneking points out, this would only affect those using the postcss plugin and its docs could mention the clean slate and the need to add your own autoprefixer configuration. |
@jquense did you have a chance to review it? |
There is no need to remove the auto prefixing, adding this plugin will already override the default css handling since it will run before it. All I'm concerned with is that this plugin doesn't break behavior for unrelated other plugins 👍 since that is both unexpected and likely to lead to bugs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great, just a few notes!
return options | ||
} | ||
|
||
const removeBuiltInCssLoaders = config => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There shouldn't be a need for this. This plugin will already intercept all css files and run the through producing the extracting file or tye inlined style before the built-in rule can run. I'd say trying to not mess with the config is safer long term and in more use cases than effectively guessing which loader is the built in one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jquense I completely agree with you.
Unfortunately, if we have i.e. two rules with the same test
matchers they will be applied in the order they are defined in module.rules
:
module: {
rules: [{
test: /\.css$/,
use: []
}, {
test: /\.css$/,
use: []
}]
}
If we don't remove them we will get:
Error: ./src/components/layout/index.css
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/l oader.js):
Error: Didn't get a result from child compiler
Now we have a couple of options:
- Remove existing
css
rules manually - Remove existing
css
rules from the core completely and tell users to use that plugin - Remove existing
css
rules from the core and move their defaults into that plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm loaders shouldn't be run in order of defining, but in reverse order (see https://stackoverflow.com/questions/32234329/what-is-the-loader-order-for-webpack) I'd like to take a moment to understand what's happening let me try and do some debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Gatsby is always (?) using oneOf
for handling CSS, to prevent *.module.css
files from matching both module and non-module CSS rules. Would it be possible to prepend these new rules before the existing ones? The end result would look like:
oneOf: [
postcssModuleRule,
postcssRule,
rules.cssModules(),
rules.css()
]
This way, the default rules would never match anyway, because the prepended ones use the same regexes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szimek I tried your approach and it worked fine. Thanks! I will publish an update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good approach for now 👍
{ actions, stage, loaders, getConfig }, | ||
pluginOptions | ||
) => { | ||
const isProduction = stage !== `develop` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two develop stages, develop and develop-html
@mdreizin It works! Thank you! |
Personally I'd vote strong no on |
@seaneking I agree with you. PS: It was just a thought came to my mind and I decided to share with you. It could help to simplify this plugin. |
@mdreizin I'd prefer to keep using |
@szimek @seaneking @jquense @m-allanson As I can see it is the final version and I would kindly ask you to review it again. If everything is ok let's merge it 🎉. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mdreizin I've just been trying this out locally and it's looking great!
Could you add m-allanson
and pieh
as package maintainers on npm? Also kylemathews
but I think you've done that already?
Once that's done, this can be merged and published 🎉.
Big thanks to everyone that's helped out with this. Now I'll be able to upgrade a couple of my sites to Gatsby v2 :)
Having another read through - I find the readme a bit confusing. Should the config go in Under the How to Use section it says "If you need to pass options to PostCSS use the plugins options; see postcss-loader for all available options." and then says that you should create a Later on there's another section about additional post processing. Is that separate from the previous section? I'd maybe emphasise putting stuff in |
@m-allanson I have just added you and also I will try to revise the readme and fix existing issues. |
@m-allanson Done. It is ready for the review :) |
Deploy preview for using-postcss-sass failed. Built with commit 72060e6 https://app.netlify.com/sites/using-postcss-sass/deploys/5b50f06fb13fb17597b6528e |
docs/docs/migrating-from-v1-to-v2.md
Outdated
Use [`onCreateWebpackConfig`](/docs/add-custom-webpack-config) to specify your postcss plugins. | ||
|
||
Note: there will be a `postcss` plugin that allows you to configure postcss from a standard postcss config file. [Follow this discussion on issue 3284](https://github.com/gatsbyjs/gatsby/issues/3284). | ||
Use [`gatsby-plugin-postcss`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-postcss) to specify your particular PostCSS configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the postcss configuration to replicate how Gatsby v1 is setup? That'll make it really easy to migrate. So the instructions for the npm install + the postcss.config.js file to add.
postcss(wp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews Yes, sure. Thanks for the suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews Done. Could you please to review it again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work @mdreizin! This is a huge contribution for adding a really key component to the ecosystem!
Merging and publish 🎉
Deploy preview for using-remark failed. Built with commit 72060e6 https://app.netlify.com/sites/using-remark/deploys/5b50f070b13fb17597b6529a |
Oh, as soon as TravisCI is happy that is :-) |
Holy buckets, @mdreizin — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
💥 |
And definitely claim your free swag @mdreizin! You deserve 3 or 4! |
@KyleAMathews Thanks a lot! It is my pleasure to contribute. In case of free time I will try to help you guys with v2. |
Hi all. I'm trying to get this working with an existing build with file.module.scss. Am I correct in saying that this is supposed to replace "gatsby-plugin-sass"? If so, what .scss processor should I be using and what is the syntax for including it? The only one I've been able to find is this: https://github.com/jonathantneal/postcss-sass But the syntax doesn't really seem to match what is needed by the plugin array. The thing I really want to get working is the pxtorem converter: https://github.com/cuth/postcss-pxtorem for pinch to zoom issues. I'm quite new to this part of the ecosystem, so would appreciate some help. |
@fardarter The main idea behind this plugin is to provide convenient way to process |
What I'm really hoping for is a pipeline where I can pass SASS output
through PostCSS.
…On Tue, 24 Jul 2018 at 22:19, Marat Dreizin ***@***.***> wrote:
@fardarter <https://github.com/fardarter> The main idea behind this
plugin is to provide convenient way to process *.css files throught
PostCSS and this is not a replacement for gatsby-plugin-sass. If you
would like to use Sass you should to use gatsby-plugin-sass.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6217 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ALYpBW-qzmB1HJdQ_zXwIUTwf9ITq0KQks5uJ4E_gaJpZM4U8CCQ>
.
|