parcel: postcss not working with config file, but with postcss key in package.json only
π bug report
Parcel says you can provide postcss configs using .postcssrc (JSON), .postcssrc.js, or postcss.config.js.. But this does not just work! Parcel misses out on this file for some reason.
π Configuration (.babelrc, package.json, cli command)
So hereβs my project using parcel, and this is the tree structure:
$ tree -L 2 -a -I "dist|build|node_modules|\.cache|\.c9|\.git"
.
βββ .babelrc
βββ .eslintrc.js
βββ .gitignore
βββ .prettierrc.js
βββ .travis.yml
βββ LICENSE
βββ README.md
βββ client
βΒ Β βββ App.js
βΒ Β βββ App.sass
βΒ Β βββ __tests__
βΒ Β βββ components
βΒ Β βββ index.html
βΒ Β βββ index.js
βββ gulpfile.babel.js
βββ issue.md
βββ package.json
βββ postcss.config.js
βββ renovate.json
βββ server
βββ __tests__
βββ controller
βββ index.js
βββ models
βββ view
And this is postcss.config.js (I tried .postcssrc and .postcssrc.js as well, supposing the name might might mightβ¦ be a problem but it wasnβt)
module.exports = {
modules: true,
plugins: {
autoprefixer: true,
},
}
And package.json:
{
"name": "super-fullstack",
"version": "1.0.0",
"description": "react + express + nodejs + MongoDB boilerplate",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "parcel -p 8080 client/index.html",
"build": "NODE_ENV=production parcel -p 8080 build client/index.html --out-dir build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/9oelM/super-fullstack.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/9oelM/super-fullstack/issues"
},
"homepage": "https://github.com/9oelM/super-fullstack#readme",
"dependencies": {
"components": "^0.1.0",
"express": "^4.16.3",
"fetch-jsonp": "^1.1.3",
"http-proxy-middleware": "^0.19.0",
"normalize.css": "^8.0.0",
"parcel-bundler": "^1.10.2",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"sass": "^1.14.1"
},
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"autoprefixer": "^9.1.5",
"cssnano": "^4.1.4",
"eslint": "^5.6.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.11.1",
"gulp": "^4.0.0",
"gulp-eslint": "^5.0.0",
"gulp-nodemon": "^2.2.1",
"gulp-prettier": "^2.0.0",
"gulp-run-command": "0.0.9",
"postcss-modules": "^1.4.1"
}
}
π€ Expected Behavior && Current Behavior
So expectedly, according to what parcel says, itβs gotta process css codes to transform them. But it just doesnβt.
Hereβs what I did to test this:
- I installed
autoprefixerandpostcss-modules:
npm install --save-dev postcss-modules autoprefixer
and set config with postcss.config.js
- I wrote
App.sass:
div#hello-container
display: flex
justify-content: center
align-content: center
align-items: center
height: 100%
h1#hello
font-size: 50px
and I know itβs gotta be transformed to:
div#_hello-container_d8koz_1 {
display: -ms-flexbox;
display: flex;
-ms-flex-pack: center;
justify-content: center;
-ms-flex-line-pack: center;
align-content: center;
-ms-flex-align: center;
align-items: center;
height: 100%;
}
div#_hello-container_d8koz_1 h1#_hello_d8koz_1 {
font-size: 50px;
}
this when I run parcel.
- And obviosuly I made
App.js
import React from "react"
import "./App.sass"
import "normalize.css"
import Hello from "./components/Hello"
const App = () => (
<div id="hello-container">
<h1 className="hello">Hello from React</h1>
<Hello />
</div>
)
export default App
-
I ran
parcel client/index.html -
I checked the css bundle inside
distfolder and it was still:
div#_hello-container_d8koz_1 {
display: flex;
justify-content: center;
align-content: center;
align-items: center;
height: 100%;
}
div#_hello-container_d8koz_1 h1#_hello_d8koz_1 {
font-size: 50px;
}
which is obviously not transformed.
π Troubleshooting
Well, at least there is a workaround: adding postcss key to package.json.
So I edited package.json by adding postcss:
{
"name": "super-fullstack",
"version": "1.0.0",
"description": "react + express + nodejs + MongoDB boilerplate",
"main": "index.js",
...
"postcss": {
"modules": true,
"plugins": {
"autoprefixer": {
"browsers": [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9"
],
"flexbox": "no-2009"
}
}
}
}
and now, the transformation works again. Whatβs even more weird is that if you change the value of autoprefixer to true like
"autoprefixer": true,
or do something like
"autoprefixer": {
"grid": true
}
the transformation will not again work. What??!?
Please help me. I know it works with package.json but itβs just not pretty putting postcss key in there. I wanna keep it clean.
π» Code Sample
You can reproduce this problem in this way:
git clone https://github.com/9oelM/super-fullstack.git- edit
package.jsonto delete the wholepostcsskey and its value - run
parcel client/index.html - check the css bundle inside
distfolder.
π Your Environment
FYI, Iβm using c9.io.
| Software | Version(s) |
|---|---|
| Parcel | 1.10.2 |
| Node | v6.11.2 |
| npm/Yarn | 3.10.10 |
| Operating System | Linux [c9] #1 SMP Wed Aug 15 22:48:26 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 26 (4 by maintainers)
Just came across the same problem.
Apparently autoprefixer is only run when a browserslist config is available, because when I placed a
.browserslistrcin my projectβs dir or abrowserslistentry in package.json it started working.That would also explain why things worked for @9oelM when he added a
browsersfield in autoprefixerβs package.json entry.my parcel version is
1.10.3, have the same problem, after i delete the .cache, is workingI was pulling my hair out for a while. I really wanted to use tailwindcss with parcel but I just couldnβt edit the postcss config file. It really is working fine now because of the json only workaround. It just comes down to not being able to use a javascript config file for some reason, only json will work. It failed running a javascript config on both the latest version of parcel 1 and also on 2.0.0-beta.2.
Oh, actually, I can only get it to work when I use the key in package.json, or when I use
.postcssrc. It doesnβt work when the config file is javascript. It seems to open the file, but not use its contents. none of the calls torequire()are actually made, but I can see the output of console.log() when I call it at the top of postcss.config.js. Strangely it will only call the js file once, after that I have to clear the cache folder each time.I had the same issue as @pykenny where it would tell me something along the lines of
Uncaught Error: Cannot find module '<some_module_number>'. I realized I had the css file referenced in myindex.htmlfile which was no issue in Parcel v1 but caused this error with Parcel v2 (2.0.0-beta.1).I run into the same issue with the latest Parcel version. Using the
postcsskey in the package.json only works withmodules: trueplugins are ignored.