Skip to content

Commit

Permalink
Add small improvements after merging #267
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Sep 1, 2016
1 parent 5dc9c2a commit 08090a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 26 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ module.exports = function (content) {
if (sassOptions.sourceMap) {
// deliberately overriding the sourceMap option
// this value is (currently) ignored by libsass when using the data input instead of file input
// however, it is still necessary for correct relative paths in result.map.sources
// however, it is still necessary for correct relative paths in result.map.sources.
sassOptions.sourceMap = this.options.context + '/sass.map';
sassOptions.omitSourceMapUrl = true;

Expand All @@ -234,24 +234,18 @@ module.exports = function (content) {
}
}

// indentedSyntax is a boolean flag
// indentedSyntax is a boolean flag.
var ext = path.extname(resourcePath);

// If we are compling sass and indentedSyntax isn't set, automatically set it.
// If we are compiling sass and indentedSyntax isn't set, automatically set it.
if (ext && ext.toLowerCase() === '.sass' && sassOptions.indentedSyntax === undefined) {
sassOptions.indentedSyntax = true;
} else {
sassOptions.indentedSyntax = Boolean(sassOptions.indentedSyntax);
}

// Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
sassOptions.importer = sassOptions.importer ? [].concat(sassOptions.importer).map(function(importer) {
return function(url, prev, done) {
return importer(url, prev === 'stdin' ? resourcePath : prev, done);
};
})
: []
;
sassOptions.importer = sassOptions.importer ? proxyCustomImporters(sassOptions.importer, resourcePath) : [];
sassOptions.importer.push(getWebpackImporter());

// `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
Expand Down Expand Up @@ -397,3 +391,25 @@ function getLoaderConfig(loaderContext) {

return assign({}, config, query);
}

/**
* Creates new custom importers that use the given `resourcePath` if libsass calls the custom importer with `prev`
* being 'stdin'.
*
* Why do we need this?
*
* We have to use the `data` option of node-sass in order to compile our sass because the `resourcePath` might
* not be an actual file on disk. When using the `data` option, libsass uses the string 'stdin' instead of a
* filename. We have to fix this behavior in order to provide a consistent experience to the webpack user.
*
* @param {function|Array<function>} importer
* @param {string} resourcePath
* @returns {Array<function>}
*/
function proxyCustomImporters(importer, resourcePath) {
return [].concat(importer).map(function (importer) {
return function (url, prev, done) {
return importer(url, prev === 'stdin' ? resourcePath : prev, done);
};
});
}
2 changes: 1 addition & 1 deletion test/tools/customImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var should = require('should');

function customImporter(path, prev) {
path.should.equal('import-with-custom-logic');
prev.match(process.cwd() + '/test/(sass|scss)/custom-importer.(scss|sass)').should.not.equal(null);
prev.should.match(/(sass|scss)[/\\]custom-importer\.(scss|sass)/);
return customImporter.returnValue;
}
customImporter.returnValue = {
Expand Down

0 comments on commit 08090a8

Please sign in to comment.