Skip to content

Commit 439ab86

Browse files
committed
only import a module's stylesheet the first time a module is seen in the tree
1 parent d4261d2 commit 439ab86

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function replace(nodes, tree) {
2626
// flatten @imports given a basepath, and a tree
2727
// inline all of the import statements into the tree
2828
// @return a tree with all statements inlined
29-
function flatten(file) {
29+
function flatten(file, modules) {
3030
var src = fs.readFileSync(file, 'utf8');
3131

3232
var base = path.dirname(file);
@@ -68,17 +68,31 @@ function flatten(file) {
6868
}
6969
});
7070

71+
if (modules.indexOf(filepath) !== -1) {
72+
return;
73+
}
74+
75+
modules.push(filepath);
76+
7177
// run css file through tree -> flatten -> prefix
7278
// get required module as a css tree
7379
// 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)));
7582
}
7683

7784
var filepath = path.join(base, name);
7885

86+
if (modules.indexOf(filepath) !== -1) {
87+
return;
88+
}
89+
90+
modules.push(filepath);
91+
92+
7993
// path is a file
8094
if (fs.statSync(filepath).isFile()) {
81-
return replace(self.parent.node, flatten(filepath));
95+
return replace(self.parent.node, flatten(filepath, modules));
8296
}
8397

8498
// path is a dir
@@ -89,7 +103,8 @@ function flatten(file) {
89103
if (fs.existsSync(pkginfo)) {
90104
var info = JSON.parse(fs.readFileSync(pkginfo));
91105
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)));
93108
}
94109

95110
// no package.json, try index.css in the dir
@@ -98,7 +113,8 @@ function flatten(file) {
98113
// do not prefix if just loading index.css from a dir
99114
// allows for easier organization of multi css files without prefixing
100115
if (fs.existsSync(filepath)) {
101-
return replace(self.parent.node, flatten(filepath));
116+
return replace(self.parent.node,
117+
flatten(filepath, modules));
102118
}
103119
});
104120

@@ -108,7 +124,6 @@ function flatten(file) {
108124
// process given file for // @require statements
109125
// file should be /full/path/to/file.css
110126
module.exports = function(file) {
111-
var base = path.dirname(file);
112-
return cssp.translate(flatten(file));
127+
return cssp.translate(flatten(file, []));
113128
};
114129

0 commit comments

Comments
 (0)