Commit 98c05d49 by Robin Winslow

Implement SASS_PATH

- New function buildIncludePaths to build the includePaths string for passing through to libsass
- In buildIncludePaths, check for SASS_PATH environment variable, and if found include them behind any other specified include-paths
- Add two tests to `.render(options, callback)`:
  - "should check SASS_PATH in the specified order"
  - "should prefer include path over SASS_PATH"
parent c453f8a8
...@@ -16,7 +16,6 @@ if (!sass.hasBinary(sass.getBinaryPath())) { ...@@ -16,7 +16,6 @@ if (!sass.hasBinary(sass.getBinaryPath())) {
} }
} }
/** /**
* Require binding * Require binding
*/ */
...@@ -165,6 +164,26 @@ function getLinefeed(options) { ...@@ -165,6 +164,26 @@ function getLinefeed(options) {
} }
/** /**
* Build an includePaths string
* from the options.includePaths array and the SASS_PATH environment variable
*
* @param {Object} options
* @api private
*/
function buildIncludePaths(options) {
options.includePaths = options.includePaths || [];
if (process.env.hasOwnProperty('SASS_PATH')) {
options.includePaths = options.includePaths.concat(
process.env.SASS_PATH.split(path.delimiter)
);
}
return options.includePaths.join(path.delimiter);
}
/**
* Get options * Get options
* *
* @param {Object} options * @param {Object} options
...@@ -173,12 +192,13 @@ function getLinefeed(options) { ...@@ -173,12 +192,13 @@ function getLinefeed(options) {
function getOptions(opts, cb) { function getOptions(opts, cb) {
var options = clonedeep(opts || {}); var options = clonedeep(opts || {});
options.sourceComments = options.sourceComments || false; options.sourceComments = options.sourceComments || false;
if (options.hasOwnProperty('file')) { if (options.hasOwnProperty('file')) {
options.file = getInputFile(options); options.file = getInputFile(options);
} }
options.outFile = getOutputFile(options); options.outFile = getOutputFile(options);
options.includePaths = (options.includePaths || []).join(path.delimiter); options.includePaths = buildIncludePaths(options);
options.precision = parseInt(options.precision) || 5; options.precision = parseInt(options.precision) || 5;
options.sourceMap = getSourceMap(options); options.sourceMap = getSourceMap(options);
options.style = getStyle(options); options.style = getStyle(options);
......
...@@ -14,6 +14,11 @@ var assert = require('assert'), ...@@ -14,6 +14,11 @@ var assert = require('assert'),
describe('api', function() { describe('api', function() {
describe('.render(options, callback)', function() { describe('.render(options, callback)', function() {
beforeEach(function() {
delete process.env.SASS_PATH;
});
it('should compile sass to css with file', function(done) { it('should compile sass to css with file', function(done) {
var expected = read(fixture('simple/expected.css'), 'utf8').trim(); var expected = read(fixture('simple/expected.css'), 'utf8').trim();
...@@ -137,6 +142,59 @@ describe('api', function() { ...@@ -137,6 +142,59 @@ describe('api', function() {
}); });
}); });
it('should check SASS_PATH in the specified order', function(done) {
var src = read(fixture('sass-path/index.scss'), 'utf8');
var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
envIncludes = [
fixture('sass-path/red'),
fixture('sass-path/orange')
];
process.env.SASS_PATH = envIncludes.join(path.delimiter);
sass.render({
data: src,
includePaths: []
}, function(error, result) {
assert.equal(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
});
process.env.SASS_PATH = envIncludes.reverse().join(path.delimiter);
sass.render({
data: src,
includePaths: []
}, function(error, result) {
assert.equal(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
done();
});
});
it('should prefer include path over SASS_PATH', function(done) {
var src = read(fixture('sass-path/index.scss'), 'utf8');
var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
envIncludes = [
fixture('sass-path/red')
];
process.env.SASS_PATH = envIncludes.join(path.delimiter);
sass.render({
data: src,
includePaths: []
}, function(error, result) {
assert.equal(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
});
sass.render({
data: src,
includePaths: [fixture('sass-path/orange')]
}, function(error, result) {
assert.equal(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
done();
});
});
it('should render with precision option', function(done) { it('should render with precision option', function(done) {
var src = read(fixture('precision/index.scss'), 'utf8'); var src = read(fixture('precision/index.scss'), 'utf8');
var expected = read(fixture('precision/expected.css'), 'utf8').trim(); var expected = read(fixture('precision/expected.css'), 'utf8').trim();
......
@import 'colors';
body {
background: $color;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment