Skip to content

Commit f99379c

Browse files
authored
check import cycles (#535)
* check cycles * Update lib/parse-styles.js * add tests
1 parent eaff3c3 commit f99379c

File tree

11 files changed

+119
-10
lines changed

11 files changed

+119
-10
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function AtImport(options) {
6363
state,
6464
[],
6565
[],
66-
"",
66+
[],
6767
postcss
6868
)
6969

lib/parse-styles.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async function resolveImportId(result, stmt, options, state, postcss) {
9191
)
9292

9393
return
94-
} else if (dataURL.isValid(stmt.from)) {
94+
} else if (dataURL.isValid(stmt.from.slice(-1))) {
9595
// Data urls can't be used a base url to resolve imports.
9696
// When the parent statement has a data url
9797
// and the current statement doesn't have a data url we ignore the statement.
@@ -148,7 +148,7 @@ async function loadImportContent(
148148
postcss
149149
) {
150150
const atRule = stmt.node
151-
const { media, layer } = stmt
151+
const { media, layer, from } = stmt
152152

153153
assignLayerNames(layer, atRule, state, options)
154154

@@ -168,6 +168,10 @@ async function loadImportContent(
168168
state.importedFiles[filename][media][layer] = true
169169
}
170170

171+
if (from.includes(filename)) {
172+
return
173+
}
174+
171175
const content = await options.load(filename, options)
172176

173177
if (content.trim() === "" && options.warnOnEmpty) {
@@ -215,7 +219,7 @@ async function loadImportContent(
215219
state,
216220
media,
217221
layer,
218-
filename,
222+
[...from, filename],
219223
postcss
220224
)
221225
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "cyclical-a.css";
2+
@import "cyclical-b.css";
3+
4+
@import "cyclical-screen.css" screen;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
.b {
4+
color: red;
5+
}
6+
7+
.a {
8+
color: blue;
9+
}
10+
11+
@media screen and all {
12+
13+
.a {
14+
color: aquamarine;
15+
}
16+
}
17+
18+
@media screen {
19+
20+
.a {
21+
color: pink;
22+
}
23+
}

test/fixtures/cyclical.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "cyclical-a.css";
2+
@import "cyclical-b.css";
3+
4+
@import "cyclical-screen.css" screen;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
.b {
4+
color: red;
5+
}
6+
7+
.a {
8+
color: blue;
9+
}
10+
11+
.a {
12+
color: blue;
13+
}
14+
15+
.b {
16+
color: red;
17+
}
18+
19+
@media screen and all {
20+
21+
.a {
22+
color: aquamarine;
23+
}
24+
}
25+
26+
@media screen {
27+
28+
.a {
29+
color: pink;
30+
}
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url(cyclical-b.css);
2+
3+
.a {
4+
color: blue;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url(cyclical-screen.css) screen;
2+
3+
.a {
4+
color: aquamarine;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url(cyclical-a.css);
2+
3+
.b {
4+
color: red;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url(cyclical-all.css) all;
2+
3+
.a {
4+
color: pink;
5+
}

0 commit comments

Comments
 (0)