Skip to content

Commit

Permalink
fix: ignoring nested non-first rules
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Meinhardt <[email protected]>
  • Loading branch information
ameinhardt committed May 7, 2024
1 parent d7f4cd0 commit ef1cb0f
Show file tree
Hide file tree
Showing 3 changed files with 4,207 additions and 2,760 deletions.
89 changes: 29 additions & 60 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,17 @@ const defaultOptions = {
darkTheme: 'dark',
}

/**
* Retrieves all non-atrule nodes from the provided node and its children.
* @param {ChildNode} node - The node to traverse.
* @returns {Rule[]} - An array containing non-atrule nodes.
*/
const notAtruleNode = (node: ChildNode): Rule[] => {
const collectedRules: Rule[] = []

/**
* Recursively collects non-atrule nodes.
* @param {ChildNode} currentNode - The current node to examine.
*/
const collectNonAtrules = (currentNode: ChildNode) => {
if (currentNode.type === 'atrule') {
for (const child of currentNode.nodes) {
collectNonAtrules(child)
}
} else {
collectedRules.push(currentNode as Rule)
}
}

collectNonAtrules(node)

return collectedRules;
}

export const presetDaisy = (
options: Partial<typeof defaultOptions> = {},
): Preset => {
options = {...defaultOptions, ...options}

const rules = new Map<string, string>()
const keyframes: string[] = []
const supports: string[] = []
const specialRules: Record<string, string[]> = {
keyframes: [],
supports: []
};
const nodes: Rule[] = []

const styles = [
options.styled ? styled : unstyled
Expand All @@ -73,26 +49,26 @@ export const presetDaisy = (
styles.push(utilities, utilitiesUnstyled, utilitiesStyled)
}

for (const node of styles.flatMap(style => process(style).root.nodes as ChildNode[])) {
const isAtRule = node.type === 'atrule'
// @keyframes
if (isAtRule && node.name === 'keyframes') {
keyframes.push(String(node))
continue
}

if (isAtRule && node.name === 'supports') {
supports.push(String(node))
continue
}

if (isAtRule && node.name !== 'supports' && node.name === 'keyframes') {
continue
const categorizeRules = (node: ChildNode) => {
if (node.type === 'rule') {
nodes.push(node);
} else if(node.type === 'atrule') {
if(Array.isArray(specialRules[node.name])) {
specialRules[node.name]!.push(String(node));
} else if(node.nodes) {
// ignore and keep traversing, e.g. for @media
for (const child of node.nodes) {
categorizeRules(child);
}
}
}
};
styles
.flatMap((style) => process(style).root.nodes as ChildNode[])
.forEach((node) => categorizeRules(node));

// Unwrap @media if necessary
const rule = notAtruleNode(node)[0]!
const selector = rule.selectors[0]!
for (const node of nodes) {
const selector = node.selectors[0]!
const tokens = tokenize(selector)
const token = tokens[0]!
let base = ''
Expand All @@ -118,20 +94,13 @@ export const presetDaisy = (
: (tokens[2] as ClassToken).name
}

rules.set(base, (rules.get(base) ?? '') + String(rule) + '\n')
rules.set(base, (rules.get(base) ?? '') + String(node) + '\n')
}

const preflights: Preflight[] = [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => keyframes.join('\n'),
layer: 'daisy-keyframes',
},
{
getCSS: () => supports.join('\n'),
layer: 'daisy-supports',
}
];
const preflights: Preflight[] = Object.entries(specialRules).map(([key, value]) => ({
getCSS: () => value.join('\n'),
layer: `daisy-${key}}`,
}));

if (options.base) {
preflights.unshift({
Expand Down Expand Up @@ -161,7 +130,7 @@ export const presetDaisy = (
},
themes,
)

return {
name: 'unocss-preset-daisy',
preflights,
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
"index.d.ts"
],
"dependencies": {
"autoprefixer": "^10.4.15",
"autoprefixer": "^10.4.19",
"camelcase": "^8.0.0",
"parsel-js": "^1.1.2",
"postcss": "^8.4.29",
"postcss": "^8.4.38",
"postcss-js": "^4.0.1"
},
"peerDependencies": {
"daisyui": "^4.4.23",
"unocss": ">0.58.1"
"daisyui": "^4.10.5",
"unocss": ">0.59.4"
},
"devDependencies": {
"@iconify-json/octicon": "^1.1.48",
"@iconify-json/octicon": "^1.1.54",
"@sindresorhus/tsconfig": "^5.0.0",
"@types/postcss-js": "^4.0.1",
"@unocss/reset": "^0.58.2",
"typescript": "^5.2.2",
"vite": "^5.0.10",
"xo": "^0.56.0"
"@unocss/reset": "^0.59.4",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"xo": "^0.58.0"
}
}
Loading

0 comments on commit ef1cb0f

Please sign in to comment.