Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"prepublish": "npm run compile"
},
"dependencies": {
"is-color": "^0.2.0",
"is-there": "^4.0.0",
"lodash": "^3.10.1",
"node-sass": "^3.0.0"
Expand Down
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';
import {resolve} from 'path';
import isThere from 'is-there';
import sass from 'node-sass';
import isColor from 'is-color';

export default function(url, prev) {
if (!/\.(json|js)$/.test(url)) {
Expand Down Expand Up @@ -40,6 +41,10 @@ export default function(url, prev) {
};
}

function isCSSUnit(value) {
return /^\d*\.*\d+(em|ex|ch|rem|vh|vw|vmin|vmax|px|mm|cm|in|pt|pc|deg|grad|rad|turn|s|ms|Hz|kHz|dpi|dpcm|dppx)$/.test(value);
}

function parseJSON(json) {
return Object.keys(json)
.map(key => `$${key}: ${parseValue(json[key])};`)
Expand All @@ -52,7 +57,7 @@ function parseValue(value) {
} else if (_.isPlainObject(value)) {
return parseMap(value);
} else {
return value;
return parseEndValue(value);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseEndValue is the worst name in the world. I should rename it to parseValue or maybe you have a better proposal :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a parseValue function, thats why I chose parseEndValue... 😴 💤

}
}

Expand All @@ -67,3 +72,11 @@ function parseMap(map) {
.map(key => `${key}: ${parseValue(map[key])}`)
.join(',')})`;
}

function parseEndValue(value) {
if (typeof value === 'string' && !isColor(value) && !isCSSUnit(value)) {
return JSON.stringify(value);
}

return value;
}
7 changes: 7 additions & 0 deletions test/fixtures/convert-strings/style-js.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import 'variables.js';

body {
content: $string;
color: $hex-color, $hsl-color, $rgba-color, $rgb-color, $css-color;
font-size: $em-unit;
}
7 changes: 7 additions & 0 deletions test/fixtures/convert-strings/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import 'variables.json';

body {
content: $string;
color: $hex-color, $hsl-color, $rgba-color, $rgb-color, $css-color;
font-size: $em-unit;
}
10 changes: 10 additions & 0 deletions test/fixtures/convert-strings/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
"hex-color": "#c33",
"hsl-color": "hsl(100, 100%, 100%)",
"rgba-color": "rgba(0, 0, 0, 1)",
"rgb-color": "rgb(10, 0, 0)",
"css-color": "blue",
"px-unit": "10px",
"em-unit": "2.3em",
"string": "Lorem ipsum, (\"foo\", bar)"
}
10 changes: 10 additions & 0 deletions test/fixtures/convert-strings/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"hex-color": "#c33",
"hsl-color": "hsl(100, 100%, 100%)",
"rgba-color": "rgba(0, 0, 0, 1)",
"rgb-color": "rgb(10, 0, 0)",
"css-color": "blue",
"px-unit": "10px",
"em-unit": "2.3em",
"string": "Lorem ipsum, (\"foo\", bar)"
}
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ describe('Import type test', function() {
})).forEach(testExpectedCSS);
});

it('converts JS/JSON strings to Sass strings', function() {
['./test/fixtures/convert-strings/style-js.scss',
'./test/fixtures/convert-strings/style.scss'
].map(sassRenderFile()).forEach(function(result) {
expect(result.css.toString()).to.eql(`body {\n content: 'Lorem ipsum, ("foo", bar)';\n color: #c33, white, black, #0a0000, blue;\n font-size: 2.3em; }\n`);
});
});

it(`JS imports do not export non valid JSON values`, function() {
function render() {
sass.renderSync({
Expand Down