Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check import cycles #535

Merged
merged 4 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function AtImport(options) {
state,
[],
[],
"",
[],
postcss
)

Expand Down
8 changes: 5 additions & 3 deletions lib/parse-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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 @@ -147,7 +147,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 @@ -165,6 +165,8 @@ async function loadImportContent(
state.importedFiles[filename][media] = {}
}
state.importedFiles[filename][media][layer] = true
} else if (from.includes(filename)) {
return
}
romainmenke marked this conversation as resolved.
Show resolved Hide resolved

const content = await options.load(filename, options)
Expand Down Expand Up @@ -214,7 +216,7 @@ async function loadImportContent(
state,
media,
layer,
filename,
[...from, filename],
postcss
)
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cyclical.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "cyclical-a.css";
@import "cyclical-b.css";
17 changes: 17 additions & 0 deletions test/fixtures/cyclical.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


.b {
color: red;
}

.a {
color: blue;
}

.a {
color: blue;
}

.b {
color: red;
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import url(cyclical-b.css);

.a {
color: blue;
}
5 changes: 5 additions & 0 deletions test/fixtures/imports/cyclical-b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import url(cyclical-a.css);

.b {
color: red;
}
26 changes: 20 additions & 6 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ 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 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