Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 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
126 changes: 125 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion 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",

Copilot AI Feb 12, 2026

Copy link

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 Expand Up @@ -36,6 +37,7 @@
},
"dependencies": {
"postcss-import": "^16.1.0",
"postcss-selector-parser": "^6.1.2"
"postcss-selector-parser": "^6.1.2",
"ts-morph": "^27.0.2"
}
Comment thread
typicode marked this conversation as resolved.
}
40 changes: 40 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node
import fs = require('node:fs')
Comment thread
typicode marked this conversation as resolved.
Outdated
import { parseArgs } from 'node:util'
import type { Parsed } from './index'
const { parse } = require('./index')

const { positionals } = parseArgs({
args: process.argv.slice(2),
options: {},
strict: true,
allowPositionals: true,
})

if (positionals.length === 0) {
console.error('Error: Please provide a CSS file path')
console.error('Usage: mistcss <path-to-css-file>')
process.exit(1)
}

const cssPath = positionals[0]
const css = fs.readFileSync(cssPath, 'utf-8')

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),
},
])
)

console.log(JSON.stringify(serializable, null, 2))
87 changes: 51 additions & 36 deletions 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

Copilot AI Feb 12, 2026

Copy link

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 All @@ -172,41 +217,8 @@ const _mistcss: PluginCreator<{}> = (_opts = {}) => {
const from = helper.result.opts.from
if (from === undefined || path.basename(from) !== 'mist.css') return

const parsed: Parsed = {}
let current: Parsed[keyof Parsed] = initialParsedValue()
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)
})
})
const css = root.toString()
const parsed = parse(css)
const rendered = render(parsed)
const to = path.resolve(from, '../mist.d.ts')
fs.writeFileSync(to, rendered, 'utf-8')
Expand All @@ -225,4 +237,7 @@ const mistcss: PluginCreator<{}> = (_opts = {}) => {

mistcss.postcss = true

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