Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 97c9f16

Browse files
martonledererxhuz
andcommitted
feat: dart-sass
Co-authored-by: xhuz <[email protected]>
1 parent 1986b22 commit 97c9f16

File tree

4 files changed

+283
-147
lines changed

4 files changed

+283
-147
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
Use [Sass](http://sass-lang.com/) with [styled-jsx](https://github.com/vercel/styled-jsx) 💥
44

5-
This is a fixed/updated version of [styled-jsx-plugin-sass](https://github.com/giuseppeg/styled-jsx-plugin-sass) by [@giuseppeg](https://github.com/giuseppeg), made because that package seems to be outdated. It works with [indented sass](#indented-syntax) and dart sass too.
5+
This is a fixed/updated version of [styled-jsx-plugin-sass](https://github.com/giuseppeg/styled-jsx-plugin-sass) by [@giuseppeg](https://github.com/giuseppeg), made because that package seems to be outdated. It works with [indented sass](#indented-syntax) and [dart sass](#dart-sass) too.
66

77
## Usage
88

9-
Install the package first.
9+
Install the package and the `node-sass` (or [dart-sass](#dart-sass)) version you need (it is a peer dependency).
1010

11-
```bash
12-
npm install --save-dev styled-jsx-plugin-sass
11+
```sh
12+
npm install --save-dev node-sass styled-jsx-plugin-sass-2
1313
```
1414

15-
Install the `node-sass` version you need (it is a peer dependency).
15+
with yarn
1616

17-
```bash
18-
npm install --save-dev node-sass
17+
```sh
18+
yarn add -D node-sass styled-jsx-plugin-sass-2
1919
```
2020

2121
Next, add `styled-jsx-plugin-sass-2` to the `styled-jsx`'s `plugins` in your babel configuration (e.g. `.babelrc`):
@@ -81,6 +81,20 @@ To use indented sytax, you will need to update your `sassOptions` inside your ba
8181
}
8282
```
8383

84+
### Dart sass
85+
86+
If you want to use Dart sass instead of `node-sass`, your install command should look like this:
87+
88+
```sh
89+
npm install --save-dev sass styled-jsx-plugin-sass-2
90+
```
91+
92+
with yarn
93+
94+
```sh
95+
yarn add -D sass styled-jsx-plugin-sass-2
96+
```
97+
8498
#### Notes
8599

86100
`styled-jsx-plugin-sass-2` uses `styled-jsx`'s plugin system which is supported from version 2.

index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const sass = require('node-sass');
2-
const stripIndent = require('strip-indent');
1+
const stripIndent = require('strip-indent')
32

43
module.exports = (css, settings) => {
4+
const sass = getSassImplementation()
55
const cssWithPlaceholders = css
66
.replace(/%%styled-jsx-placeholder-(\d+)%%(\w*\s*[),;!{])/g, (_, id, p1) =>
77
`styled-jsx-placeholder-${id}-${p1}`
@@ -11,18 +11,15 @@ module.exports = (css, settings) => {
1111
)
1212

1313
// Prepend option data to cssWithPlaceholders
14-
const optionData = settings.sassOptions && settings.sassOptions.data || "";
15-
let data = optionData + "\n" + cssWithPlaceholders;
14+
const optionData = settings.sassOptions && settings.sassOptions.data || ''
15+
let data = optionData + '\n' + cssWithPlaceholders
1616

1717
// clean up extra indent if we are using indentedSyntax
18-
if(settings.sassOptions && settings.sassOptions.indentedSyntax) data = stripIndent(data);
18+
if(settings.sassOptions && settings.sassOptions.indentedSyntax) data = stripIndent(data)
1919

20-
const preprocessed = sass.renderSync(
21-
Object.assign(
22-
{},
23-
settings.sassOptions,
24-
{ data }
25-
)).css.toString()
20+
const preprocessed = sass
21+
.renderSync(Object.assign({}, settings.sassOptions, { data }))
22+
.css.toString()
2623

2724
return preprocessed
2825
.replace(/styled-jsx-placeholder-(\d+)-(\w*\s*[),;!{])/g, (_, id, p1) =>
@@ -32,3 +29,20 @@ module.exports = (css, settings) => {
3229
`%%styled-jsx-placeholder-${id}%%`
3330
)
3431
}
32+
33+
function getSassImplementation() {
34+
let sassImplPkg = 'sass'
35+
36+
try {
37+
require.resolve('sass')
38+
} catch {
39+
try {
40+
require.resolve('node-sass')
41+
sassImplPkg = 'node-sass'
42+
} catch {
43+
sassImplPkg = 'sass'
44+
}
45+
}
46+
47+
return require(sassImplPkg)
48+
}

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const plugin = require('./')
66

77
const cleanup = str => stripIndent(str).trim()
88

9+
// testing is done with node-sass
910
describe('styled-jsx-plugin-sass', () => {
1011

1112
it('applies plugins', () => {

0 commit comments

Comments
 (0)