-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild-modules.js
More file actions
288 lines (242 loc) · 9.12 KB
/
build-modules.js
File metadata and controls
288 lines (242 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* build-modules.js
* Splits src/lib/drop-in.css into modular CSS exports for @drop-in/graffiti.
*
* Outputs:
* dist/index.css - Full file with @layer declarations (main export)
* dist/drop-in.css - Full file with @layer wrappers stripped (flat version)
* dist/core.css - @layer base content only (variables, reset, typography)
* dist/components.css - Core vars preamble + @layer components block
* dist/layouts.css - Core vars preamble + @layer layouts block
* dist/utilities.css - Core vars preamble + @layer utilities block
* dist/minimal.css - Core + @layer utilities
* dist/standard.css - Core + @layer utilities + @layer layouts
* dist/decks.css - Copy of src/lib/decks.css
*/
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const DIST = join(__dirname, "dist");
const SOURCE = join(__dirname, "src", "lib", "drop-in.css");
const DECKS_SOURCE = join(__dirname, "src", "lib", "decks.css");
// Ensure dist/ exists
if (!existsSync(DIST)) {
mkdirSync(DIST, { recursive: true });
}
const source = readFileSync(SOURCE, "utf-8");
// ── Helpers ──────────────────────────────────────────────────────────
/**
* Extract the content of a named @layer block, including the wrapper.
* Returns { wrapped, inner } where wrapped keeps `@layer name { ... }`
* and inner is just the content between the braces.
*/
function extractLayer(css, layerName) {
const startMarker = `@layer ${layerName} {`;
const endMarker = `} /* END @layer ${layerName} */`;
const startIdx = css.indexOf(startMarker);
if (startIdx === -1) {
console.warn(`Warning: @layer ${layerName} not found in source`);
return { wrapped: "", inner: "" };
}
const endIdx = css.indexOf(endMarker, startIdx);
if (endIdx === -1) {
console.warn(`Warning: END marker for @layer ${layerName} not found`);
return { wrapped: "", inner: "" };
}
const wrapped = css.slice(startIdx, endIdx + endMarker.length);
const inner = css
.slice(startIdx + startMarker.length, endIdx)
.replace(/^\n/, "");
return { wrapped, inner };
}
/**
* Build a minimal set of CSS custom properties for standalone module use.
* Extracted from the base layer's :root block.
*/
function buildCoreVarsPreamble(baseInner) {
// Extract key variable groups from the base layer
const vars = [];
// Colors
vars.push(" /* Colors - Theme aware */");
for (const match of baseInner.matchAll(
/--(fg-light|fg-dark|fg|fg-\d+|fg-0\d):\s*[^;]+;/g,
)) {
vars.push(` ${match[0]}`);
}
vars.push(" ");
for (const match of baseInner.matchAll(
/--(bg-light|bg-dark|bg):\s*[^;]+;/g,
)) {
vars.push(` ${match[0]}`);
}
// Semantic colors
vars.push(" ");
vars.push(" /* Semantic colors */");
for (const match of baseInner.matchAll(
/--(primary|error|warning|success):\s*[^;]+;/g,
)) {
vars.push(` ${match[0]}`);
}
// Spacing
vars.push(" ");
vars.push(" /* Spacing */");
for (const match of baseInner.matchAll(/--vs-[a-z]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Border radius
vars.push(" ");
vars.push(" /* Border radius */");
for (const match of baseInner.matchAll(/--br-[a-z]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Padding
vars.push(" ");
vars.push(" /* Padding */");
for (const match of baseInner.matchAll(/--pad-[a-z]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Line height
vars.push(" ");
vars.push(" /* Line height */");
for (const match of baseInner.matchAll(/--lh:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Borders
vars.push(" ");
vars.push(" /* Borders */");
for (const match of baseInner.matchAll(/--border-[a-z0-9]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Focus ring
vars.push(" ");
vars.push(" /* Focus ring */");
for (const match of baseInner.matchAll(/--focus-ring[a-z-]*:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Shadows
vars.push(" ");
vars.push(" /* Shadows */");
for (const match of baseInner.matchAll(/--shadow-[0-9]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Easing
vars.push(" ");
vars.push(" /* Easing */");
for (const match of baseInner.matchAll(/--ease-[a-z]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Safe areas
vars.push(" ");
vars.push(" /* Safe areas */");
for (const match of baseInner.matchAll(/--safe-[a-z]+:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Transition properties
vars.push(" ");
vars.push(" /* Transition properties */");
for (const match of baseInner.matchAll(
/--transition-properties:\s*[^;]+;/g,
)) {
vars.push(` ${match[0]}`);
}
// Layout gap
vars.push(" ");
vars.push(" /* Layout gap */");
for (const match of baseInner.matchAll(/--layout-gap:\s*[^;]+;/g)) {
vars.push(` ${match[0]}`);
}
// Color scheme
vars.push(" ");
vars.push(" /* Color scheme */");
vars.push(" color-scheme: light dark;");
// Dark mode shadow overrides
const darkShadows = baseInner.match(
/@media\s*\(prefers-color-scheme:\s*dark\)\s*\{[^}]*:root\s*\{[^}]*\}\s*\}/s,
);
let result = `\n/* Core CSS Variables - Minimal set for standalone use */\n:root {\n${vars.join("\n")}\n}\n`;
if (darkShadows) {
result += `\n/* Dark mode shadow overrides */\n${darkShadows[0]}\n`;
}
return result;
}
/**
* Strip @layer wrappers from CSS, keeping inner content.
* Also removes the @layer declaration line.
*/
function stripLayers(css) {
// Remove the @layer declaration line
let result = css.replace(/^@layer\s+[^;]+;\s*\n?/m, "");
// Remove @layer name { and matching } /* END @layer name */
result = result.replace(/@layer\s+\w+\s*\{\n?/g, "");
result = result.replace(/\}\s*\/\*\s*END @layer \w+\s*\*\/\n?/g, "");
// Clean up extra blank lines
result = result.replace(/\n{3,}/g, "\n\n");
return result.trim() + "\n";
}
// ── Extract layers ───────────────────────────────────────────────────
const base = extractLayer(source, "base");
const components = extractLayer(source, "components");
const layouts = extractLayer(source, "layouts");
const utilities = extractLayer(source, "utilities");
const coreVarsPreamble = buildCoreVarsPreamble(base.inner);
// ── Build outputs ────────────────────────────────────────────────────
// index.css - Full source with layers (main export)
writeFileSync(join(DIST, "index.css"), source);
console.log(" dist/index.css");
// drop-in.css - Flat version without @layer wrappers
writeFileSync(join(DIST, "drop-in.css"), stripLayers(source));
console.log(" dist/drop-in.css");
// core.css - Base layer content only (unwrapped)
const coreHeader = `/* @drop-in/graffiti/core */\n/* Core CSS - Variables, reset, typography */\n\n`;
writeFileSync(join(DIST, "core.css"), coreHeader + base.inner);
console.log(" dist/core.css");
// components.css - Core vars + full components layer
const componentsHeader = `/* @drop-in/graffiti/components */\n/* Auto-generated - do not edit directly */\n`;
writeFileSync(
join(DIST, "components.css"),
componentsHeader + coreVarsPreamble + "\n" + components.wrapped + "\n",
);
console.log(" dist/components.css");
// layouts.css - Core vars + full layouts layer
const layoutsHeader = `/* @drop-in/graffiti/layouts */\n/* Auto-generated - do not edit directly */\n`;
writeFileSync(
join(DIST, "layouts.css"),
layoutsHeader + coreVarsPreamble + "\n" + layouts.wrapped + "\n",
);
console.log(" dist/layouts.css");
// utilities.css - Core vars + full utilities layer
const utilitiesHeader = `/* @drop-in/graffiti/utilities */\n/* Auto-generated - do not edit directly */\n`;
writeFileSync(
join(DIST, "utilities.css"),
utilitiesHeader + coreVarsPreamble + "\n" + utilities.wrapped + "\n",
);
console.log(" dist/utilities.css");
// minimal.css - Core + utilities (no components, no layouts)
const minimalHeader = `/* @drop-in/graffiti/minimal */\n/* Minimal bundle - core + utilities */\n\n`;
writeFileSync(
join(DIST, "minimal.css"),
minimalHeader + base.inner + "\n" + utilities.wrapped + "\n",
);
console.log(" dist/minimal.css");
// standard.css - Core + utilities + layouts (no components)
const standardHeader = `/* @drop-in/graffiti/standard */\n/* Standard bundle - core + utilities + layouts */\n\n`;
writeFileSync(
join(DIST, "standard.css"),
standardHeader +
base.inner +
"\n" +
utilities.wrapped +
"\n\n" +
layouts.wrapped +
"\n",
);
console.log(" dist/standard.css");
// decks.css - Copy from source
if (existsSync(DECKS_SOURCE)) {
writeFileSync(join(DIST, "decks.css"), readFileSync(DECKS_SOURCE, "utf-8"));
console.log(" dist/decks.css");
} else {
console.warn(" Warning: src/lib/decks.css not found, skipping decks.css");
}
console.log("\nBuild complete!");