Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.6",
"description": "",
"exports": "./lib/index.js",
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The exports field is a string, which restricts consumers to importing only the package root. That makes the documented mistcss/lib/stats (and any other subpath like lib/stats) unavailable in Node when exports is present. If stats is intended to be a public utility, switch exports to an object and explicitly export ./stats (and any other public subpaths), while keeping . pointing at ./lib/index.js.

Suggested change
"exports": "./lib/index.js",
"exports": {
".": "./lib/index.js",
"./lib/stats": "./lib/stats.js"
},

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot don't export stats

"bin": "./lib/bin.js",
"scripts": {
"build": "rm -rf lib && tsc",
"format": "prettier --write .",
Expand Down
52 changes: 52 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node
import { parseArgs } from 'node:util'
import type { Parsed } from './index'
const { parse } = require('./index')

async function main() {
// Parse command line arguments (no args expected for now)
parseArgs({
args: process.argv.slice(2),
options: {},
strict: true,
})

// Read CSS from stdin
let css = ''

if (process.stdin.isTTY) {
console.error('Error: Please provide CSS via stdin')
console.error('Usage: mistcss < input.css')
process.exit(1)
}

for await (const chunk of process.stdin) {
css += chunk
}

// Parse the CSS
const parsed = parse(css)

// Convert Sets to Arrays for JSON serialization
const serializable = Object.fromEntries(
(Object.entries(parsed) as [string, Parsed[string]][]).map(([key, value]) => [
key,
{
...value,
attributes: Object.fromEntries(
Object.entries(value.attributes).map(([k, v]) => [k, Array.from(v)])
),
booleanAttributes: Array.from(value.booleanAttributes),
properties: Array.from(value.properties),
},
])
)

// Output the parsed result as JSON
console.log(JSON.stringify(serializable, null, 2))
}

main().catch((err) => {
console.error('Error:', err.message)
process.exit(1)
})
50 changes: 49 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs = require('node:fs')
import { type PluginCreator } from 'postcss'
import postcss = require('postcss')
import selectorParser = require('postcss-selector-parser')
import atImport = require('postcss-import')
import path = require('node:path')
Expand All @@ -14,7 +15,7 @@ declare module 'postcss-selector-parser' {
}
}

type Parsed = Record<
export type Parsed = Record<
string,
{
tag: string
Expand Down Expand Up @@ -164,6 +165,50 @@ function initialParsedValue(): Parsed[keyof Parsed] {
}
}

export function parse(css: string): Parsed {
const parsed: Parsed = {}
let current: Parsed[keyof Parsed] = initialParsedValue()

// Parse the CSS using postcss
const root = postcss.parse(css)

root.walkRules((rule) => {
selectorParser((selectors) => {
selectors.walk((selector) => {
if (selector.type === 'tag') {
current = parsed[key(selector)] = initialParsedValue()
current.tag = selector.toString().toLowerCase()
const next = selector.next()
if (next?.type === 'attribute') {
const { attribute, value } = next as selectorParser.Attribute
if (value) current.rootAttribute = attribute
}
}

if (selector.type === 'attribute') {
const { attribute, value } = selector as selectorParser.Attribute
if (value) {
const values = (current.attributes[attribute] ??=
new Set<string>())
values.add(value)
} else {
current.booleanAttributes.add(attribute)
}
}
})
}).processSync(rule.selector, {
lossless: false,
})

rule.walkDecls(({ prop }) => {
if (prop.startsWith('--') && prop !== '--apply')
current.properties.add(prop)
})
Comment on lines +209 to +219
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

parse() applies declarations to whatever selector was processed last in the rule. For rules with multiple selectors (e.g. button, a { --x: 1 }), only the last selector will receive the collected custom properties/attributes because current is a single mutable accumulator. Consider collecting all matched entries for a rule during selector parsing, then applying walkDecls results to each matched entry (or iterating selectors separately) so multi-selector rules are handled correctly.

Copilot uses AI. Check for mistakes.
})

return parsed
}

const _mistcss: PluginCreator<{}> = (_opts = {}) => {
return {
postcssPlugin: '_mistcss',
Expand Down Expand Up @@ -225,4 +270,7 @@ const mistcss: PluginCreator<{}> = (_opts = {}) => {

mistcss.postcss = true

export { mistcss as default }
module.exports = mistcss
module.exports.parse = parse
module.exports.default = mistcss