Skip to content

Commit

Permalink
Move proxyCustomImporters to dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Dec 26, 2016
1 parent a796ace commit f8ea88a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
31 changes: 1 addition & 30 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const async = require("async");
const assign = require("object-assign");
const formatSassError = require("./formatSassError");
const importsToResolve = require("./importsToResolve");
const proxyCustomImporters = require("./proxyCustomImporters");

const slice = Array.prototype.slice;
const matchCss = /\.css$/;

// This queue makes sure node-sass leaves one thread available for executing
Expand Down Expand Up @@ -207,33 +207,4 @@ 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((importer) => {
return function (url, prev, done) {
const importerContext = this; // eslint-disable-line no-invalid-this
const args = slice.call(arguments);

if (args[1] === "stdin") {
args[1] = resourcePath;
}

return importer.apply(importerContext, args);
};
});
}

module.exports = sassLoader;
29 changes: 29 additions & 0 deletions lib/proxyCustomImporters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

/**
* 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((importer) => {
return function (url, prev, done) {
return importer.apply(
this, // eslint-disable-line no-invalid-this
Array.from(arguments)
.map((arg, i) => i === 1 && arg === "stdin" ? resourcePath : arg)
);
};
});
}

module.exports = proxyCustomImporters;

0 comments on commit f8ea88a

Please sign in to comment.