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

Insert @defaults at start of stylesheet #14427

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default function expandTailwindAtRules(context) {

if (layerNodes.base) {
layerNodes.base.before(
cloneNodes([...baseNodes, ...defaultNodes], layerNodes.base.source, {
cloneNodes([...defaultNodes, ...baseNodes], layerNodes.base.source, {
layer: 'base',
})
)
Expand Down
24 changes: 11 additions & 13 deletions src/lib/resolveDefaultsAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,20 @@ export default function resolveDefaultsAtRules({ tailwindConfig }) {
}
}

if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
Copy link
Member Author

@adamwathan adamwathan Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional was unnecessary, if you look up at line 94 you can see this code is already inside a block with the exact same conditional.

if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {

if (selectorGroups.size === 0) {
universal.remove()
continue
}
if (selectorGroups.size === 0) {
universal.remove()
continue
}

for (let [, selectors] of selectorGroups) {
let universalRule = postcss.rule({
source: universal.source,
})
for (let [, selectors] of selectorGroups) {
let universalRule = postcss.rule({
source: universal.source,
})

universalRule.selectors = [...selectors]
universalRule.selectors = [...selectors]

universalRule.append(universal.nodes.map((node) => node.clone()))
universal.before(universalRule)
}
universalRule.append(universal.nodes.map((node) => node.clone()))
universal.before(universalRule)
}

universal.remove()
Expand Down
38 changes: 38 additions & 0 deletions tests/resolve-defaults-at-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,44 @@ test('no defaults and apply without @tailwind base', () => {
})
})

test('apply to rule in base layer puts defaults first with optimizeUniversalDefaults', () => {
let config = {
experimental: { optimizeUniversalDefaults: true },
content: [{ raw: html`<div class="my-card"></div>` }],
corePlugins: ['boxShadow'],
}

// Optimize universal defaults doesn't work well with isolated modules
// We require you to use @tailwind base to inject the defaults
let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
input {
@apply shadow;
}
}
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
input {
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
--tw-shadow: 0 0 #0000;
--tw-shadow-colored: 0 0 #0000;
--tw-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
--tw-shadow-colored: 0 1px 3px 0 var(--tw-shadow-color),
0 1px 2px -1px var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
var(--tw-shadow);
}
`)
})
})

test('optimize universal defaults groups :has separately', () => {
let config = {
experimental: { optimizeUniversalDefaults: true },
Expand Down
Loading