Skip to content

Commit

Permalink
check import cycles (#535)
Browse files Browse the repository at this point in the history
* check cycles

* Update lib/parse-styles.js

* add tests
  • Loading branch information
romainmenke committed Sep 30, 2023
1 parent eaff3c3 commit f99379c
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -63,7 +63,7 @@ function AtImport(options) {
state,
[],
[],
"",
[],
postcss
)

Expand Down
10 changes: 7 additions & 3 deletions lib/parse-styles.js
Expand Up @@ -91,7 +91,7 @@ async function resolveImportId(result, stmt, options, state, postcss) {
)

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

assignLayerNames(layer, atRule, state, options)

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

if (from.includes(filename)) {
return
}

const content = await options.load(filename, options)

if (content.trim() === "" && options.warnOnEmpty) {
Expand Down Expand Up @@ -215,7 +219,7 @@ async function loadImportContent(
state,
media,
layer,
filename,
[...from, filename],
postcss
)
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/cyclical-skip-duplicates.css
@@ -0,0 +1,4 @@
@import "cyclical-a.css";
@import "cyclical-b.css";

@import "cyclical-screen.css" screen;
23 changes: 23 additions & 0 deletions test/fixtures/cyclical-skip-duplicates.expected.css
@@ -0,0 +1,23 @@


.b {
color: red;
}

.a {
color: blue;
}

@media screen and all {

.a {
color: aquamarine;
}
}

@media screen {

.a {
color: pink;
}
}
4 changes: 4 additions & 0 deletions test/fixtures/cyclical.css
@@ -0,0 +1,4 @@
@import "cyclical-a.css";
@import "cyclical-b.css";

@import "cyclical-screen.css" screen;
31 changes: 31 additions & 0 deletions test/fixtures/cyclical.expected.css
@@ -0,0 +1,31 @@


.b {
color: red;
}

.a {
color: blue;
}

.a {
color: blue;
}

.b {
color: red;
}

@media screen and all {

.a {
color: aquamarine;
}
}

@media screen {

.a {
color: pink;
}
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-a.css
@@ -0,0 +1,5 @@
@import url(cyclical-b.css);

.a {
color: blue;
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-all.css
@@ -0,0 +1,5 @@
@import url(cyclical-screen.css) screen;

.a {
color: aquamarine;
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-b.css
@@ -0,0 +1,5 @@
@import url(cyclical-a.css);

.b {
color: red;
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-screen.css
@@ -0,0 +1,5 @@
@import url(cyclical-all.css) all;

.a {
color: pink;
}
35 changes: 29 additions & 6 deletions test/import.js
Expand Up @@ -13,15 +13,38 @@ const atImport = require("..")
// internal tooling
const checkFixture = require("./helpers/check-fixture")

test("should import stylsheets", checkFixture, "simple")
test("should import stylesheets", checkFixture, "simple")

test("should not import a stylsheet twice", checkFixture, "no-duplicate")
test("should not import a stylesheet twice", checkFixture, "no-duplicate")

test("should be able to import a stylsheet twice", checkFixture, "duplicates", {
skipDuplicates: false,
})
test(
"should be able to import a stylesheet twice",
checkFixture,
"duplicates",
{
skipDuplicates: false,
}
)

test(
"should be able to import a stylesheet with cyclical dependencies",
checkFixture,
"cyclical",
{
skipDuplicates: false,
}
)

test(
"should be able to import a stylesheet with cyclical dependencies and skip duplicates is true",
checkFixture,
"cyclical-skip-duplicates",
{
skipDuplicates: true,
}
)

test("should import stylsheets with same content", checkFixture, "same")
test("should import stylesheets with same content", checkFixture, "same")

test("should ignore & adjust external import", checkFixture, "ignore")

Expand Down

0 comments on commit f99379c

Please sign in to comment.