Skip to content

Commit 66d07da

Browse files
cl1ckgiuseppeg
authored andcommitted
Support PostCSS 8 (giuseppeg#43)
1 parent 231a54b commit 66d07da

File tree

10 files changed

+821
-438
lines changed

10 files changed

+821
-438
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.css

README.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,54 @@ With config:
3232
```json
3333
{
3434
"plugins": [
35-
["styled-jsx/babel", {
36-
"plugins": [
37-
[
38-
"styled-jsx-plugin-postcss",
39-
{
40-
"path":
41-
"[PATH_PREFIX]/postcss.config.js"
42-
}
35+
[
36+
"styled-jsx/babel",
37+
{
38+
"plugins": [
39+
[
40+
"styled-jsx-plugin-postcss",
41+
{
42+
"path": "[PATH_PREFIX]/postcss.config.js"
43+
}
44+
]
4345
]
44-
]
45-
}]
46+
}
47+
]
4648
]
4749
}
4850
```
4951

5052
### Example with CRA
5153

52-
Usage with Create React App requires you to either _eject_ or use [react-app-rewired](https://github.com/timarney/react-app-rewired).
54+
Usage with Create React App requires you to either _eject_ or use [react-app-rewired](https://github.com/timarney/react-app-rewired).
5355

5456
Here is an example using `react-app-rewired`:
5557

5658
```javascript
5759
// config-overrides.js
5860
// this file overrides the CRA webpack.config
5961

60-
const { getBabelLoader } = require('react-app-rewired')
62+
const { getBabelLoader } = require("react-app-rewired");
6163

62-
module.exports = function override (config, env) {
63-
const loader = getBabelLoader(config.module.rules)
64+
module.exports = function override(config, env) {
65+
const loader = getBabelLoader(config.module.rules);
6466

6567
// Older versions of webpack have `plugins` on `loader.query` instead of `loader.options`.
66-
const options = loader.options || loader.query
67-
options.plugins = [['styled-jsx/babel', {
68-
'plugins': ['styled-jsx-plugin-postcss']
69-
}]].concat(options.plugins || [])
70-
return config
71-
}
68+
const options = loader.options || loader.query;
69+
options.plugins = [
70+
[
71+
"styled-jsx/babel",
72+
{
73+
plugins: ["styled-jsx-plugin-postcss"],
74+
},
75+
],
76+
].concat(options.plugins || []);
77+
return config;
78+
};
7279
```
7380

7481
_Note: Please follow their instructions on how to set up build & test scripts, and make sure you have a correctly formatted `postcss.config.js` as well_.
7582

76-
7783
#### Notes
7884

7985
`styled-jsx-plugin-postcss` uses `styled-jsx`'s plugin system which is supported
@@ -82,13 +88,13 @@ from version 2. Read more on their repository for further info.
8288
## Plugins
8389

8490
`styled-jsx-plugin-postcss` uses
85-
[`postcss-load-plugins`](https://www.npmjs.com/package/postcss-load-plugins)
91+
[`postcss-load-config`](https://www.npmjs.com/package/postcss-load-config)
8692
therefore you may want to refer to their docs to learn more about
87-
[adding plugins](https://www.npmjs.com/package/postcss-load-plugins#packagejson).
93+
[adding plugins](https://www.npmjs.com/package/postcss-load-config#packagejson).
8894

8995
## Contributions
9096

91-
**PRs and contributions are welcome!**
97+
**PRs and contributions are welcome!**
9298

9399
We aim to drive development of this plugin through community contributions.
94100

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
plugins: ['mock-plugin']
3-
}
2+
plugins: ["mock-plugin"],
3+
};

fixture-postcss-plugin.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
const postcss = require('postcss')
1+
const postcss = require("postcss");
22

3-
module.exports = postcss.plugin('postcss-fixture', () => (root) => {
4-
console.warn('warn')
5-
console.error('error')
6-
return root
7-
})
3+
module.exports = (options = {}) => ({
4+
postcssPlugin: "postcss-csso",
5+
Once(root, { result, postcss }) {
6+
console.warn("warn");
7+
console.error("error");
8+
return root;
9+
},
10+
});
11+
12+
module.exports.postcss = true;

index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
const { spawnSync } = require('child_process')
2-
const path = require('path')
1+
const { spawnSync } = require("child_process");
2+
const path = require("path");
33

44
module.exports = (css, settings) => {
5-
const cssWithPlaceholders = css
6-
.replace(/%%styled-jsx-placeholder-(\d+)%%/g, (_, id) =>
7-
`/*%%styled-jsx-placeholder-${id}%%*/`
8-
)
5+
const cssWithPlaceholders = css.replace(
6+
/%%styled-jsx-placeholder-(\d+)%%/g,
7+
(_, id) => `/*%%styled-jsx-placeholder-${id}%%*/`
8+
);
99

10-
const result = spawnSync('node', [path.resolve(__dirname, 'processor.js')], {
10+
const result = spawnSync("node", [path.resolve(__dirname, "processor.js")], {
1111
input: JSON.stringify({
1212
css: cssWithPlaceholders,
13-
settings
13+
settings,
1414
}),
15-
encoding: 'utf8'
16-
})
15+
encoding: "utf8",
16+
});
1717

1818
if (result.status !== 0) {
19-
if (result.stderr.includes('Invalid PostCSS Plugin')) {
20-
console.error('Next.js 9 default postcss support uses a non standard postcss config schema https://err.sh/next.js/postcss-shape, you must use the interoperable object-based format instead https://nextjs.org/docs/advanced-features/customizing-postcss-config')
19+
if (result.stderr.includes("Invalid PostCSS Plugin")) {
20+
let isNext = false;
21+
try {
22+
require.resolve("next");
23+
isNext = true;
24+
} catch (err) {}
25+
if (isNext) {
26+
console.error(
27+
"Next.js 9 default postcss support uses a non standard postcss config schema https://err.sh/next.js/postcss-shape, you must use the interoperable object-based format instead https://nextjs.org/docs/advanced-features/customizing-postcss-config"
28+
);
29+
}
2130
}
2231

23-
throw new Error(`postcss failed with ${result.stderr}`)
32+
throw new Error(`postcss failed with ${result.stderr}`);
2433
}
2534

26-
return result.stdout
27-
.replace(/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g, (_, id) =>
28-
`%%styled-jsx-placeholder-${id}%%`
29-
)
30-
}
35+
return result.stdout.replace(
36+
/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g,
37+
(_, id) => `%%styled-jsx-placeholder-${id}%%`
38+
);
39+
};

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "styled-jsx-plugin-postcss",
3-
"version": "3.0.2",
3+
"version": "4.0.0",
44
"description": "Plugin to add PostCSS support to styled-jsx",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha test.js"
7+
"test": "mocha --timeout 10000 test.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -22,14 +22,17 @@
2222
},
2323
"homepage": "https://github.com/giuseppeg/styled-jsx-plugin-postcss#readme",
2424
"dependencies": {
25-
"postcss": "^7.0.2",
26-
"postcss-load-plugins": "^2.3.0"
25+
"postcss-load-config": "^2.1.2"
2726
},
2827
"devDependencies": {
29-
"mocha": "^5.2.0",
30-
"postcss-easy-import": "^3.0.0",
31-
"postcss-nested": "^4.1.0",
32-
"postcss-preset-env": "^6.0.10",
33-
"postcss-spiffing": "^0.1.0"
28+
"mocha": "^8.2.0",
29+
"postcss": "^8.1.2",
30+
"postcss-calc": "^7.0.5",
31+
"postcss-import": "^13.0.0",
32+
"postcss-nested": "^5.0.1",
33+
"postcss-preset-env": "^6.0.10"
34+
},
35+
"peerDependencies": {
36+
"postcss": "^7.0.2 || ^8.1.0"
3437
}
3538
}

postcss.config.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const path = require('path')
1+
const path = require("path");
22

33
module.exports = {
44
plugins: {
5-
'postcss-easy-import': {},
6-
'postcss-preset-env': {
7-
browsers: ['last 2 versions', 'ie >= 10'],
5+
"postcss-import": {},
6+
"postcss-preset-env": {
7+
browsers: ["last 2 versions", "ie >= 10"],
88
features: {
9-
'nesting-rules': true,
10-
'color-mod-function': {
11-
unresolved: 'warn',
9+
"nesting-rules": true,
10+
"color-mod-function": {
11+
unresolved: "warn",
1212
},
1313
},
1414
},
15-
'postcss-spiffing': {},
16-
[path.resolve(__dirname, './fixture-postcss-plugin.js')]: {}
15+
"postcss-calc": {},
16+
[path.resolve(__dirname, "./fixture-postcss-plugin.js")]: {},
1717
},
18-
}
18+
};

processor.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1-
const postcss = require('postcss')
2-
const loader = require('postcss-load-plugins')
1+
const postcss = require("postcss");
2+
const loader = require("postcss-load-config");
33

4-
let plugins
5-
let _processor
4+
let plugins;
5+
let _processor;
66

77
function processor(src, options) {
8-
options = options || {}
9-
let loaderPromise
8+
options = options || {};
9+
let loaderPromise;
1010
if (!plugins) {
11-
loaderPromise = loader(options.env || process.env, options.path, { argv: false })
12-
.then(pluginsInfo => {
13-
plugins = pluginsInfo.plugins || []
14-
})
11+
loaderPromise = loader(options.env || process.env, options.path, {
12+
argv: false,
13+
}).then((pluginsInfo) => {
14+
plugins = pluginsInfo.plugins || [];
15+
});
1516
} else {
16-
loaderPromise = Promise.resolve()
17+
loaderPromise = Promise.resolve();
1718
}
1819

1920
return loaderPromise
2021
.then(() => {
2122
if (!_processor) {
22-
_processor = postcss(plugins)
23+
_processor = postcss(plugins);
2324
}
24-
return _processor.process(src, { from: false })
25+
return _processor.process(src, { from: false });
2526
})
26-
.then(result => result.css)
27+
.then((result) => result.css);
2728
}
2829

29-
let input = ''
30-
process.stdin.on('data', data => {
31-
input += data.toString()
32-
})
30+
let input = "";
31+
process.stdin.on("data", (data) => {
32+
input += data.toString();
33+
});
3334

34-
process.stdin.on('end', () => {
35-
const inputData = JSON.parse(input)
35+
process.stdin.on("end", () => {
36+
const inputData = JSON.parse(input);
3637
processor(inputData.css, inputData.settings)
37-
.then(result => {
38-
process.stdout.write(result)
38+
.then((result) => {
39+
process.stdout.write(result);
3940
})
40-
.catch(err => {
41+
.catch((err) => {
4142
// NOTE: we console.erorr(err) and then process.exit(1) instead of throwing the error
4243
// to avoid the UnhandledPromiseRejectionWarning message.
43-
console.error(err)
44-
process.exit(1)
45-
})
46-
})
44+
console.error(err);
45+
process.exit(1);
46+
});
47+
});

0 commit comments

Comments
 (0)