Skip to content

Commit

Permalink
fix for node-sass 3.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jalusk committed Jan 5, 2016
1 parent 8b94555 commit 79a8393
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/
dist/
*.sublime*
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/src
/test
.editorconfig
.gitignore
.jshintrc
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# node-sass-json-importer [![build status](https://travis-ci.org/Updater/node-sass-json-importer.svg?branch=master)](https://travis-ci.org/Updater/node-sass-json-importer)
JSON importer for [node-sass](https://github.com/sass/node-sass). Allows `@import`ing `.json` files in Sass files parsed by `node-sass`.
JSON importer for [node-sass](https://github.com/sass/node-sass). Allows `@import`ing `.json` files in Sass files parsed by `node-sass`. (modified)

## Usage
This module hooks into [node-sass's importer api](https://github.com/sass/node-sass#importer--v200---experimental).
Expand Down
68 changes: 68 additions & 0 deletions dist/node-sass-json-importer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _path = require('path');

var _isThere = require('is-there');

var _isThere2 = _interopRequireDefault(_isThere);

exports['default'] = function (url, prev, done) {
if (/\.json$/.test(url)) {
var includePaths = this.options.includePaths ? this.options.includePaths.split(':') : [];
var paths = [].concat(prev.slice(0, prev.lastIndexOf('/'))).concat(includePaths);

var files = paths.map(function (path) {
return (0, _path.resolve)(path, url);
}).filter(_isThere2['default']);

if (files.length === 0) {
return new Error('Unable to find "' + url + '" from the following path(s): ' + paths.join(', ') + '. Check includePaths.');
}

return {
contents: parseJSON(require(files[0]))
};
} else {

return done({ file: url });
}
};

function parseJSON(json) {
return Object.keys(json).map(function (key) {
return '$' + key + ': ' + parseValue(json[key]) + ';';
}).join('\n');
}

function parseValue(value) {
if (_lodash2['default'].isArray(value)) {
return parseList(value);
} else if (_lodash2['default'].isPlainObject(value)) {
return parseMap(value);
} else {
return '"' + value + '"';
}
}

function parseList(list) {
return '(' + list.map(function (value) {
return parseValue(value);
}).join(',') + ')';
}

function parseMap(map) {
return '(' + Object.keys(map).map(function (key) {
return key + ': ' + parseValue(map[key]);
}).join(',') + ')';
}
module.exports = exports['default'];
38 changes: 19 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import _ from 'lodash';
import {resolve} from 'path';
import isThere from 'is-there';
import sass from 'node-sass';

export default function(url, prev) {
if (!/\.json$/.test(url)) {
return sass.NULL;
}
export default function(url, prev, done) {
if (/\.json$/.test(url)) {
let includePaths = this.options.includePaths ? this.options.includePaths.split(':') : [];
let paths = []
.concat(prev.slice(0, prev.lastIndexOf('/')))
.concat(includePaths);

let includePaths = this.options.includePaths ? this.options.includePaths.split(':') : [];
let paths = []
.concat(prev.slice(0, prev.lastIndexOf('/')))
.concat(includePaths);
let files = paths
.map(path => resolve(path, url))
.filter(isThere);

let files = paths
.map(path => resolve(path, url))
.filter(isThere);
if (files.length === 0) {
return new Error(`Unable to find "${url}" from the following path(s): ${paths.join(', ')}. Check includePaths.`);
}

if (files.length === 0) {
return new Error(`Unable to find "${url}" from the following path(s): ${paths.join(', ')}. Check includePaths.`);
}
return {
contents: parseJSON(require(files[0]))
};
} else {

return {
contents: parseJSON(require(files[0]))
};
return done({file: url});
}
}

function parseJSON(json) {
Expand All @@ -38,7 +38,7 @@ function parseValue(value) {
} else if (_.isPlainObject(value)) {
return parseMap(value);
} else {
return value;
return '"' + value + '"';
}
}

Expand Down

0 comments on commit 79a8393

Please sign in to comment.