diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9b86ecc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +sudo: false +node_js: + - "6" + - "8" diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ffd2a8 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# styled-jsx-plugin-sass + +[![Build Status](https://travis-ci.org/giuseppeg/styled-jsx-plugin-sass.svg?branch=master)](https://travis-ci.org/giuseppeg/styled-jsx-plugin-sass) +[![npm](https://img.shields.io/npm/v/styled-jsx-plugin-sass.svg)](https://www.npmjs.com/package/styled-jsx-plugin-sass) + +Use [Sass](http://sass-lang.com/) with [styled-jsx](https://github.com/zeit/styled-jsx) 💥 + +## Usage + +Install the package first. + +```bash +npm install --save-dev styled-jsx-plugin-sass +``` + +Install the `node-sass` version you need (it is a peer dependency). + +```bash +npm install --save-dev node-sass +``` + +Next, add `styled-jsx-plugin-sass` to the `styled-jsx`'s `plugins` in your babel configuration: + +```json +{ + "plugins": [ + [ + "styled-jsx/babel", + { "plugins": ["styled-jsx-plugin-sass"] } + ] + ] +} +``` + +#### Notes + +`styled-jsx-plugin-sass` uses `styled-jsx`'s plugin system which is supported from version 2. + +Read more on their repository for further info. + +## License + +MIT diff --git a/fixture.css b/fixture.css new file mode 100644 index 0000000..25118c1 --- /dev/null +++ b/fixture.css @@ -0,0 +1 @@ +div { color: red; } diff --git a/index.js b/index.js new file mode 100644 index 0000000..fdd5be7 --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +const sass = require('node-sass').renderSync + +module.exports = (css, settings) => { + const cssWithPlaceholders = css + .replace(/\:\s*%%styled-jsx-expression-(\d+)%%/g, (_, id) => + `: styled-jsx-expression-${id}()` + ) + .replace(/%%styled-jsx-expression-(\d+)%%/g, (_, id) => + `/*%%styled-jsx-expression-${id}%%*/` + ) + + const preprocessed = sass({ + data: cssWithPlaceholders + }).css.toString() + + return preprocessed + .replace(/\:\s*styled-jsx-expression-(\d+)\(\)/g, (_, id) => + `: %%styled-jsx-expression-${id}%%` + ) + .replace(/\/\*%%styled-jsx-expression-(\d+)%%\*\//g, (_, id) => + `%%styled-jsx-expression-${id}%%` + ) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..eae819c --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "styled-jsx-plugin-sass", + "version": "0.1.0", + "description": "Plugin to add Sass support to styled-jsx", + "main": "index.js", + "scripts": { + "test": "mocha test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/giuseppeg/styled-jsx-plugin-sass.git" + }, + "keywords": [ + "styled-jsx-plugin", + "styled-jsx", + "sass" + ], + "author": "Giuseppe Gurgone", + "license": "MIT", + "bugs": { + "url": "https://github.com/giuseppeg/styled-jsx-plugin-sass/issues" + }, + "homepage": "https://github.com/giuseppeg/styled-jsx-plugin-sass#readme", + "peerDependencies": { + "node-sass": "*" + }, + "devDependencies": { + "mocha": "^4.0.1", + "strip-indent": "^2.0.0" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..5d1f799 --- /dev/null +++ b/test.js @@ -0,0 +1,46 @@ +const assert = require('assert') +const stripIndent = require('strip-indent') +const plugin = require('./') + +const cleanup = str => stripIndent(str).trim() + +describe('styled-jsx-plugin-sass', () => { + it('applies plugins', () => { + assert.equal( + plugin('p { img { display: block} color: color(red a(90%)) }').trim(), + cleanup(` + p { + color: color(red a(90%)); } + p img { + display: block; } + `) + ) + }) + + it('works with expressions placeholders', () => { + assert.equal( + plugin('p { img { display: block } color: %%styled-jsx-expression-1%%; } %%styled-jsx-expression-1%%').trim(), + cleanup(` + p { + color: %%styled-jsx-expression-1%%; } + p img { + display: block; } + + %%styled-jsx-expression-1%% + `) + ) + }) + + it('works with @import', () => { + assert.equal( + plugin('@import "fixture"; p { color: red }').trim(), + cleanup(` + div { + color: red; } + + p { + color: red; } + `) + ) + }) +}) diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..b68244d --- /dev/null +++ b/yarn.lock @@ -0,0 +1,137 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + +brace-expansion@^1.1.7: + version "1.1.8" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.8.tgz#c07b211c7c952ec1f8efd51a77ef0d1d3990a292" + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + +browser-stdout@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.0.tgz#f351d32969d32fa5d7a5567154263d928ae3bd1f" + +commander@2.11.0: + version "2.11.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + +debug@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + dependencies: + ms "2.0.0" + +diff@3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/diff/-/diff-3.3.1.tgz#aa8567a6eed03c531fc89d3f711cd0e5259dec75" + +escape-string-regexp@1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + +glob@7.1.2: + version "7.1.2" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + +growl@1.10.3: + version "1.10.3" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.3.tgz#1926ba90cf3edfe2adb4927f5880bc22c66c790f" + +has-flag@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51" + +he@1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + +minimatch@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + dependencies: + brace-expansion "^1.1.7" + +minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + +mkdirp@0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + dependencies: + minimist "0.0.8" + +mocha@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-4.0.1.tgz#0aee5a95cf69a4618820f5e51fa31717117daf1b" + dependencies: + browser-stdout "1.3.0" + commander "2.11.0" + debug "3.1.0" + diff "3.3.1" + escape-string-regexp "1.0.5" + glob "7.1.2" + growl "1.10.3" + he "1.1.1" + mkdirp "0.5.1" + supports-color "4.4.0" + +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + dependencies: + wrappy "1" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + +strip-indent@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68" + +supports-color@4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e" + dependencies: + has-flag "^2.0.0" + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"