Closed Velenir closed 7 years ago
Yeah, great point. If I remember right, postcss.config.js
supports a function format. If we can get enough context information through that, we could handle specialization there. If the data we need for that isn't there yet, I assume it would be possible to add.
You can see the format here. Here's the relevant portion:
module.exports = (ctx) => ({
parser: ctx.sugar ? 'sugarss' : false,
map: ctx.env === 'development' ? ctx.map : false,
from: ctx.from
to: ctx.to
plugins: {
'postcss-plugin': ctx.plugin
}
})
So this could be it.
Thank you for the link. I've looked through it, and it seems to me that we can't pass parameters directly to context
as it is handled by webpack. Maybe we could use LoaderOptionsPlugin
but we really shouldn't.
Adding something like
rules: [
{
test: /\.s?css$/,
loader: 'postcss-loader',
lint: true,
enforce: 'pre'
}
]
breaks validation. So the only way to pass parameter is through ?params
or options: {}
. Then we can access query
and options
on the loader in context
(where query === (options ? "?"+options : "")
). At least I can't think of another way.
Yeah, validation is very strict. We would need a smart way to tell the loaders apart somehow without relying on custom fields as webpack chokes on those. options
sounds like a potential way out to me.
I guess we need to escalate this issue to postcss-loader
as it's quite possible this hasn't been solved properly yet.
@Velenir I set up https://github.com/postcss/postcss-loader/issues/158 if you want to follow/comment.
Looks like this will be solved by postcss-loader once they implement config
option. So give it a week. 👍
Glad to hear it. Thanks for all your help!
This should be resolved at dev
now. I ended up going through options
and plugins
(it's important to set ident
at the moment for this to work!).
Thank you very much for that! I would have preferred some kind of simpler specialization in postcss.config.js
, but this way is good too.
postcss.config.js
might be possible too. It depends on what kind of context information we have available there. There could be more than there used to be. That would allow branching.
I think it would be a good idea to cover the case of including
postcss-loader
multiple times in therules
-- e.g. withstylelint
plugin as a preloader and withautoprefixer
in css/sass/less processing rule. To illustrate, given:and
Both
autoprefixer
andstylelint
plugins get applied on eachpostcss-loader
use with unpredictable results (mostly just warnings fromstylelint
for compressed .scss files).Do you solve this somehow? In webpack 1 I used to include
postcss
config directly in themodule
and specialize in theloader: 'postcss?pack=lint'
, but it's now deprecated.In webpack 2 I specialize like this:
and return different plugins depending on context passed to postcss:
I'm not sure if this is the most efficient way to do it but it works.