Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Hacked in support for maps (#8) #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sass-variable-loader",
"version": "0.1.2",
"version": "0.2.0",
"description": "Sass variable loader module for webpack",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/get-variables.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stripComments from 'strip-json-comments';

export default function getVariables(content) {
const variableRegex = /\$(.+):\s+(.+);?/;
const variableRegex = /\$(.+?):\s+(.+);?/;
const variables = [];

stripComments(content).split('\n').forEach(line => {
Expand Down
32 changes: 30 additions & 2 deletions src/parse-variables.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import sass from 'node-sass';
import camelCase from 'lodash.camelcase';

const mapRegex = /([^(].+?):\s?([0-9A-z-_$#'"\\]+),?\s?/g;

function constructSassString(variables) {
const asVariables = variables
.map(variable => `$${variable.name}: ${variable.value};`)
.join('\n');
const asClasses = variables
.map(variable => `.${variable.name} { value: ${variable.value} }`)
.map(variable => {
if (variable.value.startsWith('(')) { // is a map
let matches = mapRegex.exec(variable.value);
const map = [];
while (matches !== null) {
map.push([matches[1], matches[2]]);
matches = mapRegex.exec(variable.value);
}
const mapVars = map.reduce(
(acc, mapVar) =>
`${acc}\n${mapVar[0]}: ${mapVar[1]};`,
''
);
return `.${variable.name} { ${mapVars} }`;
}
return `.${variable.name} { value: ${variable.value} }`;
})
.join('\n');

return `${asVariables}\n${asClasses}`;
Expand All @@ -17,10 +35,20 @@ export default function parseVariables(variables, opts = {}) {
data: constructSassString(variables),
outputStyle: 'compact',
}).css.toString();

const parsedVariables = result.split(/\n/)
.filter(line => line && line.length)
.map(variable => {
if ((variable.match(/;/g) || []).length > 1) { // is a map
const name = /\.(.+) {/.exec(variable)[1];
const varRegex = /\s([A-z-_]+?): ([0-9A-z-_$#'"\\]+?);/g;
let matches = varRegex.exec(variable);
const vars = {};
while (matches !== null) {
vars[matches[1]] = matches[2];
matches = varRegex.exec(variable);
}
return { [name]: vars };
}
const [, name, value] = /\.(.+) { value: (.+); }/.exec(variable);
const obj = {};

Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ $x: $one;
});
})

context('maps', function() {
const sass = '$map: (one: 1, two: #eee);';
const variables = getVariables(sass);

describe('getVariables()', function() {
it('should return an array with 1 item', () => {
console.log(variables);
expect(variables).to.be.a('array');
expect(variables).to.have.length(1);
});
});

describe('parseVariables()', function() {
it('should return maps', function() {
const result = parseVariables(variables);
expect(result.map).to.be.a('object');
expect(result.map).to.include.keys('one');
expect(result.map).to.include.keys('two');
expect(result.map.one).to.equal('1'); // may want to parse ints?
expect(result.map.two).to.equal('#eee');
});
});
});

context('.sass format', function() {
const sass = `$one: 123
$x: $one
Expand Down