@canvakit/designkit 0.0.0-stage → 0.2.0
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.
- package/LICENSE +105 -0
- package/README.md +97 -2
- package/dist/index.d.ts +466 -0
- package/dist/index.mjs +953 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +61 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,953 @@
|
|
|
1
|
+
import { parseDesign, toCssVars } from '@designkit-sh/core';
|
|
2
|
+
import { getBuiltinPresets, getBuiltinPreset } from '@designkit-sh/core/presets';
|
|
3
|
+
import { readText } from '@canvakit/core';
|
|
4
|
+
import { createRequire } from 'module';
|
|
5
|
+
import { dirname } from 'path';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @canvakit/designkit v0.1.0
|
|
9
|
+
* Optional bridge: load active DESIGN.md, attach $design + CSS vars to canvakit context.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
var DesignSourceRegistry = class {
|
|
13
|
+
#sources = /* @__PURE__ */ new Map();
|
|
14
|
+
#listers = /* @__PURE__ */ new Map();
|
|
15
|
+
#fallback = null;
|
|
16
|
+
register(source) {
|
|
17
|
+
this.#sources.set(source.scheme, source.resolve);
|
|
18
|
+
if (source.list) {
|
|
19
|
+
this.#listers.set(source.scheme, source.list);
|
|
20
|
+
} else {
|
|
21
|
+
this.#listers.delete(source.scheme);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Set the fallback resolver used when a ref has no recognised
|
|
26
|
+
* scheme prefix. Default fallback treats the ref as a workspace
|
|
27
|
+
* file path (back-compat with the original `frontmatter.design`
|
|
28
|
+
* hint shape).
|
|
29
|
+
*/
|
|
30
|
+
setFallback(resolver) {
|
|
31
|
+
this.#fallback = resolver;
|
|
32
|
+
}
|
|
33
|
+
has(scheme) {
|
|
34
|
+
return this.#sources.has(scheme);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolve a ref to a `DesignArtifact`. Splits on the first colon to
|
|
38
|
+
* extract the scheme; dispatches to the registered handler. Falls
|
|
39
|
+
* through to the fallback resolver (workspace path) when no scheme
|
|
40
|
+
* matches — keeps `design: brand.md` shorthand working.
|
|
41
|
+
*/
|
|
42
|
+
async resolve(ref, ctx) {
|
|
43
|
+
if (!ref) return null;
|
|
44
|
+
const colonIdx = ref.indexOf(":");
|
|
45
|
+
if (colonIdx > 0) {
|
|
46
|
+
const scheme = ref.slice(0, colonIdx);
|
|
47
|
+
const id = ref.slice(colonIdx + 1);
|
|
48
|
+
const handler = this.#sources.get(scheme);
|
|
49
|
+
if (handler) {
|
|
50
|
+
return handler(id, ctx);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (this.#fallback) {
|
|
54
|
+
return this.#fallback(ref, ctx);
|
|
55
|
+
}
|
|
56
|
+
console.warn(
|
|
57
|
+
`[canvakit-designkit] No resolver matched design ref "${ref}". Register a scheme via DesignSourceRegistry.register() or use a built-in (dk: / ws: / kit:).`
|
|
58
|
+
);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Enumerate every design source discoverable across all schemes that
|
|
63
|
+
* expose a `list`. Schemes without one (no `list` registered) are simply
|
|
64
|
+
* absent from the result — not an error. Aggregated result is sorted by
|
|
65
|
+
* `(scheme, ref)` and de-duplicated by `ref` (two schemes may legitimately
|
|
66
|
+
* point at the same underlying source — e.g. `ws:`/`kit:` both listing
|
|
67
|
+
* the workspace's `_design/` folder).
|
|
68
|
+
*/
|
|
69
|
+
async list(ctx) {
|
|
70
|
+
const seen = /* @__PURE__ */ new Set();
|
|
71
|
+
const out = [];
|
|
72
|
+
for (const [scheme, lister] of this.#listers.entries()) {
|
|
73
|
+
let entries;
|
|
74
|
+
try {
|
|
75
|
+
entries = await lister(ctx);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.warn(
|
|
78
|
+
`[canvakit-designkit] list() lister for scheme "${scheme}:" threw \u2014 skipping: ${err instanceof Error ? err.message : String(err)}`
|
|
79
|
+
);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
for (const entry of entries) {
|
|
83
|
+
if (seen.has(entry.ref)) continue;
|
|
84
|
+
seen.add(entry.ref);
|
|
85
|
+
out.push(entry);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return out.sort((a, b) => {
|
|
89
|
+
if (a.scheme !== b.scheme) return a.scheme.localeCompare(b.scheme);
|
|
90
|
+
return a.ref.localeCompare(b.ref);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var dkResolver = async (id) => {
|
|
95
|
+
const preset = getBuiltinPreset(id);
|
|
96
|
+
if (!preset) {
|
|
97
|
+
console.warn(
|
|
98
|
+
`[canvakit-designkit] Unknown designkit preset "${id}" \u2014 dk: ref ignored. Available: heritage, modern-minimal, bold-tech, atelier, neon-grid, classic.`
|
|
99
|
+
);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
return { tokens: parseDesign(preset.source).tokens };
|
|
104
|
+
} catch (err) {
|
|
105
|
+
console.warn(
|
|
106
|
+
`[canvakit-designkit] Failed to parse dk:${id}: ${err instanceof Error ? err.message : String(err)}`
|
|
107
|
+
);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
var wsResolver = async (id, { fs }) => {
|
|
112
|
+
const path = id.replace(/^\/+/, "");
|
|
113
|
+
if (!path) {
|
|
114
|
+
console.warn(`[canvakit-designkit] Empty ws: path.`);
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
if (!await fs.exists(path)) {
|
|
119
|
+
console.warn(`[canvakit-designkit] ws:${path} not found in workspace.`);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
const source = await readText(fs, path);
|
|
123
|
+
return { tokens: parseDesign(source).tokens };
|
|
124
|
+
} catch (err) {
|
|
125
|
+
console.warn(
|
|
126
|
+
`[canvakit-designkit] Failed to load ws:${path}: ${err instanceof Error ? err.message : String(err)}`
|
|
127
|
+
);
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var kitResolver = async (slug, ctx) => {
|
|
132
|
+
if (!slug) {
|
|
133
|
+
console.warn(`[canvakit-designkit] Empty kit: slug.`);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
const cleanSlug = slug.replace(/^\/+|\.md$/g, "");
|
|
137
|
+
return wsResolver(`_design/${cleanSlug}.md`, ctx);
|
|
138
|
+
};
|
|
139
|
+
var dkLister = async () => {
|
|
140
|
+
return getBuiltinPresets().map((preset) => ({
|
|
141
|
+
ref: `dk:${preset.id}`,
|
|
142
|
+
name: preset.name,
|
|
143
|
+
scheme: "dk"
|
|
144
|
+
}));
|
|
145
|
+
};
|
|
146
|
+
var kitLister = async ({ fs }) => {
|
|
147
|
+
let entries;
|
|
148
|
+
try {
|
|
149
|
+
entries = await fs.readdir("_design");
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.warn(
|
|
152
|
+
`[canvakit-designkit] Failed to list _design/: ${err instanceof Error ? err.message : String(err)}`
|
|
153
|
+
);
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
const out = [];
|
|
157
|
+
for (const entry of entries) {
|
|
158
|
+
if (entry.type === "directory" || !entry.name.endsWith(".md")) continue;
|
|
159
|
+
const slug = entry.name.slice(0, -".md".length);
|
|
160
|
+
let name;
|
|
161
|
+
try {
|
|
162
|
+
const source = await readText(fs, `_design/${entry.name}`);
|
|
163
|
+
name = parseDesign(source).tokens.name;
|
|
164
|
+
} catch (err) {
|
|
165
|
+
console.warn(
|
|
166
|
+
`[canvakit-designkit] Failed to read name from _design/${entry.name}: ${err instanceof Error ? err.message : String(err)}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
out.push({ ref: `kit:${slug}`, name: name ?? slug, scheme: "kit" });
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
};
|
|
173
|
+
var fallbackResolver = async (ref, ctx) => {
|
|
174
|
+
const looksLikePath = /\.md$/i.test(ref) || ref.includes("/") || ref.startsWith(".");
|
|
175
|
+
if (!looksLikePath) {
|
|
176
|
+
console.warn(
|
|
177
|
+
`[canvakit-designkit] Unrecognised design ref "${ref}". Use a scheme prefix (dk: / ws: / kit:) or a path ending in .md.`
|
|
178
|
+
);
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
return wsResolver(ref, ctx);
|
|
182
|
+
};
|
|
183
|
+
function createDefaultRegistry() {
|
|
184
|
+
const registry = new DesignSourceRegistry();
|
|
185
|
+
registry.register({ scheme: "dk", resolve: dkResolver, list: dkLister });
|
|
186
|
+
registry.register({ scheme: "ws", resolve: wsResolver, list: kitLister });
|
|
187
|
+
registry.register({ scheme: "kit", resolve: kitResolver, list: kitLister });
|
|
188
|
+
registry.setFallback(fallbackResolver);
|
|
189
|
+
return registry;
|
|
190
|
+
}
|
|
191
|
+
var defaultRegistry = createDefaultRegistry();
|
|
192
|
+
|
|
193
|
+
// src/active-design.ts
|
|
194
|
+
var DESIGN_DIR = "_design";
|
|
195
|
+
var DESIGN_ACTIVE_FILE = `${DESIGN_DIR}/active.txt`;
|
|
196
|
+
var DESIGN_ROOT_FILE = "DESIGN.md";
|
|
197
|
+
async function loadActiveDesign(fs, options = {}) {
|
|
198
|
+
const registry = options.registry ?? defaultRegistry;
|
|
199
|
+
const ctx = { fs };
|
|
200
|
+
if (options.overrideDesign) {
|
|
201
|
+
const artifact = await registry.resolve(options.overrideDesign, ctx);
|
|
202
|
+
if (artifact) return artifact;
|
|
203
|
+
console.warn(
|
|
204
|
+
`\u26A0\uFE0F [canvakit-designkit] design "${options.overrideDesign}" did NOT resolve \u2014 render will be UNTHEMED unless a lower-priority design source resolves instead (check the ref / path). Falling through to lower-priority sources.`
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (options.forceDesign) {
|
|
208
|
+
const artifact = await registry.resolve(options.forceDesign, ctx);
|
|
209
|
+
if (artifact) return artifact;
|
|
210
|
+
console.warn(
|
|
211
|
+
`\u26A0\uFE0F [canvakit-designkit] design "${options.forceDesign}" did NOT resolve \u2014 render will be UNTHEMED unless a lower-priority design source resolves instead (check the ref / path). Falling through to lower-priority sources.`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const fromWorkspace = await loadFromWorkspace(fs, registry);
|
|
215
|
+
if (fromWorkspace) return fromWorkspace;
|
|
216
|
+
if (options.hint) {
|
|
217
|
+
const artifact = await registry.resolve(options.hint, ctx);
|
|
218
|
+
if (artifact) return artifact;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
async function loadFromWorkspace(fs, registry) {
|
|
223
|
+
const ctx = { fs };
|
|
224
|
+
try {
|
|
225
|
+
if (await fs.exists(DESIGN_ACTIVE_FILE)) {
|
|
226
|
+
const text = await readTextSafe(fs, DESIGN_ACTIVE_FILE) ?? "";
|
|
227
|
+
const slug = text.trim();
|
|
228
|
+
if (slug) {
|
|
229
|
+
const artifact = await registry.resolve(`kit:${slug}`, ctx);
|
|
230
|
+
if (artifact) return artifact;
|
|
231
|
+
console.warn(
|
|
232
|
+
`[canvakit-designkit] Active design "${slug}" failed to resolve. Falling back to root DESIGN.md.`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (await fs.exists(DESIGN_ROOT_FILE)) {
|
|
237
|
+
return registry.resolve(`ws:${DESIGN_ROOT_FILE}`, ctx);
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.warn(
|
|
242
|
+
`[canvakit-designkit] Workspace design load failed: ${err instanceof Error ? err.message : String(err)}. Continuing without design tokens.`
|
|
243
|
+
);
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async function readTextSafe(fs, path) {
|
|
248
|
+
try {
|
|
249
|
+
const result = await fs.readFile(path);
|
|
250
|
+
if (typeof result === "string") return result;
|
|
251
|
+
if (result && typeof result.toString === "function") {
|
|
252
|
+
return result.toString();
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
} catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function resolveLocalDesignHint(fs, templatePath, ref) {
|
|
260
|
+
if (!ref || !templatePath) return ref;
|
|
261
|
+
const isScheme = /^[a-z][a-z0-9+.-]*:/i.test(ref);
|
|
262
|
+
const looksLikePath = /\.md$/i.test(ref) || ref.includes("/") || ref.startsWith(".");
|
|
263
|
+
if (isScheme || !looksLikePath) return ref;
|
|
264
|
+
const slash = templatePath.lastIndexOf("/");
|
|
265
|
+
if (slash < 0) return ref;
|
|
266
|
+
const candidate = `${templatePath.slice(0, slash + 1)}${ref}`;
|
|
267
|
+
try {
|
|
268
|
+
if (await fs.exists(candidate)) return candidate;
|
|
269
|
+
} catch {
|
|
270
|
+
}
|
|
271
|
+
return ref;
|
|
272
|
+
}
|
|
273
|
+
var SYSTEM_FONT_KEYWORDS = new Set(
|
|
274
|
+
[
|
|
275
|
+
"system-ui",
|
|
276
|
+
"ui-sans-serif",
|
|
277
|
+
"ui-serif",
|
|
278
|
+
"ui-monospace",
|
|
279
|
+
"ui-rounded",
|
|
280
|
+
"sans-serif",
|
|
281
|
+
"serif",
|
|
282
|
+
"monospace",
|
|
283
|
+
"cursive",
|
|
284
|
+
"fantasy",
|
|
285
|
+
"math",
|
|
286
|
+
"emoji",
|
|
287
|
+
"fangsong",
|
|
288
|
+
"-apple-system",
|
|
289
|
+
"blinkmacsystemfont",
|
|
290
|
+
"segoe ui",
|
|
291
|
+
"helvetica",
|
|
292
|
+
"helvetica neue",
|
|
293
|
+
"arial",
|
|
294
|
+
"tahoma",
|
|
295
|
+
"verdana",
|
|
296
|
+
"times",
|
|
297
|
+
"times new roman",
|
|
298
|
+
"georgia",
|
|
299
|
+
"courier",
|
|
300
|
+
"courier new",
|
|
301
|
+
"menlo",
|
|
302
|
+
"monaco",
|
|
303
|
+
"consolas",
|
|
304
|
+
"sfmono-regular"
|
|
305
|
+
].map((s) => s.toLowerCase())
|
|
306
|
+
);
|
|
307
|
+
function collectFontFamilies(tokens) {
|
|
308
|
+
const out = [];
|
|
309
|
+
if (!tokens || typeof tokens !== "object") return out;
|
|
310
|
+
const typo = tokens.typography;
|
|
311
|
+
if (!typo || typeof typo !== "object") return out;
|
|
312
|
+
for (const value of Object.values(typo)) {
|
|
313
|
+
if (!value || typeof value !== "object") continue;
|
|
314
|
+
const family = value.family;
|
|
315
|
+
if (typeof family === "string") out.push(family);
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
function extractImportableFonts(tokens) {
|
|
320
|
+
const families = collectFontFamilies(tokens);
|
|
321
|
+
const seen = /* @__PURE__ */ new Set();
|
|
322
|
+
const out = [];
|
|
323
|
+
for (const raw of families) {
|
|
324
|
+
const first = raw.split(",")[0]?.trim();
|
|
325
|
+
if (!first) continue;
|
|
326
|
+
const unquoted = first.replace(/^["']+|["']+$/g, "").trim();
|
|
327
|
+
if (!unquoted) continue;
|
|
328
|
+
if (SYSTEM_FONT_KEYWORDS.has(unquoted.toLowerCase())) continue;
|
|
329
|
+
if (seen.has(unquoted)) continue;
|
|
330
|
+
seen.add(unquoted);
|
|
331
|
+
out.push(unquoted);
|
|
332
|
+
}
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
var DEFAULT_FONT_WEIGHTS = [400, 500, 600, 700];
|
|
336
|
+
function buildGoogleFontsUrl(requests) {
|
|
337
|
+
if (requests.length === 0) return null;
|
|
338
|
+
const segments = requests.map((r) => {
|
|
339
|
+
const axis = (r.weights.length > 0 ? r.weights : [...DEFAULT_FONT_WEIGHTS]).slice().sort((a, b) => a - b).join(";");
|
|
340
|
+
return `family=${r.family.replace(/\s+/g, "+")}:wght@${axis}`;
|
|
341
|
+
});
|
|
342
|
+
return `https://fonts.googleapis.com/css2?${segments.join("&")}&display=swap`;
|
|
343
|
+
}
|
|
344
|
+
function resolveFamilyRequests(artifact, tokens) {
|
|
345
|
+
const byFamily = /* @__PURE__ */ new Map();
|
|
346
|
+
for (const face of artifact.fonts ?? []) {
|
|
347
|
+
const weights = byFamily.get(face.family) ?? /* @__PURE__ */ new Set();
|
|
348
|
+
for (const w of face.weights ?? []) weights.add(w);
|
|
349
|
+
byFamily.set(face.family, weights);
|
|
350
|
+
}
|
|
351
|
+
for (const family of extractImportableFonts(tokens)) {
|
|
352
|
+
if (!byFamily.has(family)) byFamily.set(family, /* @__PURE__ */ new Set());
|
|
353
|
+
}
|
|
354
|
+
return [...byFamily.entries()].map(([family, weights]) => ({
|
|
355
|
+
family,
|
|
356
|
+
weights: [...weights]
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
function fileBackedFamilies(artifact) {
|
|
360
|
+
return new Set((artifact.fonts ?? []).map((face) => face.family));
|
|
361
|
+
}
|
|
362
|
+
function bytesToBase64(bytes) {
|
|
363
|
+
const maybeBuffer = globalThis.Buffer;
|
|
364
|
+
if (maybeBuffer) {
|
|
365
|
+
return maybeBuffer.from(bytes).toString("base64");
|
|
366
|
+
}
|
|
367
|
+
let binary = "";
|
|
368
|
+
const chunk = 32768;
|
|
369
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
370
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
371
|
+
}
|
|
372
|
+
const maybeBtoa = globalThis.btoa;
|
|
373
|
+
if (maybeBtoa) return maybeBtoa(binary);
|
|
374
|
+
return manualBase64(binary);
|
|
375
|
+
}
|
|
376
|
+
var BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
377
|
+
function manualBase64(binary) {
|
|
378
|
+
let out = "";
|
|
379
|
+
for (let i = 0; i < binary.length; i += 3) {
|
|
380
|
+
const b0 = binary.charCodeAt(i);
|
|
381
|
+
const b1 = i + 1 < binary.length ? binary.charCodeAt(i + 1) : 0;
|
|
382
|
+
const b2 = i + 2 < binary.length ? binary.charCodeAt(i + 2) : 0;
|
|
383
|
+
out += BASE64_ALPHABET[b0 >> 2];
|
|
384
|
+
out += BASE64_ALPHABET[(b0 & 3) << 4 | b1 >> 4];
|
|
385
|
+
out += i + 1 < binary.length ? BASE64_ALPHABET[(b1 & 15) << 2 | b2 >> 6] : "=";
|
|
386
|
+
out += i + 2 < binary.length ? BASE64_ALPHABET[b2 & 63] : "=";
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
function fontFormatFromHref(href) {
|
|
391
|
+
const clean = href.split(/[?#]/)[0] ?? href;
|
|
392
|
+
const ext = clean.slice(clean.lastIndexOf(".") + 1).toLowerCase();
|
|
393
|
+
switch (ext) {
|
|
394
|
+
case "woff2":
|
|
395
|
+
return { format: "woff2", mime: "font/woff2" };
|
|
396
|
+
case "woff":
|
|
397
|
+
return { format: "woff", mime: "font/woff" };
|
|
398
|
+
case "ttf":
|
|
399
|
+
return { format: "truetype", mime: "font/ttf" };
|
|
400
|
+
case "otf":
|
|
401
|
+
return { format: "opentype", mime: "font/otf" };
|
|
402
|
+
default:
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
function fontFaceBlock(family, weight, src) {
|
|
407
|
+
return [
|
|
408
|
+
"@font-face {",
|
|
409
|
+
` font-family: '${family}';`,
|
|
410
|
+
` font-weight: ${weight};`,
|
|
411
|
+
" font-style: normal;",
|
|
412
|
+
" font-display: swap;",
|
|
413
|
+
` src: ${src};`,
|
|
414
|
+
"}"
|
|
415
|
+
].join("\n");
|
|
416
|
+
}
|
|
417
|
+
async function buildEmbeddedFontFaces(artifact, fs) {
|
|
418
|
+
if (!artifact.fonts || artifact.fonts.length === 0) return "";
|
|
419
|
+
const blocks = [];
|
|
420
|
+
for (const face of artifact.fonts) {
|
|
421
|
+
if (!face.href) continue;
|
|
422
|
+
const weights = face.weights && face.weights.length > 0 ? face.weights : [400];
|
|
423
|
+
let src;
|
|
424
|
+
if (face.href.startsWith("data:")) {
|
|
425
|
+
src = `url(${face.href})`;
|
|
426
|
+
} else {
|
|
427
|
+
const fmt = fontFormatFromHref(face.href);
|
|
428
|
+
if (!fmt) {
|
|
429
|
+
console.warn(
|
|
430
|
+
`[canvakit-designkit] Cannot embed font "${face.family}" \u2014 unsupported extension in href "${face.href}". Skipping.`
|
|
431
|
+
);
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
let bytes;
|
|
435
|
+
try {
|
|
436
|
+
const path = face.href.replace(/^\/+/, "");
|
|
437
|
+
const raw = await fs.readFile(path);
|
|
438
|
+
bytes = typeof raw === "string" ? new TextEncoder().encode(raw) : raw;
|
|
439
|
+
} catch (err) {
|
|
440
|
+
console.warn(
|
|
441
|
+
`[canvakit-designkit] Failed to embed font "${face.family}" from "${face.href}": ${err instanceof Error ? err.message : String(err)}. Skipping.`
|
|
442
|
+
);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
src = `url(data:${fmt.mime};base64,${bytesToBase64(bytes)}) format('${fmt.format}')`;
|
|
446
|
+
}
|
|
447
|
+
for (const weight of weights) {
|
|
448
|
+
blocks.push(fontFaceBlock(face.family, weight, src));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return blocks.join("\n");
|
|
452
|
+
}
|
|
453
|
+
function mimeForFontFormat(format) {
|
|
454
|
+
switch (format) {
|
|
455
|
+
case "woff2":
|
|
456
|
+
return "font/woff2";
|
|
457
|
+
case "woff":
|
|
458
|
+
return "font/woff";
|
|
459
|
+
case "truetype":
|
|
460
|
+
return "font/ttf";
|
|
461
|
+
case "opentype":
|
|
462
|
+
return "font/otf";
|
|
463
|
+
default:
|
|
464
|
+
return "font/woff2";
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function renderFetchedFontFaces(faces) {
|
|
468
|
+
return faces.map((face) => {
|
|
469
|
+
const mime = mimeForFontFormat(face.format);
|
|
470
|
+
const src = `url(data:${mime};base64,${face.base64}) format('${face.format}')`;
|
|
471
|
+
return fontFaceBlock(face.family, face.weight, src);
|
|
472
|
+
}).join("\n");
|
|
473
|
+
}
|
|
474
|
+
function googleFontsLinkTags(url) {
|
|
475
|
+
return [
|
|
476
|
+
`<link rel="preconnect" href="https://fonts.googleapis.com">`,
|
|
477
|
+
`<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>`,
|
|
478
|
+
`<link rel="stylesheet" href="${url}" data-design-kit="fonts">`
|
|
479
|
+
].join("\n");
|
|
480
|
+
}
|
|
481
|
+
async function resolveDeclaredOnlyEmbedFonts(artifact, tokens, fetchFontFaces) {
|
|
482
|
+
const backed = fileBackedFamilies(artifact);
|
|
483
|
+
const declaredOnly = resolveFamilyRequests(artifact, tokens).filter((request) => !backed.has(request.family)).map((request) => ({
|
|
484
|
+
family: request.family,
|
|
485
|
+
weights: request.weights.length > 0 ? request.weights : [...DEFAULT_FONT_WEIGHTS]
|
|
486
|
+
}));
|
|
487
|
+
if (declaredOnly.length === 0) return { css: "", fallbackLink: "" };
|
|
488
|
+
if (!fetchFontFaces) {
|
|
489
|
+
console.warn(
|
|
490
|
+
`[canvakit-designkit] embed mode: no fetchFontFaces capability injected \u2014 falling back to a Google-Fonts <link> for declared-only family(ies): ${declaredOnly.map((request) => request.family).join(", ")}.`
|
|
491
|
+
);
|
|
492
|
+
const url2 = buildGoogleFontsUrl(declaredOnly);
|
|
493
|
+
return { css: "", fallbackLink: url2 ? googleFontsLinkTags(url2) : "" };
|
|
494
|
+
}
|
|
495
|
+
let fetched;
|
|
496
|
+
try {
|
|
497
|
+
fetched = await fetchFontFaces(declaredOnly);
|
|
498
|
+
} catch (err) {
|
|
499
|
+
console.warn(
|
|
500
|
+
`[canvakit-designkit] embed mode: fetchFontFaces threw \u2014 falling back to a Google-Fonts <link>: ${err instanceof Error ? err.message : String(err)}`
|
|
501
|
+
);
|
|
502
|
+
fetched = null;
|
|
503
|
+
}
|
|
504
|
+
const resolved = fetched ?? [];
|
|
505
|
+
const resolvedKey = (family, weight) => `${family}::${weight}`;
|
|
506
|
+
const resolvedSet = new Set(
|
|
507
|
+
resolved.map((face) => resolvedKey(face.family, face.weight))
|
|
508
|
+
);
|
|
509
|
+
const missing = [];
|
|
510
|
+
for (const request of declaredOnly) {
|
|
511
|
+
const missingWeights = request.weights.filter(
|
|
512
|
+
(weight) => !resolvedSet.has(resolvedKey(request.family, weight))
|
|
513
|
+
);
|
|
514
|
+
if (missingWeights.length > 0) {
|
|
515
|
+
missing.push({ family: request.family, weights: missingWeights });
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (missing.length > 0) {
|
|
519
|
+
console.warn(
|
|
520
|
+
`[canvakit-designkit] embed mode: fetchFontFaces did not resolve family(ies) ${missing.map((request) => request.family).join(", ")} \u2014 falling back to a Google-Fonts <link> for just those.`
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
const css = renderFetchedFontFaces(resolved);
|
|
524
|
+
const url = missing.length > 0 ? buildGoogleFontsUrl(missing) : null;
|
|
525
|
+
return { css, fallbackLink: url ? googleFontsLinkTags(url) : "" };
|
|
526
|
+
}
|
|
527
|
+
function injectIntoHead(body, trailing) {
|
|
528
|
+
const closeHead = body.search(/<\/head\s*>/i);
|
|
529
|
+
if (closeHead >= 0) {
|
|
530
|
+
return `${body.slice(0, closeHead)}${trailing}
|
|
531
|
+
${body.slice(closeHead)}`;
|
|
532
|
+
}
|
|
533
|
+
const openHead = body.match(/<head\b[^>]*>/i);
|
|
534
|
+
if (openHead && openHead.index !== void 0) {
|
|
535
|
+
const at = openHead.index + openHead[0].length;
|
|
536
|
+
return `${body.slice(0, at)}
|
|
537
|
+
${trailing}${body.slice(at)}`;
|
|
538
|
+
}
|
|
539
|
+
const openBody = body.match(/<body\b[^>]*>/i);
|
|
540
|
+
if (openBody && openBody.index !== void 0) {
|
|
541
|
+
return `${body.slice(0, openBody.index)}${trailing}
|
|
542
|
+
${body.slice(openBody.index)}`;
|
|
543
|
+
}
|
|
544
|
+
return `${trailing}
|
|
545
|
+
${body}`;
|
|
546
|
+
}
|
|
547
|
+
function resolveFontMode(options) {
|
|
548
|
+
if (options.fonts) return options.fonts;
|
|
549
|
+
return options.loadGoogleFonts ?? true ? "link" : "none";
|
|
550
|
+
}
|
|
551
|
+
function designkitExtension(options = {}) {
|
|
552
|
+
const fontMode = resolveFontMode(options);
|
|
553
|
+
return async ({ filesystem, frontmatter, meta }) => {
|
|
554
|
+
const rawHint = typeof frontmatter.design === "string" ? frontmatter.design : void 0;
|
|
555
|
+
const hint = await resolveLocalDesignHint(
|
|
556
|
+
filesystem,
|
|
557
|
+
meta.templatePath,
|
|
558
|
+
rawHint
|
|
559
|
+
);
|
|
560
|
+
const rawForceDesign = typeof frontmatter.forceDesign === "string" ? frontmatter.forceDesign : void 0;
|
|
561
|
+
const forceDesign = await resolveLocalDesignHint(
|
|
562
|
+
filesystem,
|
|
563
|
+
meta.templatePath,
|
|
564
|
+
rawForceDesign
|
|
565
|
+
);
|
|
566
|
+
const overrideDesign = await resolveLocalDesignHint(
|
|
567
|
+
filesystem,
|
|
568
|
+
meta.templatePath,
|
|
569
|
+
options.overrideDesign
|
|
570
|
+
);
|
|
571
|
+
const artifact = await loadActiveDesign(filesystem, {
|
|
572
|
+
overrideDesign,
|
|
573
|
+
forceDesign,
|
|
574
|
+
hint,
|
|
575
|
+
registry: options.registry
|
|
576
|
+
});
|
|
577
|
+
if (!artifact) {
|
|
578
|
+
return { contextAdditions: { $design: null } };
|
|
579
|
+
}
|
|
580
|
+
const tokens = artifact.tokens;
|
|
581
|
+
if (options.skipStylePrepend) {
|
|
582
|
+
return { contextAdditions: { $design: tokens } };
|
|
583
|
+
}
|
|
584
|
+
let css;
|
|
585
|
+
try {
|
|
586
|
+
css = toCssVars(tokens);
|
|
587
|
+
} catch (err) {
|
|
588
|
+
console.warn(
|
|
589
|
+
`[canvakit-designkit] Failed to emit CSS vars: ${err instanceof Error ? err.message : String(err)}`
|
|
590
|
+
);
|
|
591
|
+
return { contextAdditions: { $design: tokens } };
|
|
592
|
+
}
|
|
593
|
+
let fontFaces = "";
|
|
594
|
+
let fontLinkBlock = "";
|
|
595
|
+
if (fontMode === "embed") {
|
|
596
|
+
const fileBacked = await buildEmbeddedFontFaces(artifact, filesystem);
|
|
597
|
+
const { css: fetchedCss, fallbackLink } = await resolveDeclaredOnlyEmbedFonts(
|
|
598
|
+
artifact,
|
|
599
|
+
tokens,
|
|
600
|
+
options.fetchFontFaces
|
|
601
|
+
);
|
|
602
|
+
fontFaces = [fileBacked, fetchedCss].filter(Boolean).join("\n");
|
|
603
|
+
fontLinkBlock = fallbackLink;
|
|
604
|
+
} else if (fontMode === "link") {
|
|
605
|
+
const url = buildGoogleFontsUrl(resolveFamilyRequests(artifact, tokens));
|
|
606
|
+
if (url) fontLinkBlock = googleFontsLinkTags(url);
|
|
607
|
+
}
|
|
608
|
+
const styleBody = fontFaces ? `${fontFaces}
|
|
609
|
+
${css}` : css;
|
|
610
|
+
const styleBlock = `<style data-design-kit="root">
|
|
611
|
+
${styleBody}
|
|
612
|
+
</style>`;
|
|
613
|
+
const trailing = fontLinkBlock ? `${fontLinkBlock}
|
|
614
|
+
${styleBlock}` : styleBlock;
|
|
615
|
+
return {
|
|
616
|
+
contextAdditions: { $design: tokens },
|
|
617
|
+
// Inject into `<head>` (just before `</head>` when present) so the
|
|
618
|
+
// bridge `:root` / `@font-face` lands after any template `<head>`
|
|
619
|
+
// styles but still inside `<head>` — reliably winning the cascade
|
|
620
|
+
// over template defaults, and never after `</html>` (which forces
|
|
621
|
+
// quirks mode / can be dropped by non-browser renderers). Bare
|
|
622
|
+
// fragments with no `<head>` fall back to a prepend.
|
|
623
|
+
transformBody: (body) => injectIntoHead(body, trailing)
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// src/google-fonts-fetcher.ts
|
|
629
|
+
var MODERN_BROWSER_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
|
|
630
|
+
function coversBasicLatin(unicodeRange) {
|
|
631
|
+
if (!unicodeRange) return true;
|
|
632
|
+
const A = 65;
|
|
633
|
+
for (const token of unicodeRange.split(",")) {
|
|
634
|
+
const m = token.trim().match(/^U\+([0-9A-Fa-f]+)(?:-([0-9A-Fa-f]+))?$/);
|
|
635
|
+
if (!m || m[1] === void 0) continue;
|
|
636
|
+
const lo = parseInt(m[1], 16);
|
|
637
|
+
const hi = m[2] !== void 0 ? parseInt(m[2], 16) : lo;
|
|
638
|
+
if (A >= lo && A <= hi) return true;
|
|
639
|
+
}
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
function parseFontFaceBlocks(css) {
|
|
643
|
+
const out = [];
|
|
644
|
+
const blockRe = /@font-face\s*\{([^}]*)\}/g;
|
|
645
|
+
let match;
|
|
646
|
+
while ((match = blockRe.exec(css)) !== null) {
|
|
647
|
+
const block = match[1] ?? "";
|
|
648
|
+
const familyMatch = block.match(/font-family:\s*'([^']+)'/);
|
|
649
|
+
const weightMatch = block.match(/font-weight:\s*(\d+)/);
|
|
650
|
+
const urlMatch = block.match(/src:\s*url\(([^)]+)\)\s*format\('woff2'\)/);
|
|
651
|
+
const rangeMatch = block.match(/unicode-range:\s*([^;}]+)/);
|
|
652
|
+
if (!familyMatch || !weightMatch || !urlMatch) continue;
|
|
653
|
+
const family = familyMatch[1];
|
|
654
|
+
const url = urlMatch[1];
|
|
655
|
+
const weight = Number(weightMatch[1]);
|
|
656
|
+
if (!family || !url || Number.isNaN(weight)) continue;
|
|
657
|
+
const unicodeRange = rangeMatch && rangeMatch[1] ? rangeMatch[1].trim() : "";
|
|
658
|
+
out.push({ family, weight, url, unicodeRange });
|
|
659
|
+
}
|
|
660
|
+
return out;
|
|
661
|
+
}
|
|
662
|
+
function defaultGoogleFontsFetcher() {
|
|
663
|
+
const cssCache = /* @__PURE__ */ new Map();
|
|
664
|
+
const bytesCache = /* @__PURE__ */ new Map();
|
|
665
|
+
return async function fetchFontFaces(requests) {
|
|
666
|
+
const url = buildGoogleFontsUrl(requests);
|
|
667
|
+
if (!url) return null;
|
|
668
|
+
let css = cssCache.get(url);
|
|
669
|
+
if (css === void 0) {
|
|
670
|
+
try {
|
|
671
|
+
const res = await fetch(url, {
|
|
672
|
+
headers: { "User-Agent": MODERN_BROWSER_USER_AGENT }
|
|
673
|
+
});
|
|
674
|
+
if (!res.ok) {
|
|
675
|
+
console.warn(
|
|
676
|
+
`[canvakit-designkit] defaultGoogleFontsFetcher: css2 request failed (${res.status}) for ${url}`
|
|
677
|
+
);
|
|
678
|
+
return null;
|
|
679
|
+
}
|
|
680
|
+
css = await res.text();
|
|
681
|
+
cssCache.set(url, css);
|
|
682
|
+
} catch (err) {
|
|
683
|
+
console.warn(
|
|
684
|
+
`[canvakit-designkit] defaultGoogleFontsFetcher: css2 fetch threw: ${err instanceof Error ? err.message : String(err)}`
|
|
685
|
+
);
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
const requestedKeys = new Set(
|
|
690
|
+
requests.flatMap((r) => r.weights.map((w) => `${r.family}::${w}`))
|
|
691
|
+
);
|
|
692
|
+
const chosen = /* @__PURE__ */ new Map();
|
|
693
|
+
for (const block of parseFontFaceBlocks(css)) {
|
|
694
|
+
const key = `${block.family}::${block.weight}`;
|
|
695
|
+
if (!requestedKeys.has(key)) continue;
|
|
696
|
+
const existing = chosen.get(key);
|
|
697
|
+
if (!existing || !coversBasicLatin(existing.unicodeRange) && coversBasicLatin(block.unicodeRange)) {
|
|
698
|
+
chosen.set(key, block);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
const faces = [];
|
|
702
|
+
for (const block of chosen.values()) {
|
|
703
|
+
const key = `${block.family}::${block.weight}`;
|
|
704
|
+
let bytes = bytesCache.get(key);
|
|
705
|
+
if (!bytes) {
|
|
706
|
+
try {
|
|
707
|
+
const fontRes = await fetch(block.url);
|
|
708
|
+
if (!fontRes.ok) continue;
|
|
709
|
+
bytes = new Uint8Array(await fontRes.arrayBuffer());
|
|
710
|
+
bytesCache.set(key, bytes);
|
|
711
|
+
} catch {
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
faces.push({
|
|
716
|
+
family: block.family,
|
|
717
|
+
weight: block.weight,
|
|
718
|
+
format: "woff2",
|
|
719
|
+
base64: bytesToBase64(bytes)
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
return faces;
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
var DEFAULT_THEME_EXTEND = {
|
|
726
|
+
colors: {
|
|
727
|
+
// Brand colour — accept either token name. Designs that ship
|
|
728
|
+
// `--color-primary` win first; otherwise fall back to `--color-accent`,
|
|
729
|
+
// then a Tailwind-indigo default.
|
|
730
|
+
primary: "var(--color-primary, var(--color-accent, #6366f1))",
|
|
731
|
+
secondary: "var(--color-secondary, var(--color-accent-alt, #64748b))",
|
|
732
|
+
accent: "var(--color-accent, var(--color-primary, #ec4899))",
|
|
733
|
+
"accent-alt": "var(--color-accent-alt, var(--color-accent, #f97316))",
|
|
734
|
+
background: "var(--color-background, #ffffff)",
|
|
735
|
+
surface: "var(--color-surface, var(--color-background, #ffffff))",
|
|
736
|
+
"surface-raised": "var(--color-surface-raised, var(--color-surface, #ffffff))",
|
|
737
|
+
"surface-muted": "var(--color-surface-muted, var(--color-surface, #f8fafc))",
|
|
738
|
+
ink: "var(--color-ink, #0f172a)",
|
|
739
|
+
"ink-muted": "var(--color-ink-muted, #64748b)",
|
|
740
|
+
border: "var(--color-border, #e2e8f0)",
|
|
741
|
+
link: "var(--color-link, var(--color-accent, #2563eb))",
|
|
742
|
+
success: "var(--color-success, #10b981)",
|
|
743
|
+
warning: "var(--color-warning, #f59e0b)",
|
|
744
|
+
danger: "var(--color-danger, #ef4444)",
|
|
745
|
+
focus: "var(--color-focus, #3898ec)"
|
|
746
|
+
},
|
|
747
|
+
fontFamily: {
|
|
748
|
+
// Heading — try h1 first (most kits use h1.family), then body.
|
|
749
|
+
heading: "var(--font-h1-family, var(--font-heading-family, var(--font-body-family, ui-sans-serif, system-ui, sans-serif)))",
|
|
750
|
+
// Body — `typography.body.family` → `--font-body-family`. Legacy
|
|
751
|
+
// nested kits (`typography.body.md.family`) emit `--font-body-md-family`.
|
|
752
|
+
body: "var(--font-body-family, var(--font-body-md-family, ui-sans-serif, system-ui, sans-serif))",
|
|
753
|
+
// Mono / code — designkit convention uses `code.*`, some kits ship `mono.*`.
|
|
754
|
+
mono: "var(--font-code-family, var(--font-mono-family, ui-monospace, SFMono-Regular, monospace))"
|
|
755
|
+
},
|
|
756
|
+
spacing: {
|
|
757
|
+
xs: "var(--spacing-xs, 0.25rem)",
|
|
758
|
+
sm: "var(--spacing-sm, 0.5rem)",
|
|
759
|
+
md: "var(--spacing-md, 1rem)",
|
|
760
|
+
lg: "var(--spacing-lg, 1.5rem)",
|
|
761
|
+
xl: "var(--spacing-xl, 2rem)",
|
|
762
|
+
"2xl": "var(--spacing-2xl, 3rem)"
|
|
763
|
+
},
|
|
764
|
+
borderRadius: {
|
|
765
|
+
sm: "var(--rounded-sm, 0.25rem)",
|
|
766
|
+
md: "var(--rounded-md, 0.5rem)",
|
|
767
|
+
lg: "var(--rounded-lg, 0.75rem)",
|
|
768
|
+
xl: "var(--rounded-xl, 1rem)",
|
|
769
|
+
full: "var(--rounded-full, 9999px)"
|
|
770
|
+
},
|
|
771
|
+
boxShadow: {
|
|
772
|
+
sm: "var(--shadow-sm, 0 1px 2px 0 rgb(0 0 0 / 0.05))",
|
|
773
|
+
md: "var(--shadow-md, 0 4px 6px -1px rgb(0 0 0 / 0.1))",
|
|
774
|
+
lg: "var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1))"
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
var DEFAULT_CDN_URL = "https://cdn.tailwindcss.com";
|
|
778
|
+
function buildConfigScript(themeExtend) {
|
|
779
|
+
const json = JSON.stringify(themeExtend, null, 2);
|
|
780
|
+
return [
|
|
781
|
+
"<script>",
|
|
782
|
+
" tailwind.config = {",
|
|
783
|
+
` theme: { extend: ${json.replace(/\n/g, "\n ")} },`,
|
|
784
|
+
" }",
|
|
785
|
+
"</script>"
|
|
786
|
+
].join("\n");
|
|
787
|
+
}
|
|
788
|
+
var V3_GROUP_TO_V4_NAMESPACE = {
|
|
789
|
+
colors: "--color-",
|
|
790
|
+
fontFamily: "--font-",
|
|
791
|
+
spacing: "--spacing-",
|
|
792
|
+
borderRadius: "--radius-",
|
|
793
|
+
boxShadow: "--shadow-"
|
|
794
|
+
};
|
|
795
|
+
function themeExtendToThemeCss(themeExtend) {
|
|
796
|
+
const decls = [];
|
|
797
|
+
for (const [group, ns] of Object.entries(V3_GROUP_TO_V4_NAMESPACE)) {
|
|
798
|
+
const entries = themeExtend[group];
|
|
799
|
+
if (!entries || typeof entries !== "object") continue;
|
|
800
|
+
for (const [key, value] of Object.entries(
|
|
801
|
+
entries
|
|
802
|
+
)) {
|
|
803
|
+
if (typeof value !== "string") continue;
|
|
804
|
+
decls.push(` ${ns}${key}: ${value};`);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
return decls.join("\n");
|
|
808
|
+
}
|
|
809
|
+
var STANDARD_SIZE_SCALE = {
|
|
810
|
+
"3xs": "16rem",
|
|
811
|
+
"2xs": "18rem",
|
|
812
|
+
xs: "20rem",
|
|
813
|
+
sm: "24rem",
|
|
814
|
+
md: "28rem",
|
|
815
|
+
lg: "32rem",
|
|
816
|
+
xl: "36rem",
|
|
817
|
+
"2xl": "42rem",
|
|
818
|
+
"3xl": "48rem",
|
|
819
|
+
"4xl": "56rem",
|
|
820
|
+
"5xl": "64rem",
|
|
821
|
+
"6xl": "72rem",
|
|
822
|
+
"7xl": "80rem"
|
|
823
|
+
};
|
|
824
|
+
var SIZE_NAMESPACES = [
|
|
825
|
+
"--container-",
|
|
826
|
+
"--width-",
|
|
827
|
+
"--min-width-",
|
|
828
|
+
"--max-width-",
|
|
829
|
+
"--height-",
|
|
830
|
+
"--min-height-",
|
|
831
|
+
"--max-height-"
|
|
832
|
+
];
|
|
833
|
+
function buildStandardSizingCss() {
|
|
834
|
+
const decls = [];
|
|
835
|
+
for (const ns of SIZE_NAMESPACES) {
|
|
836
|
+
for (const [key, value] of Object.entries(STANDARD_SIZE_SCALE)) {
|
|
837
|
+
decls.push(` ${ns}${key}: ${value};`);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
return decls.join("\n");
|
|
841
|
+
}
|
|
842
|
+
function buildEntryCss(themeCss) {
|
|
843
|
+
return `@import "tailwindcss";
|
|
844
|
+
@theme inline {
|
|
845
|
+
${buildStandardSizingCss()}
|
|
846
|
+
${themeCss}
|
|
847
|
+
}
|
|
848
|
+
`;
|
|
849
|
+
}
|
|
850
|
+
function injectIntoHead2(body, trailing) {
|
|
851
|
+
const closeHead = body.search(/<\/head\s*>/i);
|
|
852
|
+
if (closeHead >= 0) {
|
|
853
|
+
return `${body.slice(0, closeHead)}${trailing}
|
|
854
|
+
${body.slice(closeHead)}`;
|
|
855
|
+
}
|
|
856
|
+
const openHead = body.match(/<head\b[^>]*>/i);
|
|
857
|
+
if (openHead && openHead.index !== void 0) {
|
|
858
|
+
const at = openHead.index + openHead[0].length;
|
|
859
|
+
return `${body.slice(0, at)}
|
|
860
|
+
${trailing}${body.slice(at)}`;
|
|
861
|
+
}
|
|
862
|
+
const openBody = body.match(/<body\b[^>]*>/i);
|
|
863
|
+
if (openBody && openBody.index !== void 0) {
|
|
864
|
+
return `${body.slice(0, openBody.index)}${trailing}
|
|
865
|
+
${body.slice(openBody.index)}`;
|
|
866
|
+
}
|
|
867
|
+
return `${body}
|
|
868
|
+
${trailing}`;
|
|
869
|
+
}
|
|
870
|
+
function extractClasses(html) {
|
|
871
|
+
const out = /* @__PURE__ */ new Set();
|
|
872
|
+
const re = /class(?:Name)?\s*=\s*("([^"]*)"|'([^']*)')/gi;
|
|
873
|
+
let m;
|
|
874
|
+
while ((m = re.exec(html)) !== null) {
|
|
875
|
+
const raw = m[2] ?? m[3] ?? "";
|
|
876
|
+
for (const tok of raw.split(/\s+/)) {
|
|
877
|
+
if (tok) out.add(tok);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
return [...out];
|
|
881
|
+
}
|
|
882
|
+
function stripSelfReferentialRootDecls(css) {
|
|
883
|
+
return css.replace(
|
|
884
|
+
/^[ \t]*--([a-z0-9-]+):\s*var\(\s*--\1[\s,)][^;]*;[ \t]*\r?\n?/gim,
|
|
885
|
+
""
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
var _compileCache = /* @__PURE__ */ new Map();
|
|
889
|
+
async function getBuild(entryCss) {
|
|
890
|
+
let cached = _compileCache.get(entryCss);
|
|
891
|
+
if (!cached) {
|
|
892
|
+
cached = (async () => {
|
|
893
|
+
const { compile } = await import('@tailwindcss/node');
|
|
894
|
+
const require2 = createRequire(import.meta.url);
|
|
895
|
+
const base = dirname(require2.resolve("tailwindcss/package.json"));
|
|
896
|
+
const { build } = await compile(entryCss, {
|
|
897
|
+
base,
|
|
898
|
+
onDependency: () => {
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
return build;
|
|
902
|
+
})();
|
|
903
|
+
_compileCache.set(entryCss, cached);
|
|
904
|
+
}
|
|
905
|
+
return cached;
|
|
906
|
+
}
|
|
907
|
+
function tailwindBridgeExtension(options = {}) {
|
|
908
|
+
const mode = options.mode ?? "compile";
|
|
909
|
+
const themeExtend = options.themeExtend ?? DEFAULT_THEME_EXTEND;
|
|
910
|
+
if (mode === "cdn") {
|
|
911
|
+
const cdnUrl = options.cdnUrl ?? DEFAULT_CDN_URL;
|
|
912
|
+
const configScript = buildConfigScript(themeExtend);
|
|
913
|
+
const cdnScript = `<script src="${cdnUrl}"></script>`;
|
|
914
|
+
return async () => ({
|
|
915
|
+
contextAdditions: {},
|
|
916
|
+
transformBody: (body) => injectIntoHead2(body, `${cdnScript}
|
|
917
|
+
${configScript}`)
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
const themeCss = options.themeCss ?? themeExtendToThemeCss(themeExtend);
|
|
921
|
+
const entryCss = buildEntryCss(themeCss);
|
|
922
|
+
return async () => ({
|
|
923
|
+
contextAdditions: {},
|
|
924
|
+
transformBody: async (body) => {
|
|
925
|
+
try {
|
|
926
|
+
const build = await getBuild(entryCss);
|
|
927
|
+
const candidates = extractClasses(body);
|
|
928
|
+
const compiled = stripSelfReferentialRootDecls(build(candidates));
|
|
929
|
+
return injectIntoHead2(
|
|
930
|
+
body,
|
|
931
|
+
`<style data-canvakit-tailwind="compiled">
|
|
932
|
+
${compiled}
|
|
933
|
+
</style>`
|
|
934
|
+
);
|
|
935
|
+
} catch (err) {
|
|
936
|
+
console.warn(
|
|
937
|
+
`[canvakit-designkit] Tailwind compile failed, falling back to CDN: ${err instanceof Error ? err.message : String(err)}`
|
|
938
|
+
);
|
|
939
|
+
const cdnUrl = options.cdnUrl ?? DEFAULT_CDN_URL;
|
|
940
|
+
const configScript = buildConfigScript(themeExtend);
|
|
941
|
+
return injectIntoHead2(
|
|
942
|
+
body,
|
|
943
|
+
`<script src="${cdnUrl}"></script>
|
|
944
|
+
${configScript}`
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
export { DesignSourceRegistry, createDefaultRegistry, defaultGoogleFontsFetcher, defaultRegistry, designkitExtension, loadActiveDesign, tailwindBridgeExtension };
|
|
952
|
+
//# sourceMappingURL=index.mjs.map
|
|
953
|
+
//# sourceMappingURL=index.mjs.map
|