frutjam 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +172 -172
  3. package/dist/base/reboot/object.js +1 -1
  4. package/dist/base/tokens/object.js +1 -1
  5. package/dist/base.css +4 -4
  6. package/dist/components/accordion/object.js +1 -1
  7. package/dist/components/accordion.css +2 -2
  8. package/dist/components/alert.css +2 -2
  9. package/dist/components/avatar/object.js +1 -1
  10. package/dist/components/avatar.css +2 -2
  11. package/dist/components/badge.css +2 -2
  12. package/dist/components/breadcrumb.css +2 -2
  13. package/dist/components/button/object.js +1 -1
  14. package/dist/components/button.css +4 -4
  15. package/dist/components/card/object.js +1 -1
  16. package/dist/components/card.css +2 -2
  17. package/dist/components/carousel/object.js +1 -1
  18. package/dist/components/carousel.css +2 -2
  19. package/dist/components/chat/object.js +1 -1
  20. package/dist/components/chat.css +2 -2
  21. package/dist/components/checkbox/object.js +1 -1
  22. package/dist/components/checkbox.css +2 -2
  23. package/dist/components/collapsible.css +2 -2
  24. package/dist/components/combobox/object.js +1 -1
  25. package/dist/components/combobox.css +2 -2
  26. package/dist/components/countdown/object.js +1 -1
  27. package/dist/components/countdown.css +2 -2
  28. package/dist/components/diff/object.js +1 -1
  29. package/dist/components/diff.css +2 -2
  30. package/dist/components/divider/object.js +1 -1
  31. package/dist/components/divider.css +2 -2
  32. package/dist/components/drawer/object.js +1 -1
  33. package/dist/components/drawer.css +2 -2
  34. package/dist/components/footer/object.js +1 -1
  35. package/dist/components/footer.css +2 -2
  36. package/dist/components/header/object.js +1 -1
  37. package/dist/components/header.css +2 -2
  38. package/dist/components/hero.css +2 -2
  39. package/dist/components/indicator.css +2 -2
  40. package/dist/components/input/object.js +1 -1
  41. package/dist/components/input.css +2 -2
  42. package/dist/components/join.css +2 -2
  43. package/dist/components/kbd.css +2 -2
  44. package/dist/components/link.css +2 -2
  45. package/dist/components/loading/object.js +1 -1
  46. package/dist/components/loading.css +2 -2
  47. package/dist/components/mask.css +2 -2
  48. package/dist/components/menu/object.js +1 -1
  49. package/dist/components/menu.css +2 -2
  50. package/dist/components/modal/object.js +1 -1
  51. package/dist/components/modal.css +2 -2
  52. package/dist/components/navbar.css +2 -2
  53. package/dist/components/popover/object.js +1 -1
  54. package/dist/components/popover.css +2 -2
  55. package/dist/components/progress/object.js +1 -1
  56. package/dist/components/progress.css +2 -2
  57. package/dist/components/radial-progress/object.js +1 -1
  58. package/dist/components/radial-progress.css +2 -2
  59. package/dist/components/radio/object.js +1 -1
  60. package/dist/components/radio.css +2 -2
  61. package/dist/components/range/object.js +1 -1
  62. package/dist/components/range.css +2 -2
  63. package/dist/components/rating/object.js +1 -1
  64. package/dist/components/rating.css +2 -2
  65. package/dist/components/select/object.js +1 -1
  66. package/dist/components/select.css +2 -2
  67. package/dist/components/sidebar.css +2 -2
  68. package/dist/components/skeleton/object.js +1 -1
  69. package/dist/components/skeleton.css +2 -2
  70. package/dist/components/stat.css +2 -2
  71. package/dist/components/status.css +2 -2
  72. package/dist/components/steps.css +2 -2
  73. package/dist/components/surface.css +2 -2
  74. package/dist/components/swap/object.js +1 -1
  75. package/dist/components/swap.css +2 -2
  76. package/dist/components/table/object.js +1 -1
  77. package/dist/components/table.css +2 -2
  78. package/dist/components/tabs.css +2 -2
  79. package/dist/components/tag.css +2 -2
  80. package/dist/components/textarea.css +2 -2
  81. package/dist/components/timeline/object.js +1 -1
  82. package/dist/components/timeline.css +2 -2
  83. package/dist/components/toast.css +2 -2
  84. package/dist/components/toggle.css +2 -2
  85. package/dist/components/tooltip/object.js +1 -1
  86. package/dist/components/tooltip.css +2 -2
  87. package/dist/frutjam.min.css +6 -6
  88. package/dist/plugin.js +56 -9
  89. package/dist/themes/darkberry.css +1 -1
  90. package/dist/themes/snowberry.css +1 -1
  91. package/dist/utilities/headings/object.js +1 -1
  92. package/dist/utilities/headings.css +2 -2
  93. package/dist/utilities/para.css +2 -2
  94. package/dist/utilities/typography/object.js +1 -1
  95. package/dist/utilities/typography.css +2 -2
  96. package/dist/utilities/wrapper.css +2 -2
  97. package/index.js +315 -272
  98. package/package.json +101 -101
  99. package/packages/js/carousel.js +2 -1
  100. package/packages/js/collapsible.js +9 -3
  101. package/packages/js/combobox.js +23 -1
  102. package/packages/js/tabs.js +58 -2
  103. package/packages/js/toast.js +8 -0
  104. package/packages/react/useCollapsible.js +11 -3
  105. package/packages/react/useCombobox.js +14 -2
  106. package/packages/react/useTabs.js +33 -3
