@@ -22,6 +22,35 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
22
22
// sass resolve cache
23
23
const cache = { } ;
24
24
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
+
25
54
return new Promise (
26
55
// promise sass results
27
56
( resolve , reject ) => sassEngine . render (
@@ -31,31 +60,23 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
31
60
outFile : postConfig . from ,
32
61
data : postCSS ,
33
62
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
+
48
68
// push the dependency to watch tasks
49
69
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 ) ;
57
70
}
58
- ) ;
71
+
72
+ done ( importerResult )
73
+ }
74
+
75
+ // strip the #sass suffix we added
76
+ const prev = parentId . replace ( / # s a s s $ / , '' )
77
+
78
+ // call the sass importer and catch its output
79
+ sassImporter . call ( this , id , prev , doneWrap )
59
80
}
60
81
} ) ,
61
82
( sassError , sassResult ) => sassError ? reject ( sassError ) : resolve ( sassResult )
0 commit comments