Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: giuseppeg/styled-jsx-plugin-postcss
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1ccce1321e524667eddccdd7d04bc95c8fe1ef56
Choose a base ref
...
head repository: giuseppeg/styled-jsx-plugin-postcss
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 66d07dae89e6f81d8c11056a93ee8a919f8e8b0d
Choose a head ref

Commits on Oct 18, 2017

  1. Replace styled-jsx-placeholder patterns (#2)

    The original version mistakenly matched `%%styled-jsx-expression-XXX%%` while it should have matched `%%styled-jsx-placeholder-XXX%%`
    andrewb authored and giuseppeg committed Oct 18, 2017
    Copy the full SHA
    0a5dcb6 View commit details
  2. 0.1.1

    giuseppeg committed Oct 18, 2017
    Copy the full SHA
    9151f33 View commit details

Commits on Nov 22, 2017

  1. Copy the full SHA
    f62fd94 View commit details

Commits on Dec 27, 2017

  1. Fix: deasync compatibility issue with node@9.3 (#9)

    Upgrade deasync to address compatibility issue with node@9.3
    
    abbr/deasync#89
    pukapukan authored and giuseppeg committed Dec 27, 2017
    Copy the full SHA
    377a4b1 View commit details

Commits on Dec 29, 2017

  1. update lockfiles

    giuseppeg committed Dec 29, 2017
    Copy the full SHA
    7474ebb View commit details
  2. 0.1.2

    giuseppeg committed Dec 29, 2017
    Copy the full SHA
    ef0f78b View commit details

Commits on Apr 1, 2018

  1. Copy the full SHA
    86bf547 View commit details
  2. 0.1.3

    giuseppeg committed Apr 1, 2018
    Copy the full SHA
    dc1ff8d View commit details

Commits on Sep 4, 2018

  1. Updated dependencies, converted .postcssrc into a postcss.config.js, …

    …updated tests to use preset-env, added a new plugin test and converted old one into a features/browser feature test.
    JosephScript committed Sep 4, 2018
    Copy the full SHA
    4291471 View commit details

Commits on Oct 7, 2018

  1. Copy the full SHA
    91b456c View commit details
  2. Updated dependencies.

    JosephScript committed Oct 7, 2018
    Copy the full SHA
    60b9be9 View commit details

Commits on Oct 8, 2018

  1. Upgrade to PostCSS 7 (#16)

    also update some dependencies and add tests for postcss-preset-env
    Joseph A. Szczesniak authored and giuseppeg committed Oct 8, 2018
    Copy the full SHA
    20ff2b0 View commit details
  2. 1.0.0

    giuseppeg committed Oct 8, 2018
    Copy the full SHA
    93c872c View commit details

Commits on Oct 11, 2018

  1. Copy the full SHA
    29cebd1 View commit details
  2. Copy the full SHA
    1534e46 View commit details

Commits on Oct 16, 2018

  1. Merge pull request #18 from NukaPunk/master

    Removed postcss.config.js and updated readme
    Joseph A. Szczesniak authored Oct 16, 2018
    Copy the full SHA
    d058bb9 View commit details
  2. #18 (#19)

    Joseph A. Szczesniak authored Oct 16, 2018
    Copy the full SHA
    5611b98 View commit details

Commits on Oct 22, 2018

  1. 2.0.0

    giuseppeg committed Oct 22, 2018
    Copy the full SHA
    7df45db View commit details

Commits on Apr 5, 2019

  1. Support custom config file path, close: #23 (#24)

    * Support custom config file path, close: /issues/23
    
    * format example config as json
    
    * v2.0.1
    leecade authored and Joseph A. Szczesniak committed Apr 5, 2019
    Copy the full SHA
    fac3a09 View commit details

Commits on Jun 1, 2019

  1. Revert manual version bump

    giuseppeg committed Jun 1, 2019
    Copy the full SHA
    cfb1997 View commit details
  2. 2.0.1

    giuseppeg committed Jun 1, 2019
    Copy the full SHA
    63b248a View commit details

Commits on Jul 11, 2019

  1. Bump lodash from 4.17.4 to 4.17.14 (#29)

    Bumps [lodash](https://github.com/lodash/lodash) from 4.17.4 to 4.17.14.
    - [Release notes](https://github.com/lodash/lodash/releases)
    - [Commits](lodash/lodash@4.17.4...4.17.14)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored and giuseppeg committed Jul 11, 2019
    Copy the full SHA
    824a4c7 View commit details
  2. Bump lodash.template from 4.4.0 to 4.5.0 (#28)

    Bumps [lodash.template](https://github.com/lodash/lodash) from 4.4.0 to 4.5.0.
    - [Release notes](https://github.com/lodash/lodash/releases)
    - [Commits](lodash/lodash@4.4.0...4.5.0)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored and giuseppeg committed Jul 11, 2019
    Copy the full SHA
    aac482a View commit details
  3. Copy the full SHA
    70d2cc4 View commit details

Commits on Oct 8, 2019

  1. FUNDING.yml

    giuseppeg authored Oct 8, 2019
    Copy the full SHA
    ec8a46a View commit details

Commits on Sep 6, 2020

  1. Fix sync issue (#35)

    It should fix #31 #32 #34
    
    Remove `deasync` in favor of `spawnSync`.
    tldrd0117 authored Sep 6, 2020
    Copy the full SHA
    52a30d9 View commit details
  2. Update dependencies

    giuseppeg committed Sep 6, 2020
    Copy the full SHA
    81a4a69 View commit details
  3. 3.0.0

    giuseppeg committed Sep 6, 2020
    Copy the full SHA
    4240e4a View commit details

Commits on Sep 23, 2020

  1. fix: silent error (#38)

    This can happen with invalid postcss config
    
    Fixes #37
    
    * fix error handling for css syntax error
    * add message about Next.js 9 non standard postcss config
    ramasilveyra authored Sep 23, 2020
    Copy the full SHA
    cead2c4 View commit details
  2. 3.0.1

    giuseppeg committed Sep 23, 2020
    Copy the full SHA
    ea48949 View commit details

Commits on Oct 10, 2020

  1. Copy the full SHA
    220d8d6 View commit details

Commits on Oct 13, 2020

  1. update node in ci

    giuseppeg committed Oct 13, 2020
    Copy the full SHA
    0964791 View commit details
  2. 3.0.2

    giuseppeg committed Oct 13, 2020
    Copy the full SHA
    c51fe8c View commit details

Commits on Nov 24, 2020

  1. Update README.md

    giuseppeg authored Nov 24, 2020
    Copy the full SHA
    f442f2c View commit details
  2. Update issue templates

    giuseppeg authored Nov 24, 2020
    Copy the full SHA
    231a54b View commit details
  3. Support PostCSS 8 (#43)

    cl1ck authored and giuseppeg committed Nov 24, 2020
    Copy the full SHA
    66d07da View commit details
Showing with 1,302 additions and 616 deletions.
  1. +3 −0 .github/FUNDING.yml
  2. +10 −0 .github/ISSUE_TEMPLATE/custom.md
  3. +10 −0 .github/ISSUE_TEMPLATE/not-actively-maintained.md
  4. +3 −0 .npmignore
  5. +0 −7 .postcssrc
  6. +1 −0 .prettierignore
  7. +2 −2 .travis.yml
  8. +70 −7 README.md
  9. +3 −0 fixture-invalid-config/postcss.config.js
  10. +12 −0 fixture-postcss-plugin.js
  11. +32 −16 index.js
  12. +12 −9 package.json
  13. +18 −0 postcss.config.js
  14. +33 −16 processor.js
  15. +71 −21 test.js
  16. +1,022 −538 yarn.lock
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: giuseppeg
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: ''
about: ''
title: ''
labels: ''
assignees: ''

---

# ⚠️ This plugin is not actively being maintained. If you want me to work on it please [consider donating]( https://github.com/sponsors/giuseppeg)
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/not-actively-maintained.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Not actively maintained
about: Sponsors only
title: ''
labels: ''
assignees: ''

---

# ⚠️ This plugin is not actively being maintained. If you want me to work on it please [consider donating]( https://github.com/sponsors/giuseppeg)
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
postcss.config.js
test.js
fixture.css
7 changes: 0 additions & 7 deletions .postcssrc

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.css
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
sudo: false
node_js:
- "6"
- "8"
- "10"
- "14"
77 changes: 70 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,37 +3,100 @@
[![Build Status](https://travis-ci.org/giuseppeg/styled-jsx-plugin-postcss.svg?branch=master)](https://travis-ci.org/giuseppeg/styled-jsx-plugin-postcss)
[![npm](https://img.shields.io/npm/v/styled-jsx-plugin-postcss.svg)](https://www.npmjs.com/package/styled-jsx-plugin-postcss)

Use [PostCSS](https://github.com/postcss/postcss) with [styled-jsx](https://github.com/zeit/styled-jsx) 💥
Use [PostCSS](https://github.com/postcss/postcss) with
[styled-jsx](https://github.com/zeit/styled-jsx) 💥

⚠️ **This plugin is not actively being maintained. If you want me to work on it please [consider donating](https://github.com/sponsors/giuseppeg).**

## Usage

Install the package first.
Install this package first.

```bash
npm install --save styled-jsx-plugin-postcss
```

Next, add `styled-jsx-plugin-postcss` to the `styled-jsx`'s `plugins` in your babel configuration:
Next, add `styled-jsx-plugin-postcss` to the `styled-jsx`'s `plugins` in your
babel configuration:

```json
{
"plugins": [
["styled-jsx/babel", { "plugins": ["styled-jsx-plugin-postcss"] }]
]
}
```

With config:

```json
{
"plugins": [
[
"styled-jsx/babel",
{ "plugins": ["styled-jsx-plugin-postcss"] }
{
"plugins": [
[
"styled-jsx-plugin-postcss",
{
"path": "[PATH_PREFIX]/postcss.config.js"
}
]
]
}
]
]
}
```

### Example with CRA

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

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

```javascript
// config-overrides.js
// this file overrides the CRA webpack.config

const { getBabelLoader } = require("react-app-rewired");

module.exports = function override(config, env) {
const loader = getBabelLoader(config.module.rules);

// Older versions of webpack have `plugins` on `loader.query` instead of `loader.options`.
const options = loader.options || loader.query;
options.plugins = [
[
"styled-jsx/babel",
{
plugins: ["styled-jsx-plugin-postcss"],
},
],
].concat(options.plugins || []);
return config;
};
```

_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_.

#### Notes

`styled-jsx-plugin-postcss` uses `styled-jsx`'s plugin system which is supported from version 2.
Read more on their repository for further info.
`styled-jsx-plugin-postcss` uses `styled-jsx`'s plugin system which is supported
from version 2. Read more on their repository for further info.

## Plugins

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

## Contributions

**PRs and contributions are welcome!**

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

## License

3 changes: 3 additions & 0 deletions fixture-invalid-config/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ["mock-plugin"],
};
12 changes: 12 additions & 0 deletions fixture-postcss-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const postcss = require("postcss");

module.exports = (options = {}) => ({
postcssPlugin: "postcss-csso",
Once(root, { result, postcss }) {
console.warn("warn");
console.error("error");
return root;
},
});

module.exports.postcss = true;
48 changes: 32 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
const loopWhile = require('deasync').loopWhile
const processor = require('./processor')
const { spawnSync } = require("child_process");
const path = require("path");

module.exports = (css, settings) => {
let processedCss
let wait = true
const cssWithPlaceholders = css.replace(
/%%styled-jsx-placeholder-(\d+)%%/g,
(_, id) => `/*%%styled-jsx-placeholder-${id}%%*/`
);

function resolved(result) {
processedCss = result
wait = false
}
const result = spawnSync("node", [path.resolve(__dirname, "processor.js")], {
input: JSON.stringify({
css: cssWithPlaceholders,
settings,
}),
encoding: "utf8",
});

processor(css)
.then(resolved)
.catch(resolved)
loopWhile(() => wait)
if (result.status !== 0) {
if (result.stderr.includes("Invalid PostCSS Plugin")) {
let isNext = false;
try {
require.resolve("next");
isNext = true;
} catch (err) {}
if (isNext) {
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"
);
}
}

if (processedCss instanceof Error || processedCss.name === 'CssSyntaxError') {
throw processedCss
throw new Error(`postcss failed with ${result.stderr}`);
}

return processedCss
}
return result.stdout.replace(
/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g,
(_, id) => `%%styled-jsx-placeholder-${id}%%`
);
};
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "styled-jsx-plugin-postcss",
"version": "0.1.0",
"version": "4.0.0",
"description": "Plugin to add PostCSS support to styled-jsx",
"main": "index.js",
"scripts": {
"test": "mocha test.js"
"test": "mocha --timeout 10000 test.js"
},
"repository": {
"type": "git",
@@ -22,14 +22,17 @@
},
"homepage": "https://github.com/giuseppeg/styled-jsx-plugin-postcss#readme",
"dependencies": {
"deasync": "^0.1.10",
"postcss": "^6.0.13",
"postcss-load-plugins": "^2.3.0"
"postcss-load-config": "^2.1.2"
},
"devDependencies": {
"mocha": "^4.0.1",
"postcss-cssnext": "^3.0.2",
"postcss-easy-import": "^3.0.0",
"postcss-nested": "^2.1.2"
"mocha": "^8.2.0",
"postcss": "^8.1.2",
"postcss-calc": "^7.0.5",
"postcss-import": "^13.0.0",
"postcss-nested": "^5.0.1",
"postcss-preset-env": "^6.0.10"
},
"peerDependencies": {
"postcss": "^7.0.2 || ^8.1.0"
}
}
18 changes: 18 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require("path");

module.exports = {
plugins: {
"postcss-import": {},
"postcss-preset-env": {
browsers: ["last 2 versions", "ie >= 10"],
features: {
"nesting-rules": true,
"color-mod-function": {
unresolved: "warn",
},
},
},
"postcss-calc": {},
[path.resolve(__dirname, "./fixture-postcss-plugin.js")]: {},
},
};
49 changes: 33 additions & 16 deletions processor.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
const postcss = require('postcss')
const loader = require('postcss-load-plugins')
const postcss = require("postcss");
const loader = require("postcss-load-config");

let plugins
let _processor
let plugins;
let _processor;

function processor(src, options) {
options = options || {}

let loaderPromise
options = options || {};
let loaderPromise;
if (!plugins) {
loaderPromise = loader(options.env || process.env)
.then(pluginsInfo => {
plugins = pluginsInfo.plugins || []
})
loaderPromise = loader(options.env || process.env, options.path, {
argv: false,
}).then((pluginsInfo) => {
plugins = pluginsInfo.plugins || [];
});
} else {
loaderPromise = Promise.resolve()
loaderPromise = Promise.resolve();
}

return loaderPromise
.then(() => {
if (!_processor) {
_processor = postcss(plugins)
_processor = postcss(plugins);
}
return _processor.process(src)
return _processor.process(src, { from: false });
})
.then(result => result.css)
.then((result) => result.css);
}

module.exports = processor
let input = "";
process.stdin.on("data", (data) => {
input += data.toString();
});

process.stdin.on("end", () => {
const inputData = JSON.parse(input);
processor(inputData.css, inputData.settings)
.then((result) => {
process.stdout.write(result);
})
.catch((err) => {
// NOTE: we console.erorr(err) and then process.exit(1) instead of throwing the error
// to avoid the UnhandledPromiseRejectionWarning message.
console.error(err);
process.exit(1);
});
});
Loading