@@ -26,7 +26,7 @@ function replace(nodes, tree) {
26
26
// flatten @imports given a basepath, and a tree
27
27
// inline all of the import statements into the tree
28
28
// @return a tree with all statements inlined
29
- function flatten ( file ) {
29
+ function flatten ( file , modules ) {
30
30
var src = fs . readFileSync ( file , 'utf8' ) ;
31
31
32
32
var base = path . dirname ( file ) ;
@@ -68,17 +68,31 @@ function flatten(file) {
68
68
}
69
69
} ) ;
70
70
71
+ if ( modules . indexOf ( filepath ) !== - 1 ) {
72
+ return ;
73
+ }
74
+
75
+ modules . push ( filepath ) ;
76
+
71
77
// run css file through tree -> flatten -> prefix
72
78
// get required module as a css tree
73
79
// replace @import node with new tree
74
- return replace ( self . parent . node , prefix ( name , flatten ( filepath ) ) ) ;
80
+ return replace ( self . parent . node ,
81
+ prefix ( name , flatten ( filepath , modules ) ) ) ;
75
82
}
76
83
77
84
var filepath = path . join ( base , name ) ;
78
85
86
+ if ( modules . indexOf ( filepath ) !== - 1 ) {
87
+ return ;
88
+ }
89
+
90
+ modules . push ( filepath ) ;
91
+
92
+
79
93
// path is a file
80
94
if ( fs . statSync ( filepath ) . isFile ( ) ) {
81
- return replace ( self . parent . node , flatten ( filepath ) ) ;
95
+ return replace ( self . parent . node , flatten ( filepath , modules ) ) ;
82
96
}
83
97
84
98
// path is a dir
@@ -89,7 +103,8 @@ function flatten(file) {
89
103
if ( fs . existsSync ( pkginfo ) ) {
90
104
var info = JSON . parse ( fs . readFileSync ( pkginfo ) ) ;
91
105
filepath = path . join ( base , name , info . style || 'index.css' )
92
- return replace ( self . parent . node , prefix ( info . name , flatten ( filepath ) ) ) ;
106
+ return replace ( self . parent . node ,
107
+ prefix ( info . name , flatten ( filepath , modules ) ) ) ;
93
108
}
94
109
95
110
// no package.json, try index.css in the dir
@@ -98,7 +113,8 @@ function flatten(file) {
98
113
// do not prefix if just loading index.css from a dir
99
114
// allows for easier organization of multi css files without prefixing
100
115
if ( fs . existsSync ( filepath ) ) {
101
- return replace ( self . parent . node , flatten ( filepath ) ) ;
116
+ return replace ( self . parent . node ,
117
+ flatten ( filepath , modules ) ) ;
102
118
}
103
119
} ) ;
104
120
@@ -108,7 +124,6 @@ function flatten(file) {
108
124
// process given file for // @require statements
109
125
// file should be /full/path/to/file.css
110
126
module . exports = function ( file ) {
111
- var base = path . dirname ( file ) ;
112
- return cssp . translate ( flatten ( file ) ) ;
127
+ return cssp . translate ( flatten ( file , [ ] ) ) ;
113
128
} ;
114
129
0 commit comments