@eldrajs/vite-plugin-theme 0.0.0-stage → 0.1.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/CHANGELOG.md +259 -0
- package/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/index.d.ts +74 -0
- package/dist/index.js +452 -0
- package/dist/scan-DjW4f_E8.js +2162 -0
- package/dist/scan.d.ts +6 -0
- package/dist/scan.js +2 -0
- package/dist/types-BRNXfpXW.d.ts +430 -0
- package/package.json +59 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { a as validateTemplateRoles, i as validatePageSeeds, n as scanTheme, o as validateTemplateSeeds, r as seedLayout, t as readPreviousManifest } from "./scan-DjW4f_E8.js";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
import { generateDesignTokenCss, generateTailwindThemeCss, normalizeThemeDesignTokens } from "@eldrajs/theme-core/design-tokens";
|
|
5
|
+
import { DEFAULT_LAYOUT_BREAKPOINTS } from "@eldrajs/theme-core/layout";
|
|
6
|
+
//#region src/blockTypes.ts
|
|
7
|
+
/**
|
|
8
|
+
* Generates `.eldra/block-types.d.ts`: a global ambient contract for every
|
|
9
|
+
* block declared under `blocks/*.block.json` — `interface EldraBlockData`
|
|
10
|
+
* (one property per block `apiId`, typed from its `fields`) and the
|
|
11
|
+
* `EldraBlockEntry<K>` helper a `Block.vue` uses in `defineProps`. Written by
|
|
12
|
+
* the Vite plugin on scan (`plugin.ts`) and by `eldra-theme types --blocks`
|
|
13
|
+
* (`theme-cli`).
|
|
14
|
+
*
|
|
15
|
+
* Field mapping (spec §3 "Block contract"): required validators
|
|
16
|
+
* (`validators.required === true`) make the TS property non-optional;
|
|
17
|
+
* everything else is optional (`?`). `composite`/`json`/`styling`/`seo`/
|
|
18
|
+
* `reference` — and any type this generator does not recognize — fall back
|
|
19
|
+
* to `Record<string, unknown>` (typed later), except when a `composite`
|
|
20
|
+
* field is a `list`'s item shape, where its declared `metadata.fields` are
|
|
21
|
+
* expanded into an inline object type so `list` produces a real
|
|
22
|
+
* `Array<{ … }>` rather than `Array<Record<string, unknown>>`.
|
|
23
|
+
*
|
|
24
|
+
* The one `reference` shape that is typed is a relation targeting catalog
|
|
25
|
+
* collections and nothing else (`relation.allowCollections` with no
|
|
26
|
+
* `allowedTagIds` and no `allowProducts`): `EldraCollectionReference | null`,
|
|
27
|
+
* or `EldraCollectionReference[]` when the relation is `multiple`. A relation
|
|
28
|
+
* mixing targets keeps `Record<string, unknown>`, because the value's shape
|
|
29
|
+
* then depends on which kind of thing the author picked.
|
|
30
|
+
*/
|
|
31
|
+
/** Emitted into the generated preamble only when some block declares a `link`
|
|
32
|
+
* field, the way `RichTextNode` is only imported when one declares rich text.
|
|
33
|
+
* `target` and `url` are both optional because exactly one of them is set, and
|
|
34
|
+
* which one depends on `kind`; `children` is one level deep and never more. */
|
|
35
|
+
const LINK_INTERFACE = ` /** A destination a \`link\` field points at: a catalog object, an entry,
|
|
36
|
+
* a page, or an external URL — or \`kind: "none"\`, a heading that groups
|
|
37
|
+
* the links under it and goes nowhere itself. Resolve it to an href with
|
|
38
|
+
* \`resolveLink\` from \`@eldrajs/theme-core/links\`. */
|
|
39
|
+
interface EldraLink {
|
|
40
|
+
kind: 'product' | 'collection' | 'category' | 'entry' | 'page' | 'url' | 'none';
|
|
41
|
+
target?: { _type: string; id: string };
|
|
42
|
+
url?: string;
|
|
43
|
+
label?: string;
|
|
44
|
+
openInNewTab?: boolean;
|
|
45
|
+
group?: string;
|
|
46
|
+
children?: EldraLink[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
`;
|
|
50
|
+
function generateBlockTypes(blocks, messages) {
|
|
51
|
+
const sorted = [...blocks].sort((a, b) => a.apiId.localeCompare(b.apiId));
|
|
52
|
+
const usesRichText = sorted.some((block) => fieldsUseTypeDeep(block.fields ?? [], "rich-text"));
|
|
53
|
+
const usesLink = sorted.some((block) => fieldsUseTypeDeep(block.fields ?? [], "link"));
|
|
54
|
+
const typeImports = usesRichText ? "ImageFraming, RichTextNode" : "ImageFraming";
|
|
55
|
+
const blockDataBody = sorted.length === 0 ? "" : `\n${sorted.map((block) => ` ${propertyKey(block.apiId)}: ${blockDataType(block.fields ?? [], " ")};`).join("\n")}\n `;
|
|
56
|
+
return `// generated by @eldrajs/vite-plugin-theme — do not edit
|
|
57
|
+
|
|
58
|
+
import type { ${typeImports} } from '@eldrajs/theme-core';
|
|
59
|
+
|
|
60
|
+
declare global {
|
|
61
|
+
interface EldraMedia {
|
|
62
|
+
assetId: string;
|
|
63
|
+
url: string;
|
|
64
|
+
altText?: string;
|
|
65
|
+
width?: number;
|
|
66
|
+
height?: number;
|
|
67
|
+
framing?: ImageFraming;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A catalog collection a \`reference\` field points at. Only \`id\` and
|
|
71
|
+
* \`_type\` are guaranteed: the public read returns the stub at depth 0, for
|
|
72
|
+
* an archived collection, and for a draft overlay in the page builder. */
|
|
73
|
+
interface EldraCollectionReference {
|
|
74
|
+
id: string;
|
|
75
|
+
_type: 'collection';
|
|
76
|
+
slug?: string;
|
|
77
|
+
status?: string;
|
|
78
|
+
type?: string;
|
|
79
|
+
productCount?: number;
|
|
80
|
+
translations?: unknown;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
${usesLink ? LINK_INTERFACE : ""} interface EldraBlockData {${blockDataBody}}
|
|
84
|
+
|
|
85
|
+
type EldraBlockEntry<K extends keyof EldraBlockData> = {
|
|
86
|
+
id: string;
|
|
87
|
+
data: EldraBlockData[K];
|
|
88
|
+
};
|
|
89
|
+
${messageKeyDeclaration(messages)}}
|
|
90
|
+
|
|
91
|
+
export {};
|
|
92
|
+
`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* `MessageKey`, a union of the theme's own message keys (its default
|
|
96
|
+
* locale's flattened `i18n/<tag>.json` keys) for a typed `t()` argument — so
|
|
97
|
+
* a theme keeps typed keys without a hand-written `Messages` interface.
|
|
98
|
+
* Emitted only when the theme ships at least one key; absent when it
|
|
99
|
+
* declares no `i18n/` directory, the same way `EldraLink` is only emitted
|
|
100
|
+
* when some block declares a `link` field.
|
|
101
|
+
*/
|
|
102
|
+
function messageKeyDeclaration(messages) {
|
|
103
|
+
if (messages === void 0) return "";
|
|
104
|
+
const keys = Object.keys(messages.locales[messages.defaultLocale] ?? {}).sort();
|
|
105
|
+
if (keys.length === 0) return "";
|
|
106
|
+
return `
|
|
107
|
+
/** The theme's own message keys (\`i18n/<tag>.json\`'s default locale),
|
|
108
|
+
* flattened to dotted paths — vue-i18n's \`t()\` argument. */
|
|
109
|
+
type MessageKey = ${keys.map(singleQuoted).join(" | ")};
|
|
110
|
+
`;
|
|
111
|
+
}
|
|
112
|
+
function blockDataType(fields, indent) {
|
|
113
|
+
if (fields.length === 0) return "{}";
|
|
114
|
+
return `{\n${fields.map((field) => `${indent} ${propertyKey(field.fieldId)}${isRequired(field) ? "" : "?"}: ${fieldTypeExpr(field)};`).join("\n")}\n${indent}}`;
|
|
115
|
+
}
|
|
116
|
+
function fieldTypeExpr(field) {
|
|
117
|
+
switch (field.type) {
|
|
118
|
+
case "string":
|
|
119
|
+
case "text":
|
|
120
|
+
case "markdown":
|
|
121
|
+
case "slug":
|
|
122
|
+
case "uuid":
|
|
123
|
+
case "color":
|
|
124
|
+
case "icon":
|
|
125
|
+
case "tabler-icon":
|
|
126
|
+
case "lucide-icon":
|
|
127
|
+
case "datetime": return "string";
|
|
128
|
+
case "bool": return "boolean";
|
|
129
|
+
case "int":
|
|
130
|
+
case "float": return "number";
|
|
131
|
+
case "rich-text": return "RichTextNode";
|
|
132
|
+
case "link": return "EldraLink";
|
|
133
|
+
case "media": return isMultipleMedia(field) ? "EldraMedia[]" : "EldraMedia";
|
|
134
|
+
case "select": return selectUnion(field);
|
|
135
|
+
case "list": return listType(field);
|
|
136
|
+
case "reference": return referenceType(field);
|
|
137
|
+
default: return "Record<string, unknown>";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** A `reference` field's type. Collections-only relations are the one shape
|
|
141
|
+
* whose resolved value is known here — see the module doc comment. */
|
|
142
|
+
function referenceType(field) {
|
|
143
|
+
const relation = field.relation;
|
|
144
|
+
if (relation === void 0 || !isCollectionsOnly(relation)) return "Record<string, unknown>";
|
|
145
|
+
return relation.multiple === true ? "EldraCollectionReference[]" : "EldraCollectionReference | null";
|
|
146
|
+
}
|
|
147
|
+
function isCollectionsOnly(relation) {
|
|
148
|
+
return relation.allowCollections === true && relation.allowProducts !== true && (relation.allowedTagIds === void 0 || relation.allowedTagIds.length === 0);
|
|
149
|
+
}
|
|
150
|
+
function isMultipleMedia(field) {
|
|
151
|
+
return isRecord(field.metadata) && field.metadata.multiple === true;
|
|
152
|
+
}
|
|
153
|
+
function selectUnion(field) {
|
|
154
|
+
const metadata = field.metadata;
|
|
155
|
+
const literals = (isRecord(metadata) && Array.isArray(metadata.options) ? metadata.options : []).filter((option) => typeof option === "string");
|
|
156
|
+
return literals.length > 0 ? literals.map(singleQuoted).join(" | ") : "string";
|
|
157
|
+
}
|
|
158
|
+
function singleQuoted(value) {
|
|
159
|
+
return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
160
|
+
}
|
|
161
|
+
function listType(field) {
|
|
162
|
+
const item = isRecord(field.metadata) ? field.metadata.item : void 0;
|
|
163
|
+
if (!isBlockFieldLike(item)) return "unknown[]";
|
|
164
|
+
return `Array<${itemTypeExpr(item)}>`;
|
|
165
|
+
}
|
|
166
|
+
/** The type of one `list` entry. A `composite` item expands its declared
|
|
167
|
+
* `metadata.fields` inline instead of collapsing to `Record<string, unknown>`
|
|
168
|
+
* — see the module doc comment. */
|
|
169
|
+
function itemTypeExpr(item) {
|
|
170
|
+
if (item.type === "composite") return inlineObjectType(compositeFields(item));
|
|
171
|
+
return fieldTypeExpr(item);
|
|
172
|
+
}
|
|
173
|
+
function compositeFields(field) {
|
|
174
|
+
const metadata = field.metadata;
|
|
175
|
+
return (isRecord(metadata) && Array.isArray(metadata.fields) ? metadata.fields : []).filter(isBlockFieldLike);
|
|
176
|
+
}
|
|
177
|
+
function inlineObjectType(fields) {
|
|
178
|
+
if (fields.length === 0) return "Record<string, unknown>";
|
|
179
|
+
return `{ ${fields.map((field) => `${propertyKey(field.fieldId)}${isRequired(field) ? "" : "?"}: ${fieldTypeExpr(field)}`).join("; ")} }`;
|
|
180
|
+
}
|
|
181
|
+
const VALID_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
182
|
+
/** Quotes a property key when it is not a valid bare TS identifier — block
|
|
183
|
+
* `apiId`s may contain hyphens (e.g. `feature-grid`), which field ids never
|
|
184
|
+
* do, but this is applied uniformly for safety. */
|
|
185
|
+
function propertyKey(id) {
|
|
186
|
+
return VALID_IDENTIFIER.test(id) ? id : singleQuoted(id);
|
|
187
|
+
}
|
|
188
|
+
function isRequired(field) {
|
|
189
|
+
return isRecord(field.validators) && field.validators.required === true;
|
|
190
|
+
}
|
|
191
|
+
function fieldsUseTypeDeep(fields, type) {
|
|
192
|
+
return fields.some((field) => fieldUsesTypeDeep(field, type));
|
|
193
|
+
}
|
|
194
|
+
function fieldUsesTypeDeep(field, type) {
|
|
195
|
+
if (field.type === type) return true;
|
|
196
|
+
if (field.type === "list") {
|
|
197
|
+
const item = isRecord(field.metadata) ? field.metadata.item : void 0;
|
|
198
|
+
return isBlockFieldLike(item) && fieldUsesTypeDeep(item, type);
|
|
199
|
+
}
|
|
200
|
+
if (field.type === "composite") return compositeFields(field).some((child) => fieldUsesTypeDeep(child, type));
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
function isBlockFieldLike(value) {
|
|
204
|
+
return isRecord(value) && typeof value.fieldId === "string" && typeof value.type === "string";
|
|
205
|
+
}
|
|
206
|
+
function isRecord(value) {
|
|
207
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
208
|
+
}
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/plugin.ts
|
|
211
|
+
const MANIFEST_ID = "virtual:eldra/manifest";
|
|
212
|
+
const BLOCKS_ID = "virtual:eldra/blocks";
|
|
213
|
+
const BLOCK_FIELDS_ID = "virtual:eldra/block-fields";
|
|
214
|
+
const BREAKPOINTS_ID = "virtual:eldra/breakpoints";
|
|
215
|
+
const TOKENS_ID = "virtual:eldra/tokens.css";
|
|
216
|
+
const TAILWIND_ID = "virtual:eldra/tailwind-theme.css";
|
|
217
|
+
const MESSAGES_ID = "virtual:eldra/messages";
|
|
218
|
+
const RESOLVED_MANIFEST_ID = `\0${MANIFEST_ID}`;
|
|
219
|
+
const RESOLVED_BLOCKS_ID = `\0${BLOCKS_ID}`;
|
|
220
|
+
const RESOLVED_BLOCK_FIELDS_ID = `\0${BLOCK_FIELDS_ID}`;
|
|
221
|
+
const RESOLVED_BREAKPOINTS_ID = `\0${BREAKPOINTS_ID}`;
|
|
222
|
+
const RESOLVED_TOKENS_ID = `\0${TOKENS_ID}`;
|
|
223
|
+
const RESOLVED_TAILWIND_ID = `\0${TAILWIND_ID}`;
|
|
224
|
+
const RESOLVED_MESSAGES_ID = `\0${MESSAGES_ID}`;
|
|
225
|
+
/** `virtual:eldra/messages`'s shape when the theme ships no `i18n/`
|
|
226
|
+
* directory — `manifest.messages` is absent, not an empty catalogue, so the
|
|
227
|
+
* module needs its own fallback rather than reading one off the manifest. */
|
|
228
|
+
const EMPTY_MESSAGES = {
|
|
229
|
+
defaultLocale: "en-US",
|
|
230
|
+
locales: {}
|
|
231
|
+
};
|
|
232
|
+
function eldraTheme(options = {}) {
|
|
233
|
+
let config = null;
|
|
234
|
+
let themeDir = "";
|
|
235
|
+
let scan = {
|
|
236
|
+
manifest: null,
|
|
237
|
+
blockDirs: {},
|
|
238
|
+
errors: [],
|
|
239
|
+
breakpoints: DEFAULT_LAYOUT_BREAKPOINTS
|
|
240
|
+
};
|
|
241
|
+
function rescanOrThrow(failHard) {
|
|
242
|
+
const historyErrors = [];
|
|
243
|
+
const previousManifest = readPreviousManifest(themeDir, historyErrors);
|
|
244
|
+
scan = scanTheme({
|
|
245
|
+
previousManifest,
|
|
246
|
+
themeDir,
|
|
247
|
+
framework: options.framework ?? "vite",
|
|
248
|
+
routes: options.routes,
|
|
249
|
+
customPages: options.customPages,
|
|
250
|
+
templates: options.templates,
|
|
251
|
+
templateRoles: options.templateRoles,
|
|
252
|
+
breakpoints: options.breakpoints
|
|
253
|
+
});
|
|
254
|
+
scan.errors.unshift(...historyErrors);
|
|
255
|
+
if (scan.errors.length > 0 || scan.manifest === null) {
|
|
256
|
+
scan.manifest = null;
|
|
257
|
+
const message = `eldra theme validation failed:\n ${scan.errors.join("\n ")}`;
|
|
258
|
+
if (failHard) throw new Error(message);
|
|
259
|
+
config?.logger.error(message);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
writeManifest(join(themeDir, ".eldra", "manifest.json"), scan.manifest);
|
|
263
|
+
writeBlockTypes(join(themeDir, ".eldra", "block-types.d.ts"), scan.manifest.blocks, scan.manifest.messages);
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
name: "eldra-theme",
|
|
267
|
+
config() {
|
|
268
|
+
return { define: {
|
|
269
|
+
"import.meta.env.ELDRA_GATEWAY_URL": JSON.stringify(process.env.ELDRA_GATEWAY_URL ?? ""),
|
|
270
|
+
"import.meta.env.ELDRA_ORG_ID": JSON.stringify(process.env.ELDRA_ORG_ID ?? "")
|
|
271
|
+
} };
|
|
272
|
+
},
|
|
273
|
+
configResolved(resolved) {
|
|
274
|
+
config = resolved;
|
|
275
|
+
themeDir = options.themeDir ?? resolved.root;
|
|
276
|
+
},
|
|
277
|
+
buildStart() {
|
|
278
|
+
rescanOrThrow(config?.command === "build");
|
|
279
|
+
if (options.tailwind === true) assertTailwindV4(themeDir);
|
|
280
|
+
},
|
|
281
|
+
resolveId(id) {
|
|
282
|
+
if (id === MANIFEST_ID) return RESOLVED_MANIFEST_ID;
|
|
283
|
+
if (id === BLOCKS_ID) return RESOLVED_BLOCKS_ID;
|
|
284
|
+
if (id === BLOCK_FIELDS_ID) return RESOLVED_BLOCK_FIELDS_ID;
|
|
285
|
+
if (id === BREAKPOINTS_ID) return RESOLVED_BREAKPOINTS_ID;
|
|
286
|
+
if (id === MESSAGES_ID) return RESOLVED_MESSAGES_ID;
|
|
287
|
+
if (id === TOKENS_ID) return RESOLVED_TOKENS_ID;
|
|
288
|
+
if (id === TAILWIND_ID) {
|
|
289
|
+
if (options.tailwind !== true) throw new Error("eldra: virtual:eldra/tailwind-theme.css is disabled; set tailwind: true in eldraTheme()");
|
|
290
|
+
return RESOLVED_TAILWIND_ID;
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
},
|
|
294
|
+
load(id) {
|
|
295
|
+
if (id === RESOLVED_MANIFEST_ID) return `export default ${JSON.stringify(scan.manifest)};`;
|
|
296
|
+
if (id === RESOLVED_BLOCKS_ID) return `export default {\n${Object.entries(scan.blockDirs).sort(([a], [b]) => a.localeCompare(b)).map(([apiId, directory]) => blockImportLine(apiId, join(directory, "Block.vue"))).join(",\n")}\n};`;
|
|
297
|
+
if (id === RESOLVED_BLOCK_FIELDS_ID) {
|
|
298
|
+
if (scan.manifest === null) throw new Error("eldra theme manifest is unavailable");
|
|
299
|
+
return blockFieldsModuleSource(scan.manifest);
|
|
300
|
+
}
|
|
301
|
+
if (id === RESOLVED_BREAKPOINTS_ID) return `export default ${JSON.stringify(scan.breakpoints)};`;
|
|
302
|
+
if (id === RESOLVED_MESSAGES_ID) {
|
|
303
|
+
const manifestMessages = scan.manifest?.messages ?? EMPTY_MESSAGES;
|
|
304
|
+
const messages = options.resolveMessages ? options.resolveMessages(manifestMessages) : manifestMessages;
|
|
305
|
+
return `export default ${JSON.stringify(messages)};`;
|
|
306
|
+
}
|
|
307
|
+
if (id === RESOLVED_TOKENS_ID) {
|
|
308
|
+
if (scan.manifest === null) throw new Error("eldra theme manifest is unavailable");
|
|
309
|
+
const tokens = options.resolveTokens ? options.resolveTokens(scan.manifest.tokens) : normalizeThemeDesignTokens(scan.manifest.tokens);
|
|
310
|
+
return generateDesignTokenCss(tokens);
|
|
311
|
+
}
|
|
312
|
+
if (id === RESOLVED_TAILWIND_ID) {
|
|
313
|
+
if (scan.manifest === null) throw new Error("eldra theme manifest is unavailable");
|
|
314
|
+
const tokens = normalizeThemeDesignTokens(scan.manifest.tokens);
|
|
315
|
+
return `@import "tailwindcss";${generateDesignTokenCss(tokens)}${generateTailwindThemeCss(tokens)}`;
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
},
|
|
319
|
+
handleHotUpdate(context) {
|
|
320
|
+
const file = relative(themeDir, context.file).split("\\").join("/");
|
|
321
|
+
if (!/^blocks\/[^/]+\/(block\.json|mock\.json|preview\.png)$/.test(file) && !/^i18n\/[^/]+\.json$/.test(file) && file !== "tokens.json" && file !== "package.json") return;
|
|
322
|
+
rescanOrThrow(false);
|
|
323
|
+
for (const virtualId of [
|
|
324
|
+
RESOLVED_MANIFEST_ID,
|
|
325
|
+
RESOLVED_BLOCKS_ID,
|
|
326
|
+
RESOLVED_BLOCK_FIELDS_ID,
|
|
327
|
+
RESOLVED_BREAKPOINTS_ID,
|
|
328
|
+
RESOLVED_TOKENS_ID,
|
|
329
|
+
RESOLVED_TAILWIND_ID,
|
|
330
|
+
RESOLVED_MESSAGES_ID
|
|
331
|
+
]) {
|
|
332
|
+
const module = context.server.moduleGraph.getModuleById(virtualId);
|
|
333
|
+
if (module !== void 0) context.server.moduleGraph.invalidateModule(module);
|
|
334
|
+
}
|
|
335
|
+
context.server.ws.send({ type: "full-reload" });
|
|
336
|
+
return [];
|
|
337
|
+
},
|
|
338
|
+
generateBundle() {
|
|
339
|
+
if (scan.manifest === null) throw new Error("eldra theme manifest is unavailable");
|
|
340
|
+
this.emitFile({
|
|
341
|
+
type: "asset",
|
|
342
|
+
fileName: ".eldra/manifest.json",
|
|
343
|
+
source: serializeManifest(scan.manifest)
|
|
344
|
+
});
|
|
345
|
+
for (const block of scan.manifest.blocks) {
|
|
346
|
+
if (block.previewImage === null) continue;
|
|
347
|
+
const source = join(themeDir, block.previewImage);
|
|
348
|
+
if (!existsSync(source)) continue;
|
|
349
|
+
this.emitFile({
|
|
350
|
+
type: "asset",
|
|
351
|
+
fileName: block.previewImage,
|
|
352
|
+
source: readFileSync(source)
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function assertTailwindV4(themeDir) {
|
|
359
|
+
let directory = themeDir;
|
|
360
|
+
for (;;) {
|
|
361
|
+
const packagePath = join(directory, "node_modules", "tailwindcss", "package.json");
|
|
362
|
+
if (existsSync(packagePath)) {
|
|
363
|
+
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
364
|
+
const version = typeof pkg.version === "string" ? pkg.version : "";
|
|
365
|
+
if (!version.startsWith("4.")) throw new Error(`eldra: Tailwind adapter requires tailwindcss major 4 (found ${version || "unknown"}); set tailwind: false to disable it`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const parent = dirname(directory);
|
|
369
|
+
if (parent === directory) break;
|
|
370
|
+
directory = parent;
|
|
371
|
+
}
|
|
372
|
+
throw new Error("eldra: Tailwind adapter is enabled but tailwindcss was not found; install tailwindcss@^4 or set tailwind: false");
|
|
373
|
+
}
|
|
374
|
+
function writeManifest(path, manifest) {
|
|
375
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
376
|
+
writeFileSync(path, serializeManifest(manifest));
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Writes `.eldra/block-types.d.ts` from the manifest's blocks (which already
|
|
380
|
+
* carry every `BlockDefinition` field plus `mock`/`previewImage` — the
|
|
381
|
+
* generator only reads `apiId`/`fields`, so the extra keys are harmless).
|
|
382
|
+
* Tracked like `manifest.json`, but only rewritten when content actually
|
|
383
|
+
* changed, so an unrelated rescan does not touch its mtime.
|
|
384
|
+
*/
|
|
385
|
+
function writeBlockTypes(path, blocks, messages) {
|
|
386
|
+
const content = generateBlockTypes(blocks, messages);
|
|
387
|
+
if (existsSync(path) && readFileSync(path, "utf8") === content) return;
|
|
388
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
389
|
+
writeFileSync(path, content);
|
|
390
|
+
}
|
|
391
|
+
function serializeManifest(manifest) {
|
|
392
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Builds `virtual:eldra/block-fields`: per block, only `fieldId`, `type`,
|
|
396
|
+
* `localized` (omitted when false) and `metadata` (nested composite
|
|
397
|
+
* `metadata.fields` passed through as-is) — the slice theme-core needs at
|
|
398
|
+
* runtime without pulling in the rest of the manifest. `isBlockFieldLocalized`
|
|
399
|
+
* uses `fieldId`/`localized` to resolve a field's locale default;
|
|
400
|
+
* `isBlockFieldSelect` uses `type` plus `metadata.item`/`metadata.fields`
|
|
401
|
+
* (carried verbatim here) to resolve a field's type through `list`/`composite`
|
|
402
|
+
* nesting. `metadata` is also carried because a theme may read its own keys;
|
|
403
|
+
* the SDK itself no longer reads `metadata.toolbar` (§18 v2 moved the toolbar
|
|
404
|
+
* to Studio).
|
|
405
|
+
*/
|
|
406
|
+
function blockFieldsModuleSource(manifest) {
|
|
407
|
+
return `export default {\n${manifest.blocks.map((block) => {
|
|
408
|
+
return [String(block.apiId ?? ""), (Array.isArray(block.fields) ? block.fields : []).map((field) => {
|
|
409
|
+
const entry = {
|
|
410
|
+
fieldId: String(field.fieldId ?? ""),
|
|
411
|
+
type: String(field.type ?? "")
|
|
412
|
+
};
|
|
413
|
+
if (field.localized === true) entry.localized = true;
|
|
414
|
+
if (field.metadata !== null && typeof field.metadata === "object") entry.metadata = field.metadata;
|
|
415
|
+
return entry;
|
|
416
|
+
})];
|
|
417
|
+
}).sort(([a], [b]) => a.localeCompare(b)).map(([apiId, projected]) => ` ${JSON.stringify(apiId)}: ${JSON.stringify(projected)}`).join(",\n")}\n};`;
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* One line of the generated `virtual:eldra/blocks` module:
|
|
421
|
+
* ` "<apiId>": () => import("<path>")`. Both `apiId` (a block's directory name,
|
|
422
|
+
* which need not match `API_ID_PATTERN` when `block.json`'s own `apiId` is
|
|
423
|
+
* missing — `scanTheme` then falls back to the raw directory name) and `path`
|
|
424
|
+
* (that directory joined with `Block.vue`) are scanned from disk, so each is
|
|
425
|
+
* `library input` as far as static analysis is concerned. Rather than escaping
|
|
426
|
+
* them with two separate `JSON.stringify` calls spliced into one template —
|
|
427
|
+
* two partially-sanitized pieces glued together by hand around a `() =>
|
|
428
|
+
* import(...)` wrapper, which read as the code construction CodeQL's
|
|
429
|
+
* "improperly sanitized value" check flags — this stringifies the whole
|
|
430
|
+
* `[apiId, path]` pair in one call and only ever reuses the two JSON string
|
|
431
|
+
* literals it produced, by slicing them back out; nothing is re-escaped or
|
|
432
|
+
* re-interpolated from the raw values. `findJsonStringEnd` walks the escape
|
|
433
|
+
* sequences `JSON.stringify` wrote so the slice lands on the real closing
|
|
434
|
+
* quote, not one inside an escaped character.
|
|
435
|
+
*/
|
|
436
|
+
function blockImportLine(apiId, path) {
|
|
437
|
+
const tuple = JSON.stringify([apiId, path]);
|
|
438
|
+
const keyEnd = findJsonStringEnd(tuple, 1);
|
|
439
|
+
return ` ${tuple.slice(1, keyEnd + 1)}: () => import(${tuple.slice(keyEnd + 2, -1)})`;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* The index of the closing `"` of the JSON string literal that starts at
|
|
443
|
+
* `text[start]` (itself a `"`), skipping over `\"`, `\\` and every other
|
|
444
|
+
* backslash escape `JSON.stringify` may have written.
|
|
445
|
+
*/
|
|
446
|
+
function findJsonStringEnd(text, start) {
|
|
447
|
+
let i = start + 1;
|
|
448
|
+
while (text[i] !== "\"") i += text[i] === "\\" ? 2 : 1;
|
|
449
|
+
return i;
|
|
450
|
+
}
|
|
451
|
+
//#endregion
|
|
452
|
+
export { eldraTheme as default, generateBlockTypes, seedLayout, validatePageSeeds, validateTemplateRoles, validateTemplateSeeds };
|