package/index.js CHANGED
@@ -1,272 +1,315 @@
1
- import { readFileSync, readdirSync, statSync, existsSync } from "fs"
2
- import { join, dirname, basename, extname } from "path"
3
- import { fileURLToPath } from "url"
4
- import postcss from "postcss"
5
-
6
- const __filename = fileURLToPath(import.meta.url)
7
- const __dir = dirname(__filename)
8
- const srcDir = join(__dir, "src")
9
- const VERSION = JSON.parse(readFileSync(join(__dir, "package.json"), "utf8")).version
10
-
11
- // ── Keys that must be injected after Tailwind processes layers ───────────────
12
- // These files conflict with @layer utilities from Tailwind plugins (e.g. typography),
13
- // so they are injected via OnceExit into @layer utilities (or root if not found).
14
- const LATE_INJECT = new Set(['typography'])
15
-
16
- // ── Registry (built once at startup, not per build) ──────────────────────────
17
-
18
- function buildRegistry() {
19
- const registry = {}
20
-
21
- // Components: src/components/*/base.css — keyed by folder name
22
- const componentsDir = join(srcDir, "components")
23
- for (const name of readdirSync(componentsDir)) {
24
- const entry = join(componentsDir, name, "base.css")
25
- if (statSync(join(componentsDir, name)).isDirectory() && existsSync(entry)) {
26
- registry[name] = entry
27
- }
28
- }
29
-
30
- // Utilities: src/utilities/*.css — keyed by filename without extension
31
- const utilitiesDir = join(srcDir, "utilities")
32
- for (const file of readdirSync(utilitiesDir)) {
33
- if (extname(file) === ".css") {
34
- registry[basename(file, ".css")] = join(utilitiesDir, file)
35
- }
36
- }
37
-
38
- return registry
39
- }
40
-
41
- const REGISTRY = buildRegistry()
42
-
43
- // ── Helpers ─────────────────────────────────────────────────────────────────
44
-
45
- function readFile(p) { return readFileSync(p, "utf8") }
46
-
47
- function parseList(val) {
48
- if (!val) return []
49
- if (Array.isArray(val)) return val.map(s => s.trim()).filter(Boolean)
50
- return String(val).split(",").map(s => s.trim()).filter(Boolean)
51
- }
52
-
53
- function applyPrefix(css, prefix) {
54
- if (!prefix) return css
55
-
56
- css = css.replace(
57
- /@utility\s+([\w-]+\*?)\s*\{/g,
58
- (_, name) => `@utility ${prefix}-${name} {`
59
- )
60
- css = css.replace(
61
- /@apply\s+([^;{}\n]+)/g,
62
- (_, classes) => `@apply ${classes.trim().split(/\s+/).map(c => `${prefix}-${c}`).join(' ')}`
63
- )
64
- css = css.replace(
65
- /\/\*[\s\S]*?\*\/|"[^"]*"|'[^']*'|\.(([a-z][a-z0-9]*-?)+)/g,
66
- (match, name) => name ? `.${prefix}-${name}` : match
67
- )
68
- return css
69
- }
70
-
71
- function buildUtilityMap(css) {
72
- const map = new Map()
73
- const pattern = /@utility\s+([\w-]+(?:\s*\*)?)\s*\{/g
74
- let match
75
- while ((match = pattern.exec(css)) !== null) {
76
- const name = match[1].trim()
77
- if (map.has(name)) continue
78
- let depth = 0, bodyStart = -1
79
- for (let i = match.index + match[0].length - 1; i < css.length; i++) {
80
- if (css[i] === '{') {
81
- if (depth === 0) bodyStart = i + 1
82
- depth++
83
- } else if (css[i] === '}') {
84
- if (--depth === 0) { map.set(name, css.slice(bodyStart, i)); break }
85
- }
86
- }
87
- }
88
- return map
89
- }
90
-
91
- function resolveCopy(css, source = css) {
92
- const map = source instanceof Map ? source : buildUtilityMap(source)
93
- return css.replace(/@copy\s+([\w-]+)\s*;/g, (original, name) => map.get(name) ?? original)
94
- }
95
-
96
- function resolveImports(css, baseDir) {
97
- return css.replace(
98
- /@import\s+["']([^"']+)["'];?\s*/g,
99
- (_, importPath) => {
100
- const fullPath = join(baseDir, importPath)
101
- const importedDir = dirname(fullPath)
102
- const importedCSS = readFile(fullPath)
103
- return resolveImports(importedCSS, importedDir)
104
- }
105
- )
106
- }
107
-
108
- function buildCustomThemes(themes) {
109
- if (!themes || Object.keys(themes).length === 0) return ""
110
-
111
- return Object.entries(themes)
112
- .map(([name, vars]) => {
113
- const varString = Object.entries(vars)
114
- .map(([k, v]) => ` ${k}: ${v};`)
115
- .join("\n")
116
- return `[data-theme="${name}"] {\n${varString}\n}`
117
- })
118
- .join("\n\n")
119
- }
120
-
121
- // ── Base layers (inlined — no entry CSS files needed) ───────────────────────
122
-
123
- const BASE_FULL = [
124
- "base/reboot.css",
125
- "theme/base.css",
126
- ]
127
-
128
- const BASE_CORE = [
129
- "base/variants.css",
130
- "base/tokens.css",
131
- "theme/base.css",
132
- ]
133
-
134
- // ── Build ────────────────────────────────────────────────────────────────────
135
-
136
- function buildCSS(prefix, themes, reset, rootSelector, include, exclude) {
137
- // Resolve which components+utilities to include
138
- let keys = Object.keys(REGISTRY)
139
- if (include.length > 0) {
140
- keys = include.filter(k => REGISTRY[k])
141
- } else if (exclude.length > 0) {
142
- const excludeSet = new Set(exclude)
143
- keys = keys.filter(k => !excludeSet.has(k))
144
- }
145
- // LATE_INJECT keys are handled separately via OnceExit — skip them here
146
- keys = keys.filter(k => !LATE_INJECT.has(k))
147
-
148
- // Base layer (full with reboot, or core without)
149
- const baseFiles = reset ? BASE_FULL : BASE_CORE
150
- const baseCss = baseFiles
151
- .map(f => resolveImports(readFile(join(srcDir, f)), join(srcDir, dirname(f))))
152
- .join("\n")
153
-
154
- // Components + utilities
155
- const modulesCss = keys
156
- .map(k => resolveImports(readFile(REGISTRY[k]), dirname(REGISTRY[k])))
157
- .join("\n")
158
-
159
- const combined = baseCss + "\n" + modulesCss
160
- const utilMap = buildUtilityMap(combined)
161
- let css = resolveCopy(combined, utilMap)
162
-
163
- if (rootSelector !== ":root") {
164
- css = css.replace(/:root\b/g, rootSelector)
165
- }
166
-
167
- css = applyPrefix(css, prefix)
168
-
169
- const customThemes = buildCustomThemes(themes)
170
- if (customThemes) css += "\n\n" + customThemes
171
-
172
- return { css, utilMap }
173
- }
174
-
175
- function buildLateCSS(prefix, include, exclude, utilMap = new Map()) {
176
- let keys = [...LATE_INJECT].filter(k => REGISTRY[k])
177
- if (include.length > 0) {
178
- keys = keys.filter(k => include.includes(k))
179
- } else if (exclude.length > 0) {
180
- const excludeSet = new Set(exclude)
181
- keys = keys.filter(k => !excludeSet.has(k))
182
- }
183
- if (keys.length === 0) return ""
184
- const raw = keys
185
- .map(k => resolveImports(readFile(REGISTRY[k]), dirname(REGISTRY[k])))
186
- .join("\n")
187
- const css = resolveCopy(raw, utilMap)
188
- return applyPrefix(css, prefix)
189
- }
190
-
191
- // ── Plugin ───────────────────────────────────────────────────────────────────
192
-
193
- function parseAtPluginOptions(atRule) {
194
- const opts = {}
195
- atRule.each(node => {
196
- if (node.type !== "decl") return
197
- let val = node.value.trim()
198
- if (val === "true") val = true
199
- else if (val === "false") val = false
200
- else if (
201
- (val.startsWith('"') && val.endsWith('"')) ||
202
- (val.startsWith("'") && val.endsWith("'"))
203
- ) val = val.slice(1, -1)
204
- opts[node.prop.trim()] = val
205
- })
206
- return opts
207
- }
208
-
209
- export { buildUtilityMap, resolveCopy }
210
-
211
- export default function frutjam(options = {}) {
212
- let _lateCss = ""
213
-
214
- return {
215
- postcssPlugin: "frutjam",
216
- Once(root) {
217
- // Collect @plugin "frutjam" { ... } blocks from the CSS
218
- const atPluginNodes = []
219
- root.each(node => {
220
- if (
221
- node.type === "atrule" &&
222
- node.name === "plugin" &&
223
- /^["']frutjam["']/.test(node.params.trim()) &&
224
- node.nodes
225
- ) atPluginNodes.push(node)
226
- })
227
-
228
- // CSS-level options override factory options
229
- let cssOptions = {}
230
- for (const node of atPluginNodes) {
231
- Object.assign(cssOptions, parseAtPluginOptions(node))
232
- node.remove()
233
- }
234
- const merged = { ...options, ...cssOptions }
235
-
236
- const prefix = typeof merged.prefix === "string" ? merged.prefix.trim() : ""
237
- const themes = typeof merged.themes === "object" ? merged.themes : {}
238
- const reset = merged.reset !== false
239
- const rootSelector = typeof merged.root === "string" ? merged.root : ":root"
240
- const logs = merged.logs !== false
241
- const include = parseList(merged.include)
242
- const exclude = parseList(merged.exclude)
243
-
244
- const { css, utilMap } = buildCSS(prefix, themes, reset, rootSelector, include, exclude)
245
- root.append(postcss.parse(css).nodes)
246
- _lateCss = buildLateCSS(prefix, include, exclude, utilMap)
247
- if (logs) {
248
- const d = "\x1b[2m", r = "\x1b[0m"
249
- const badge = "\x1b[46m\x1b[30m frutjam \x1b[0m"
250
- console.log("\n" + badge + " " + d + "v" + VERSION + r + "\n")
251
- }
252
- },
253
- OnceExit(root) {
254
- if (!_lateCss) return
255
- // Inject into @layer utilities if it exists (correct cascade layer),
256
- // otherwise append to root (unlayered CSS beats all @layer rules).
257
- let utilLayer = null
258
- root.each(node => {
259
- if (node.type === "atrule" && node.name === "layer" && node.params === "utilities" && node.nodes) {
260
- utilLayer = node
261
- }
262
- })
263
- const nodes = postcss.parse(_lateCss).nodes
264
- if (utilLayer) {
265
- utilLayer.append(nodes)
266
- } else {
267
- root.append(nodes)
268
- }
269
- }
270
- }
271
- }
272
- frutjam.postcss = true
1
+ import { readFileSync, readdirSync, statSync, existsSync } from "fs"
2
+ import { join, dirname, basename, extname } from "path"
3
+ import { fileURLToPath } from "url"
4
+ import postcss from "postcss"
5
+
6
+ const __filename = fileURLToPath(import.meta.url)
7
+ const __dir = dirname(__filename)
8
+ const srcDir = join(__dir, "src")
9
+ const VERSION = JSON.parse(readFileSync(join(__dir, "package.json"), "utf8")).version
10
+
11
+ // ── Keys that must be injected after Tailwind processes layers ───────────────
12
+ // These files conflict with @layer utilities from Tailwind plugins (e.g. typography),
13
+ // so they are injected via OnceExit into @layer utilities (or root if not found).
14
+ const LATE_INJECT = new Set(['typography'])
15
+
16
+ // ── Registry (built once at startup, not per build) ──────────────────────────
17
+
18
+ function buildRegistry() {
19
+ const registry = {}
20
+
21
+ // Components: src/components/*/base.css — keyed by folder name
22
+ const componentsDir = join(srcDir, "components")
23
+ for (const name of readdirSync(componentsDir)) {
24
+ const entry = join(componentsDir, name, "base.css")
25
+ if (statSync(join(componentsDir, name)).isDirectory() && existsSync(entry)) {
26
+ registry[name] = entry
27
+ }
28
+ }
29
+
30
+ // Utilities: src/utilities/*.css — keyed by filename without extension
31
+ const utilitiesDir = join(srcDir, "utilities")
32
+ for (const file of readdirSync(utilitiesDir)) {
33
+ if (extname(file) === ".css") {
34
+ registry[basename(file, ".css")] = join(utilitiesDir, file)
35
+ }
36
+ }
37
+
38
+ return registry
39
+ }
40
+
41
+ const REGISTRY = buildRegistry()
42
+
43
+ // ── Helpers ─────────────────────────────────────────────────────────────────
44
+
45
+ function readFile(p) { return readFileSync(p, "utf8") }
46
+
47
+ function parseList(val) {
48
+ if (!val) return []
49
+ if (Array.isArray(val)) return val.map(s => s.trim()).filter(Boolean)
50
+ return String(val).split(",").map(s => s.trim()).filter(Boolean)
51
+ }
52
+
53
+ function applyPrefix(css, prefix) {
54
+ if (!prefix) return css
55
+
56
+ css = css.replace(
57
+ /@utility\s+([\w-]+\*?)\s*\{/g,
58
+ (_, name) => `@utility ${prefix}-${name} {`
59
+ )
60
+ css = css.replace(
61
+ /@apply\s+([^;{}\n]+)/g,
62
+ (_, classes) => `@apply ${classes.trim().split(/\s+/).map(c => `${prefix}-${c}`).join(' ')}`
63
+ )
64
+ css = css.replace(
65
+ /\/\*[\s\S]*?\*\/|"[^"]*"|'[^']*'|\.(([a-z][a-z0-9]*-?)+)/g,
66
+ (match, name) => name ? `.${prefix}-${name}` : match
67
+ )
68
+ return css
69
+ }
70
+
71
+ function buildUtilityMap(css) {
72
+ const map = new Map()
73
+ const pattern = /@utility\s+([\w-]+(?:\s*\*)?)\s*\{/g
74
+ let match
75
+ while ((match = pattern.exec(css)) !== null) {
76
+ const name = match[1].trim()
77
+ if (map.has(name)) continue
78
+ let depth = 0, bodyStart = -1
79
+ for (let i = match.index + match[0].length - 1; i < css.length; i++) {
80
+ if (css[i] === '{') {
81
+ if (depth === 0) bodyStart = i + 1
82
+ depth++
83
+ } else if (css[i] === '}') {
84
+ if (--depth === 0) { map.set(name, css.slice(bodyStart, i)); break }
85
+ }
86
+ }
87
+ }
88
+ return map
89
+ }
90
+
91
+ function resolveCopy(css, source = css) {
92
+ const map = source instanceof Map ? source : buildUtilityMap(source)
93
+ return css.replace(/@copy\s+([\w-]+)\s*;/g, (original, name) => map.get(name) ?? original)
94
+ }
95
+
96
+ function resolveImports(css, baseDir) {
97
+ return css.replace(
98
+ /@import\s+["']([^"']+)["'];?\s*/g,
99
+ (_, importPath) => {
100
+ const fullPath = join(baseDir, importPath)
101
+ const importedDir = dirname(fullPath)
102
+ const importedCSS = readFile(fullPath)
103
+ return resolveImports(importedCSS, importedDir)
104
+ }
105
+ )
106
+ }
107
+
108
+ function buildCustomThemes(themes) {
109
+ if (!themes || Object.keys(themes).length === 0) return ""
110
+
111
+ return Object.entries(themes)
112
+ .map(([name, vars]) => {
113
+ const varString = Object.entries(vars)
114
+ .map(([k, v]) => ` ${k}: ${v};`)
115
+ .join("\n")
116
+ return `[data-theme="${name}"] {\n${varString}\n}`
117
+ })
118
+ .join("\n\n")
119
+ }
120
+
121
+ // ── Base layers (inlined — no entry CSS files needed) ───────────────────────
122
+
123
+ const BASE_FULL = [
124
+ "base/reboot.css",
125
+ "theme/base.css",
126
+ ]
127
+
128
+ const BASE_CORE = [
129
+ "base/variants.css",
130
+ "base/tokens.css",
131
+ "theme/base.css",
132
+ ]
133
+
134
+ // ── Build ────────────────────────────────────────────────────────────────────
135
+
136
+ // Scope one selector part (already split from a comma list) to rootSelector.
137
+ // Returns an array of replacement selectors.
138
+ // [data-theme] selectors become compound with rootSelector so theme variables
139
+ // only apply when data-theme is set directly on the root element — no ancestor
140
+ // inheritance, which would break the isolation guarantee of the root option.
141
+ function scopeThemeSelector(sel, rootSelector) {
142
+ const t = sel.trimStart()
143
+ const lead = sel.slice(0, sel.length - t.length)
144
+
145
+ // Already remapped from :root (e.g. rootSelector:not([data-theme])) — keep as-is
146
+ if (t.startsWith(rootSelector)) return [sel]
147
+
148
+ // :is([data-theme="x"], [data-theme="y"]) — flatten into individual compound selectors
149
+ if (t.startsWith(":is([data-theme")) {
150
+ const values = []
151
+ const re = /\[data-theme=["']([^"']+)["']\]/g
152
+ let m
153
+ while ((m = re.exec(t)) !== null) values.push(m[1])
154
+ return values.map(v => `${lead}${rootSelector}[data-theme="${v}"]`)
155
+ }
156
+
157
+ // [data-theme="x"] — compound with rootSelector
158
+ if (/^\[data-theme=/.test(t)) {
159
+ const m = t.match(/^\[data-theme=["']([^"']+)["']\]/)
160
+ if (m) return [`${lead}${rootSelector}[data-theme="${m[1]}"]`]
161
+ }
162
+
163
+ // [data-theme] without value (base tokens) — compound with rootSelector
164
+ if (t.startsWith("[data-theme]")) {
165
+ return [`${lead}${rootSelector}[data-theme]`]
166
+ }
167
+
168
+ return [sel]
169
+ }
170
+
171
+ function buildCSS(prefix, themes, reset, rootSelector, include, exclude) {
172
+ // Resolve which components+utilities to include
173
+ let keys = Object.keys(REGISTRY)
174
+ if (include.length > 0) {
175
+ keys = include.filter(k => REGISTRY[k])
176
+ } else if (exclude.length > 0) {
177
+ const excludeSet = new Set(exclude)
178
+ keys = keys.filter(k => !excludeSet.has(k))
179
+ }
180
+ // LATE_INJECT keys are handled separately via OnceExit — skip them here
181
+ keys = keys.filter(k => !LATE_INJECT.has(k))
182
+
183
+ // Base layer (full with reboot, or core without)
184
+ const baseFiles = reset ? BASE_FULL : BASE_CORE
185
+ const baseCss = baseFiles
186
+ .map(f => resolveImports(readFile(join(srcDir, f)), join(srcDir, dirname(f))))
187
+ .join("\n")
188
+
189
+ // Components + utilities
190
+ const modulesCss = keys
191
+ .map(k => resolveImports(readFile(REGISTRY[k]), dirname(REGISTRY[k])))
192
+ .join("\n")
193
+
194
+ const combined = baseCss + "\n" + modulesCss
195
+ const utilMap = buildUtilityMap(combined)
196
+ let css = resolveCopy(combined, utilMap)
197
+
198
+ if (rootSelector !== ":root") {
199
+ css = css.replace(/:root\b/g, rootSelector)
200
+ }
201
+
202
+ css = applyPrefix(css, prefix)
203
+
204
+ const customThemes = buildCustomThemes(themes)
205
+ if (customThemes) css += "\n\n" + customThemes
206
+
207
+ if (rootSelector !== ":root") {
208
+ const ast = postcss.parse(css)
209
+ ast.walkRules(rule => {
210
+ rule.selectors = rule.selectors.flatMap(sel => scopeThemeSelector(sel, rootSelector))
211
+ })
212
+ css = ast.toString()
213
+ }
214
+
215
+ return { css, utilMap }
216
+ }
217
+
218
+ function buildLateCSS(prefix, include, exclude, utilMap = new Map()) {
219
+ let keys = [...LATE_INJECT].filter(k => REGISTRY[k])
220
+ if (include.length > 0) {
221
+ keys = keys.filter(k => include.includes(k))
222
+ } else if (exclude.length > 0) {
223
+ const excludeSet = new Set(exclude)
224
+ keys = keys.filter(k => !excludeSet.has(k))
225
+ }
226
+ if (keys.length === 0) return ""
227
+ const raw = keys
228
+ .map(k => resolveImports(readFile(REGISTRY[k]), dirname(REGISTRY[k])))
229
+ .join("\n")
230
+ const css = resolveCopy(raw, utilMap)
231
+ return applyPrefix(css, prefix)
232
+ }
233
+
234
+ // ── Plugin ───────────────────────────────────────────────────────────────────
235
+
236
+ function parseAtPluginOptions(atRule) {
237
+ const opts = {}
238
+ atRule.each(node => {
239
+ if (node.type !== "decl") return
240
+ let val = node.value.trim()
241
+ if (val === "true") val = true
242
+ else if (val === "false") val = false
243
+ else if (
244
+ (val.startsWith('"') && val.endsWith('"')) ||
245
+ (val.startsWith("'") && val.endsWith("'"))
246
+ ) val = val.slice(1, -1)
247
+ opts[node.prop.trim()] = val
248
+ })
249
+ return opts
250
+ }
251
+
252
+ export { buildUtilityMap, resolveCopy }
253
+
254
+ export default function frutjam(options = {}) {
255
+ let _lateCss = ""
256
+
257
+ return {
258
+ postcssPlugin: "frutjam",
259
+ Once(root) {
260
+ // Collect @plugin "frutjam" { ... } blocks from the CSS
261
+ const atPluginNodes = []
262
+ root.each(node => {
263
+ if (
264
+ node.type === "atrule" &&
265
+ node.name === "plugin" &&
266
+ /^["']frutjam["']/.test(node.params.trim()) &&
267
+ node.nodes
268
+ ) atPluginNodes.push(node)
269
+ })
270
+
271
+ // CSS-level options override factory options
272
+ let cssOptions = {}
273
+ for (const node of atPluginNodes) {
274
+ Object.assign(cssOptions, parseAtPluginOptions(node))
275
+ node.remove()
276
+ }
277
+ const merged = { ...options, ...cssOptions }
278
+
279
+ const prefix = typeof merged.prefix === "string" ? merged.prefix.trim() : ""
280
+ const themes = typeof merged.themes === "object" ? merged.themes : {}
281
+ const reset = merged.reset !== false
282
+ const rootSelector = typeof merged.root === "string" ? merged.root : ":root"
283
+ const logs = merged.logs !== false
284
+ const include = parseList(merged.include)
285
+ const exclude = parseList(merged.exclude)
286
+
287
+ const { css, utilMap } = buildCSS(prefix, themes, reset, rootSelector, include, exclude)
288
+ root.append(postcss.parse(css).nodes)
289
+ _lateCss = buildLateCSS(prefix, include, exclude, utilMap)
290
+ if (logs) {
291
+ const d = "\x1b[2m", r = "\x1b[0m"
292
+ const badge = "\x1b[46m\x1b[30m frutjam \x1b[0m"
293
+ console.log("\n" + badge + " " + d + "v" + VERSION + r + "\n")
294
+ }
295
+ },
296
+ OnceExit(root) {
297
+ if (!_lateCss) return
298
+ // Inject into @layer utilities if it exists (correct cascade layer),
299
+ // otherwise append to root (unlayered CSS beats all @layer rules).
300
+ let utilLayer = null
301
+ root.each(node => {
302
+ if (node.type === "atrule" && node.name === "layer" && node.params === "utilities" && node.nodes) {
303
+ utilLayer = node
304
+ }
305
+ })
306
+ const nodes = postcss.parse(_lateCss).nodes
307
+ if (utilLayer) {
308
+ utilLayer.append(nodes)
309
+ } else {
310
+ root.append(nodes)
311
+ }
312
+ }
313
+ }
314
+ }
315
+ frutjam.postcss = true