node-sass-import
Advanced tools
Comparing version 1.0.5 to 1.1.0
@@ -8,2 +8,3 @@ 'use strict'; | ||
var pathFormat = require('path-format'); | ||
var async = require('async'); | ||
@@ -26,2 +27,7 @@ var opts = { | ||
/** | ||
* Resolves `url` and calls `done(error, null)` when done. | ||
* Note that the `done()` function args don't match what node-sass expects. | ||
*/ | ||
var resolver = function (url, baseDir, done) { | ||
@@ -40,10 +46,26 @@ resolve(url, { | ||
}, function (err, file) { | ||
if (err) throw err; | ||
if (err) return done(err); | ||
// Strip the extension so .css gets evaluated properly as .scss | ||
file = file.replace(/\.(sa|sc|c)ss$/, ''); | ||
done({ file: file }); | ||
done(null, file); | ||
}); | ||
}; | ||
/** | ||
* Builds `{file}` or `{contents}` (as node-sass expects) from a list of files. | ||
*/ | ||
var importify = function (files) { | ||
if (files.length === 1) { | ||
return { file: files[0] }; | ||
} else { | ||
var contents = files.map(function (fname) { | ||
return '@import ' + JSON.stringify(fname) + ';'; | ||
}).join('\n'); | ||
return { contents: contents }; | ||
} | ||
}; | ||
module.exports = function (url, file, done) { | ||
@@ -62,7 +84,13 @@ var baseDir = path.dirname(file); | ||
if (dirs.length === 0) { | ||
resolver(url, baseDir, done); | ||
resolver(url, baseDir, function (err, results) { | ||
if (err) throw err; | ||
done(importify([results])); | ||
}); | ||
} | ||
if (dirs.length === 1) { | ||
resolver(dirs[0], baseDir, done); | ||
resolver(dirs[0], baseDir, function (err, results) { | ||
if (err) throw err; | ||
done(importify([results])); | ||
}); | ||
} | ||
@@ -72,11 +100,17 @@ }); | ||
if (urls.length === 1) { | ||
resolver(urls[0], baseDir, done); | ||
} | ||
if (urls.length === 0) return; | ||
if (urls.length > 1) { | ||
console.log(urls); | ||
throw new Error('Resolve conflicting files'); | ||
} | ||
// Resolve all of `urls` through resolver(); then compile them into | ||
// '@import' statements. | ||
async.parallel( | ||
urls.map(function (url) { | ||
return function (callback) { | ||
resolver(url, baseDir, callback); | ||
}; | ||
}) | ||
, function (err, filenames) { | ||
if (err) throw err; | ||
done(importify(filenames)); | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "node-sass-import", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Allows usage of @include (of .scss) akin to require (of .js) in node.js", | ||
@@ -30,2 +30,3 @@ "bugs": "https://github.com/anarh/node-sass-import/issues", | ||
"dependencies": { | ||
"async": "2.0.1", | ||
"glob": "^6.0.4", | ||
@@ -32,0 +33,0 @@ "path-format": "^1.2.1", |
@@ -5,3 +5,3 @@ # node-sass-import | ||
Allows usage of (@include "some-sass-npm-module") akin to require("some-module") in node.js | ||
Allows usage of `@import "some-sass-npm-module"` akin to `require("some-npm-module")` in node.js | ||
@@ -8,0 +8,0 @@ ## Install |
7301
102
6
+ Addedasync@2.0.1
+ Addedasync@2.0.1(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedlodash@4.17.21(transitive)
- Removedes-object-atoms@1.1.0(transitive)