Skip to content

Commit e8a4942

Browse files
Allow using a custom sass importer option (#15)
* Allow using a custom sass importer option * Update index.js * Support undefined opts * Linting * strip #sass suffix from importer args
1 parent 2359913 commit e8a4942

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

index.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
2222
// sass resolve cache
2323
const cache = {};
2424

25+
// replication of the default sass file importer
26+
const defaultSassImporter = (id, parentId, done) => {
27+
// resolve the absolute parent
28+
const parent = pathResolve(parentId);
29+
30+
// cwds is the list of all directories to search
31+
const cwds = [dirname(parent)].concat(includePaths).map(includePath => pathResolve(includePath));
32+
33+
cwds.reduce(
34+
// resolve the first available files
35+
(promise, cwd) => promise.catch(
36+
() => sassResolve(id, { cwd, cache, readFile: true })
37+
),
38+
Promise.reject()
39+
).then(
40+
({ file, contents }) => {
41+
// pass the file and contents back to sass
42+
done({ file, contents });
43+
},
44+
importerError => {
45+
// otherwise, pass the error
46+
done(importerError);
47+
}
48+
);
49+
}
50+
51+
// sass importer
52+
const sassImporter = opts && opts.importer || defaultSassImporter
53+
2554
return new Promise(
2655
// promise sass results
2756
(resolve, reject) => sassEngine.render(
@@ -31,31 +60,23 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
3160
outFile: postConfig.from,
3261
data: postCSS,
3362
importer(id, parentId, done) {
34-
// resolve the absolute parent
35-
const parent = pathResolve(parentId);
36-
37-
// cwds is the list of all directories to search
38-
const cwds = [dirname(parent)].concat(includePaths).map(includePath => pathResolve(includePath));
39-
40-
cwds.reduce(
41-
// resolve the first available files
42-
(promise, cwd) => promise.catch(
43-
() => sassResolve(id, { cwd, cache, readFile: true })
44-
),
45-
Promise.reject()
46-
).then(
47-
({ file, contents }) => {
63+
const doneWrap = (importerResult) => {
64+
const file = importerResult && importerResult.file
65+
if (file) {
66+
const parent = pathResolve(parentId);
67+
4868
// push the dependency to watch tasks
4969
result.messages.push({ type: 'dependency', file, parent });
50-
51-
// pass the file and contents back to sass
52-
done({ file, contents });
53-
},
54-
importerError => {
55-
// otherwise, pass the error
56-
done(importerError);
5770
}
58-
);
71+
72+
done(importerResult)
73+
}
74+
75+
// strip the #sass suffix we added
76+
const prev = parentId.replace(/#sass$/, '')
77+
78+
// call the sass importer and catch its output
79+
sassImporter.call(this, id, prev, doneWrap)
5980
}
6081
}),
6182
(sassError, sassResult) => sassError ? reject(sassError) : resolve(sassResult)

0 commit comments

Comments
 (0)