-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Ensure layer(…)
on @import
is only removed when @utility
is present
#14783
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1024,7 +1024,7 @@ test( | |
@import './a.1.css' layer(utilities); | ||
@import './a.1.utilities.1.css'; | ||
@import './b.1.css'; | ||
@import './c.1.css'; | ||
@import './c.1.css' layer(utilities); | ||
@import './c.1.utilities.css'; | ||
@import './d.1.css'; | ||
|
||
|
@@ -1545,3 +1545,87 @@ test( | |
`) | ||
}, | ||
) | ||
|
||
test( | ||
'that it attaches the correct layers to the imported files', | ||
{ | ||
fs: { | ||
'package.json': json` | ||
{ | ||
"dependencies": { | ||
"tailwindcss": "workspace:^", | ||
"@tailwindcss/upgrade": "workspace:^" | ||
} | ||
} | ||
`, | ||
'tailwind.config.js': js`module.exports = {}`, | ||
'src/index.css': css` | ||
@import 'tailwindcss/utilities'; | ||
|
||
/* No layer expected */ | ||
@import './my-components.css'; | ||
|
||
/* No layer expected */ | ||
@import './my-utilities.css'; | ||
|
||
/* Expecting a layer */ | ||
@import './my-other.css'; | ||
|
||
@import 'tailwindcss/components'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it important that |
||
`, | ||
'src/my-components.css': css` | ||
@layer components { | ||
.foo { | ||
color: red; | ||
} | ||
} | ||
`, | ||
'src/my-utilities.css': css` | ||
@layer utilities { | ||
.css { | ||
color: red; | ||
} | ||
} | ||
`, | ||
'src/my-other.css': css` | ||
/* All my fonts! */ | ||
@font-face { | ||
} | ||
`, | ||
}, | ||
}, | ||
async ({ fs, exec }) => { | ||
await exec('npx @tailwindcss/upgrade --force') | ||
|
||
expect(await fs.dumpFiles('./src/**/*.css')).toMatchInlineSnapshot(` | ||
" | ||
--- ./src/index.css --- | ||
@import 'tailwindcss/utilities' layer(utilities); | ||
|
||
/* No layer expected */ | ||
@import './my-components.css'; | ||
|
||
/* No layer expected */ | ||
@import './my-utilities.css'; | ||
|
||
/* Expecting a layer */ | ||
@import './my-other.css' layer(utilities); | ||
|
||
--- ./src/my-components.css --- | ||
@utility foo { | ||
color: red; | ||
} | ||
|
||
--- ./src/my-other.css --- | ||
/* All my fonts! */ | ||
@font-face { | ||
} | ||
|
||
--- ./src/my-utilities.css --- | ||
@utility css { | ||
color: red; | ||
} | ||
" | ||
`) | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -151,42 +151,23 @@ async function run() { | |||||||||
|
||||||||||
// Cleanup `@import "…" layer(utilities)` | ||||||||||
for (let sheet of stylesheets) { | ||||||||||
// If the `@import` contains an injected `layer(…)` we need to remove it | ||||||||||
if (!Array.from(sheet.importRules).some((node) => node.raws.tailwind_injected_layer)) { | ||||||||||
continue | ||||||||||
} | ||||||||||
|
||||||||||
let hasAtUtility = false | ||||||||||
|
||||||||||
// Only remove the `layer(…)` next to the import, if any of the children | ||||||||||
// contains an `@utility`. Otherwise the `@utility` will not be top-level. | ||||||||||
{ | ||||||||||
sheet.root.walkAtRules('utility', () => { | ||||||||||
hasAtUtility = true | ||||||||||
return false | ||||||||||
}) | ||||||||||
|
||||||||||
if (!hasAtUtility) { | ||||||||||
for (let child of sheet.descendants()) { | ||||||||||
child.root.walkAtRules('utility', () => { | ||||||||||
hasAtUtility = true | ||||||||||
return false | ||||||||||
}) | ||||||||||
|
||||||||||
if (hasAtUtility) { | ||||||||||
break | ||||||||||
} | ||||||||||
} | ||||||||||
for (let importRule of sheet.importRules) { | ||||||||||
if (!importRule.raws.tailwind_injected_layer) continue | ||||||||||
let importedSheet = stylesheets.find( | ||||||||||
(sheet) => sheet.id === importRule.raws.tailwind_destination_sheet_id, | ||||||||||
) | ||||||||||
if (!importedSheet) continue | ||||||||||
|
||||||||||
// Only remove the `layer(…)` next to the import, if any of the children | ||||||||||
// contains an `@utility`. Otherwise the `@utility` will not be top-level. | ||||||||||
Comment on lines
+161
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
if ( | ||||||||||
!importedSheet.containsRule((node) => node.type === 'atrule' && node.name === 'utility') | ||||||||||
) { | ||||||||||
continue | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
// No `@utility` found, we can keep the `layer(…)` next to the import | ||||||||||
if (!hasAtUtility) continue | ||||||||||
|
||||||||||
for (let importNode of sheet.importRules) { | ||||||||||
if (importNode.raws.tailwind_injected_layer) { | ||||||||||
importNode.params = importNode.params.replace(/ layer\([^)]+\)/, '').trim() | ||||||||||
} | ||||||||||
// Make sure to remove the `layer(…)` from the `@import` at-rule | ||||||||||
importRule.params = importRule.params.replace(/ layer\([^)]+\)/, '').trim() | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this down to be consistent with other changelog entries