Skip to content

Commit

Permalink
feature(importer): adding options.importer to pass data to importer c…
Browse files Browse the repository at this point in the history
…allback - #43
  • Loading branch information
rodneyrehm committed Feb 1, 2016
1 parent 0719dff commit 8acfca5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ sass.options({
// String to be used to for line feeds
linefeed: '\n',

// data passed through to the importer callback
// must be serializable to JSON
importer: {},

// Path to source map file
// Enables the source map generating
// Used to create sourceMappingUrl
Expand Down Expand Up @@ -262,6 +266,7 @@ sass.importer(function(request, done) {
// (string) request.previous absolute path of previously imported file ("stdin" if first)
// (string) request.resolved currentPath resolved against previousPath
// (string) request.path absolute path in file system, null if not found
// (mixed) request.options the value of options.importer
// -------------------------------
// (object) result
// (string) result.path the absolute path to load from file system
Expand Down
7 changes: 7 additions & 0 deletions src/sass.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ var Sass = {
Object.keys(options).forEach(function(key) {
var _type = Sass._optionTypes[key];

if (key === 'importer') {
// allow passing compile() time options
// to the importer callback
Sass._options[key] = options[key];
return;
}

// no need to import crap
if (!_type) {
throw new Error('Unknown option "' + key + '"');
Expand Down
1 change: 1 addition & 0 deletions src/sass.importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var Importer = {
previous: previous,
resolved: resolved,
path: found,
options: Sass._options.importer || null,
}, done);
} catch(e) {
// allow emscripten to resume libsass,
Expand Down
71 changes: 71 additions & 0 deletions test/test.importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,75 @@ describe('importer', function() {
});
});

it('should accept options', function(done) {
var source = '@import "testfile";';
var expected = '.hello{content:"world"}\n';

Sass.importer(function(request, done) {
done({
content: request.options.gustav,
});
});

Sass.options('defaults');
Sass.options({ style: Sass.style.compressed });

var options = {
importer: {
gustav: '.hello { content: "world" }',
},
};

Sass.compile(source, options, function(result) {
expect(result.text).to.equal(expected);
done();
});
});

it('should use correct options', function(done) {
var source = '@import "testfile";';
var alpha = '.alpha{content:"alpha"}\n';
var bravo = '.bravo{content:"bravo"}\n';
var charlie = '.charlie{content:"charlie"}\n';

var remaining = 4;
var _done = function() {
remaining--;
if (!remaining) {
done();
}
};

Sass.options('defaults');
Sass.options({ style: Sass.style.compressed });

Sass.importer(function(request, done) {
done({
content: request.options,
});
});

Sass.options({ importer: '.alpha { content: "alpha"; }' });
Sass.compile(source, function(result) {
expect(result.text).to.equal(alpha, 'alpha');
_done();
});

Sass.options({ importer: '.bravo { content: "bravo"; }' });
Sass.compile(source, function(result) {
expect(result.text).to.equal(bravo, 'first bravo');
_done();
});

Sass.compile(source, { importer: '.charlie { content: "charlie"; }' }, function(result) {
expect(result.text).to.equal(charlie, 'charlie');
_done();
});

Sass.compile(source, function(result) {
expect(result.text).to.equal(bravo, 'second bravo');
_done();
});
});

});

0 comments on commit 8acfca5

Please sign in to comment.