Skip to content

Commit 384401a

Browse files
committed
Check if postcss-load-config will resolve config if not use default options
- Update post-processing-css.md - Add test - Add postcss-load-config as react-scripts dependency
1 parent f5c3bdb commit 384401a

File tree

10 files changed

+86
-14
lines changed

10 files changed

+86
-14
lines changed

docusaurus/docs/post-processing-css.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,19 @@ If you need to disable autoprefixing for some reason, [follow this section](http
4242
If you'd like to opt-in to CSS Grid prefixing, [first familiarize yourself about its limitations](https://github.com/postcss/autoprefixer#does-autoprefixer-polyfill-grid-layout-for-ie).
4343

4444
To enable CSS Grid prefixing, add `/* autoprefixer grid: autoplace */` to the top of your CSS file.
45+
46+
### Customizing PostCSS Config
47+
48+
However, if you want to add custom plugins, add a `postcss.config.js` file to the root of your project. It will replace the internal CRA postcss plugins.
49+
50+
You can read more about common PostCSS Config [here](https://github.com/michael-ciniawsky/postcss-load-config).
51+
52+
```js
53+
module.exports = {
54+
parser: 'sugarss',
55+
map: false,
56+
plugins: {
57+
'postcss-plugin': {},
58+
},
59+
};
60+
```

packages/react-scripts/config/webpack.config.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'
3636
// @remove-on-eject-begin
3737
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
3838
// @remove-on-eject-end
39-
const postcssNormalize = require('postcss-normalize');
39+
const postcssLoadConfig = require('postcss-load-config');
40+
41+
const hasPostcssConfig = (() => {
42+
try {
43+
return !!postcssLoadConfig.sync();
44+
} catch (_error) {
45+
return false;
46+
}
47+
})();
4048

4149
const appPackageJson = require(paths.appPackageJson);
4250

@@ -110,19 +118,23 @@ module.exports = function (webpackEnv) {
110118
// Necessary for external CSS imports to work
111119
// https://github.com/facebook/create-react-app/issues/2677
112120
ident: 'postcss',
113-
plugins: () => [
114-
require('postcss-flexbugs-fixes'),
115-
require('postcss-preset-env')({
116-
autoprefixer: {
117-
flexbox: 'no-2009',
118-
},
119-
stage: 3,
120-
}),
121-
// Adds PostCSS Normalize as the reset css with default options,
122-
// so that it honors browserslist config in package.json
123-
// which in turn let's users customize the target behavior as per their needs.
124-
postcssNormalize(),
125-
],
121+
...(hasPostcssConfig
122+
? {}
123+
: {
124+
plugins: () => [
125+
require('postcss-flexbugs-fixes'),
126+
require('postcss-preset-env')({
127+
autoprefixer: {
128+
flexbox: 'no-2009',
129+
},
130+
stage: 3,
131+
}),
132+
// Adds PostCSS Normalize as the reset css with default options,
133+
// so that it honors browserslist config in package.json
134+
// which in turn let's users customize the target behavior as per their needs.
135+
require('postcss-normalize')(),
136+
],
137+
}),
126138
sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
127139
},
128140
},

packages/react-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"pnp-webpack-plugin": "1.6.4",
6565
"postcss-flexbugs-fixes": "4.2.1",
6666
"postcss-loader": "3.0.0",
67+
"postcss-load-config": "2.1.0",
6768
"postcss-normalize": "8.0.1",
6869
"postcss-preset-env": "6.7.0",
6970
"postcss-safe-parser": "4.0.2",

test/fixtures/postcss-config/.disable-pnp

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`builds with custom postcss config 1`] = `
4+
"body{background:#056ef0}
5+
/*# sourceMappingURL=main.9b96d08a.chunk.css.map */"
6+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const testSetup = require('../__shared__/test-setup');
2+
3+
const fs = require('fs-extra');
4+
const globby = require('globby');
5+
const path = require('path');
6+
7+
test('builds with custom postcss config', async () => {
8+
await testSetup.scripts.build();
9+
10+
const buildDir = path.join(testSetup.testDirectory, 'build');
11+
const cssFile = path.join(
12+
buildDir,
13+
globby.sync('**/*.css', { cwd: buildDir }).pop()
14+
);
15+
16+
expect(fs.readFileSync(cssFile, 'utf8')).toMatchSnapshot();
17+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"react": "latest",
4+
"react-dom": "latest",
5+
"postcss-simple-vars": "latest"
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: [require('postcss-simple-vars')],
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$blue: #056ef0;
2+
3+
body {
4+
background: $blue;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './index.css';
4+
5+
ReactDOM.render(<div />, document.getElementById('root'));

0 commit comments

Comments
 (0)