postcss-loader: Doesn't work with webpack 2
Hello guys. Looks like it doesn’t works with the latest webpack 2 beta.
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader!postcss-loader' })
},
Then I ran webpack-dev-server and it doesn’t finish.
Some guys have trouble too here https://github.com/webpack/webpack/issues/2812
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 67 (17 by maintainers)
@panjiesw You’ve pretty much got it. Here’s an excerpt from my
webpack.config.js
. Hopefully anyone else looking for how to getwebpack@2
to play nice withpostcss-loader
will see this because I had to spend several hours crawling github seeing aswebpack@2
is in beta there’s no documentation. I have tested this, and at of the time I’m posting this, using the latest releases, it works, but may require some tweaking to fit into your setup.No docs, it’s webpack you know. Anyway I found this, take a look please at loaders section https://gist.github.com/sokra/27b24881210b56bbaff7
@StevenIseki that doesn’t work for me too. This is the only way I managed to make it work using Css Modules, ExtractTextPlugin and the Autoprefixer plugin.
postcss.config.js
webpack.config.js
package.json
@StevenIseki
loaders
->rules
,loader
->use
,LoaderOptionsPlugin
was a ‘polyfill’ required inwebpack =< 2.1.0-beta.24
and is unnessecary beginning from2.1.0-beta.25
. This is the final syntax for webpack@2The loader configuration in webpack 2 itself has changed, which, i think, requires you to set the loader name without the query string.
So maybe you should change the config to this (untested):
I’m not so sure about the order, but you get the idea.
@francoisromain
but not atm 😛 (v2-rc*), wait until v2@latest is out
@edmundo096 webpack v2.2.1 ships the ident fix, it’s finally working now
@StevenIseki , @michael-ciniawsky : Came up against this issue with the postcss-loader blowing up when I tried to pass it the autoprefixer plugin as well. Found a solution that worked for me at the bottom of this thread: -
Hadn’t seen the use of
ident
in this thread yet so I figured I’d post this here in case anyone else finds themselves here after googling this problem.Also, I didn’t want another smelly config file in my repo root… 😜
Webpack docs regarding Complex Options
I am using the following setup for
postcss-loader
with webpack 2 🔽 The important part isnew webpack.LoaderOptionsPlugin({ options: { postcss: [ autoprefixer ] } })
in plugins and!postcss-loader
in css-loader.So basically, what I conclude after reading and some researching, there are 4 ways (as for now) to load the PostCSS configuration for Webpack 2:
postcss.config.js
file, which must be “anywhere down the file tree” where Webpack matches a file (e.g. if a match was on a\assets\stylesheets
file,postcss.config.js
must be anywhere inside that path). This is due how postcss-loader works. It uses postcss-load-config module, which uses cosmiconfig module (see more in davidtheclark/cosmiconfig). That is why there is almost no documentation about where one can place that file. Placing it on the root almost assures thatpostcss.config.js
will just work, although this is not prefered by everybody.As a side note, since this depends mainly on cosmiconfig we also have other 3 ways here:
ident
on the options object (as @owenDods commented). Unnecessary in the in Webpack >= 2.2.1, see point 4webpack.LoaderOptionsPlugin
. This makespostcss-loader
load the options (and plugins) as if we were using Webpack 1. See more here https://webpack.js.org/plugins/loader-options-plugin/The last 2 are handled respectively by this line of code:
var options = params.plugins || loader.options.postcss;
in the index.js#L48. All of this 3 proaches support an array of plugins.Maybe documentating or mentioning this 3 ways in the Webpack 2 section on the README.md would be nice in my opinion @ai
Hope this helps someone else (avoiding possible headaches) in the future.
EDIT: Update as for Webpack
>= v2.2.1
ident
workaround for webpack <= 2.2.0).For future lost soul who is also stuck with webpack.
For those who want to use post-css, extract-text-plugin, resolve-url-loader, css-module and react-css-module with webpack 2 and source map enable Here is how
package.json
webpack.config.babel.js
postcss.config.js
.babelrc
For those who don’t use url-resolve you can remove this
webpack.config.babel.js
For those who wonder why my webpack config file is
webpack.config.babel.js
notwebpack.config.js
is because I use
es6
syntax in webpack config, I had to name my configwebpack.config.babel.js
so webpack would understand the syntax.@koutsenko
@ai, yeah, didn’t liked the
ident
neither. More like a workaround and took me several Google searches to find it as a mere “workaround solution”, specially because it doccumented with intention for the Loader devs (I skipped it haha, https://webpack.js.org/guides/migrating/#loader-changes)Have not digged more into it, but… There is any way that we could use options normally (without the ident)? Something like:
It’s something that could be fixed on PostCSS-loader? (or it’s something from Webpack 2 itself?) Or which of the ways do you recommend us using?
@owenDods 👍 but this is going away in webpack >= v2.3.x (next v2 minor), after talking with sokra about it #4109, the ident will be added by webpack and no config step required for complex options anymore. Also the reason this was not documented until yet 😛
Yep, and the config structure is the same:
I also find that I have to give
ident: 'something'
to make@import
work:with test.css:
leads to:
With
ident: 'remove-this-and-it-fails'
uncommented it works.Here’s a complete example: https://github.com/AndreKR/issue-webpack-postcss-ident
Just spent some time fixing this issue in my react boilerplate.
Check this commit
I had to move my postcss plugins to a
postcss.config.js
. Also had to return an object in that file to getpostcss-cssnext
to work with browser settings.Then had a weird issue with a production build if I use
options
instead ofquery
for my.css
files.@michael-ciniawsky What is
[...plugins]
? I get the errorPostCSS Config could not be loaded. Please check your PostCSS Config
Here is my config…
Okay here we go
Changing
query
tooptions
failsremoving
resolve-url-loader
failsPostCSS options inside. also fails
without ETWP also fails
without ETWP + ident = Works
You can have a look at this with the real files i am using here: https://github.com/milewski/portfolio
@gazpachu I got the same issue. After some research, I concluded how the loading of
postcss.config.js
worked.I recommend you read my suggestions in the comment above https://github.com/postcss/postcss-loader/issues/92#issuecomment-276036682. Summed up, if you use
postcss.config.js
, it must be “anywhere down the file tree” where Webpack matches a file (in your case, of/src/app/components/bundle.scss
file,postcss.config.js
must be anywhere inside that path; using the same folder level where thewebpack.config.js
file is located, as you said, wouldn’t work).Otherwise, you should pass the options by any of other ways used by cosmiconfig, or indicate its location by the query params
?config
as you have used, which I believe is processed at https://github.com/postcss/postcss-loader/blob/master/index.js#L54For the prefixes, Autoprefixer applies it depending the current browser use. See the README first lines of https://github.com/postcss/autoprefixer.
@gazpachu dependant on what browsers you have defined. Add some older browsers and it should prefix as required
Finally I had something working (with webpack 2.2.1. and extract text plugin @2.0.0-beta). I’ll put it here:
@edmundo096 It’s a webpack core issue, which can’t be fixed within loaders (the query is already malformed when the loader receives it), I addressed it to sokra and it will be fixed in webpack v2.3+, see the PR I linked above for more info, it basically has to do with how webpack stringifies the loader query. Your example is the desired syntax and will be working in webpack v2.3+
Ah I see the execution order is different - for me postcss runs first, css-loader second. Maybe that’s causing an issue on your end if css-loader is producing JS, and later post-css loader is expecting css input?