@design-embed/plugin-figma-html 0.1.0 → 1.0.1
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 +1 -1
- package/dist/compilers/compilerUtils.d.mts +11 -0
- package/dist/compilers/compilerUtils.mjs +119 -0
- package/dist/compilers/htmlCompiler.d.mts +6 -0
- package/dist/compilers/htmlCompiler.mjs +31 -0
- package/dist/compilers/index.d.mts +11 -0
- package/dist/compilers/index.mjs +17 -0
- package/dist/compilers/index.test.d.mts +1 -0
- package/dist/compilers/index.test.mjs +45 -0
- package/dist/compilers/reactCompiler.d.mts +6 -0
- package/dist/compilers/reactCompiler.mjs +47 -0
- package/dist/compilers/vanjsCompiler.d.mts +6 -0
- package/dist/compilers/vanjsCompiler.mjs +47 -0
- package/dist/design-embed/src/core/diagnostics/diagnostic.d.mts +16 -0
- package/dist/design-embed/src/core/nodes.d.mts +14 -0
- package/dist/design-embed/src/core/plugins/pluginApi.d.mts +34 -0
- package/dist/external/figmaApi.d.mts +17 -0
- package/dist/external/figmaApi.mjs +55 -0
- package/dist/external/figmaApi.test.d.mts +1 -0
- package/dist/external/figmaApi.test.mjs +101 -0
- package/dist/external/imageDownloader.d.mts +17 -0
- package/dist/external/imageDownloader.mjs +66 -0
- package/dist/external/imageDownloader.test.d.mts +1 -0
- package/dist/external/imageDownloader.test.mjs +42 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +7 -0
- package/dist/plugin.d.mts +16 -0
- package/dist/plugin.mjs +43 -0
- package/dist/types.d.mts +84 -0
- package/package.json +12 -10
- package/src/plugin.ts +2 -3
- package/dist/compilers/compilerUtils.js +0 -182
- package/dist/compilers/htmlCompiler.js +0 -35
- package/dist/compilers/index.js +0 -17
- package/dist/compilers/reactCompiler.js +0 -58
- package/dist/compilers/vanjsCompiler.js +0 -55
- package/dist/external/figmaApi.js +0 -74
- package/dist/external/imageDownloader.js +0 -82
- package/dist/index.js +0 -3
- package/dist/plugin.js +0 -56
- package/node_modules/@design-embed/config/README.md +0 -5
- package/node_modules/@design-embed/config/dist/index.js +0 -283
- package/node_modules/@design-embed/config/package.json +0 -19
- package/node_modules/@design-embed/config/src/index.ts +0 -518
- package/node_modules/@design-embed/core/README.md +0 -5
- package/node_modules/@design-embed/core/dist/diagnostics/diagnostic.js +0 -3
- package/node_modules/@design-embed/core/dist/diagnostics/jsonDiagnostic.js +0 -35
- package/node_modules/@design-embed/core/dist/index.js +0 -351
- package/node_modules/@design-embed/core/dist/pipeline/checkMode.js +0 -29
- package/node_modules/@design-embed/core/dist/plugins/pluginApi.js +0 -1
- package/node_modules/@design-embed/core/dist/plugins/pluginRegistry.js +0 -25
- package/node_modules/@design-embed/core/package.json +0 -19
- package/node_modules/@design-embed/core/src/diagnostics/diagnostic.ts +0 -18
- package/node_modules/@design-embed/core/src/diagnostics/jsonDiagnostic.ts +0 -51
- package/node_modules/@design-embed/core/src/index.ts +0 -591
- package/node_modules/@design-embed/core/src/pipeline/checkMode.ts +0 -46
- package/node_modules/@design-embed/core/src/plugins/pluginApi.ts +0 -78
- package/node_modules/@design-embed/core/src/plugins/pluginRegistry.ts +0 -37
- /package/dist/{types.js → types.mjs} +0 -0
|
@@ -1,591 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ComponentMapping,
|
|
3
|
-
DesignEmbedConfig,
|
|
4
|
-
} from "../../config/src/index.ts";
|
|
5
|
-
import type { Diagnostic } from "./diagnostics/diagnostic.ts";
|
|
6
|
-
import type { TargetEmitter, TransformerPlugin } from "./plugins/pluginApi.ts";
|
|
7
|
-
import { sortTransformers } from "./plugins/pluginRegistry.ts";
|
|
8
|
-
|
|
9
|
-
export type {
|
|
10
|
-
Diagnostic,
|
|
11
|
-
DiagnosticSeverity,
|
|
12
|
-
} from "./diagnostics/diagnostic.ts";
|
|
13
|
-
export type { JsonDiagnostic } from "./diagnostics/jsonDiagnostic.ts";
|
|
14
|
-
export {
|
|
15
|
-
formatDiagnosticText,
|
|
16
|
-
toJsonDiagnostic,
|
|
17
|
-
toJsonDiagnostics,
|
|
18
|
-
} from "./diagnostics/jsonDiagnostic.ts";
|
|
19
|
-
export type {
|
|
20
|
-
CheckModeInput,
|
|
21
|
-
CheckModeResult,
|
|
22
|
-
} from "./pipeline/checkMode.ts";
|
|
23
|
-
export { checkGeneratedFiles } from "./pipeline/checkMode.ts";
|
|
24
|
-
export type {
|
|
25
|
-
GeneratedAsset,
|
|
26
|
-
SourcePlugin,
|
|
27
|
-
SourcePluginInput,
|
|
28
|
-
SourcePluginResult,
|
|
29
|
-
TargetEmitInput,
|
|
30
|
-
TargetEmitResult,
|
|
31
|
-
TargetEmitter,
|
|
32
|
-
TargetTestGenerateInput,
|
|
33
|
-
TargetTestGenerateResult,
|
|
34
|
-
TargetTestGenerator,
|
|
35
|
-
TransformContext,
|
|
36
|
-
TransformerPlugin,
|
|
37
|
-
TransformResult,
|
|
38
|
-
} from "./plugins/pluginApi.ts";
|
|
39
|
-
export {
|
|
40
|
-
PluginRegistry,
|
|
41
|
-
sortTransformers,
|
|
42
|
-
} from "./plugins/pluginRegistry.ts";
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Represents a file generated by the compiler.
|
|
46
|
-
*/
|
|
47
|
-
export interface GeneratedFile {
|
|
48
|
-
/** Relative path from the output directory. */
|
|
49
|
-
path: string;
|
|
50
|
-
/** File content. */
|
|
51
|
-
contents: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Location in the source HTML file.
|
|
56
|
-
*/
|
|
57
|
-
export interface SourceLocation {
|
|
58
|
-
/** Absolute offset in characters. */
|
|
59
|
-
offset: number;
|
|
60
|
-
/** 1-based line number. */
|
|
61
|
-
line: number;
|
|
62
|
-
/** 1-based column number. */
|
|
63
|
-
column: number;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* A normalized node in the design AST.
|
|
68
|
-
*/
|
|
69
|
-
export interface DesignNode {
|
|
70
|
-
/** The type of node. */
|
|
71
|
-
kind: "element" | "text" | "component";
|
|
72
|
-
/** HTML tag name (for element kind). */
|
|
73
|
-
tagName?: string;
|
|
74
|
-
/** HTML attributes (for element kind). */
|
|
75
|
-
attributes?: Record<string, string>;
|
|
76
|
-
/** Parsed inline styles (for element kind). */
|
|
77
|
-
styles?: Record<string, string>;
|
|
78
|
-
/** Utility classes to apply. */
|
|
79
|
-
generatedClassNames?: string[];
|
|
80
|
-
/** Child nodes. */
|
|
81
|
-
children?: DesignNode[];
|
|
82
|
-
/** Inner text content (for text kind). */
|
|
83
|
-
text?: string;
|
|
84
|
-
/** Original location in the source HTML. */
|
|
85
|
-
source?: SourceLocation;
|
|
86
|
-
/** Component name (for component kind). */
|
|
87
|
-
component?: string;
|
|
88
|
-
/** Named export of the component. */
|
|
89
|
-
importName?: string;
|
|
90
|
-
/** Mapped prop values for the component. */
|
|
91
|
-
props?: Record<string, PropValue>;
|
|
92
|
-
/** Import path of the component. */
|
|
93
|
-
importPath?: string;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* A value passed to a component prop.
|
|
98
|
-
*/
|
|
99
|
-
export type PropValue =
|
|
100
|
-
| { kind: "literal"; value: string | number | boolean }
|
|
101
|
-
| { kind: "text"; value: string }
|
|
102
|
-
| { kind: "children"; value: DesignNode[] };
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* A parsed CSS selector.
|
|
106
|
-
*/
|
|
107
|
-
export interface ParsedSelector {
|
|
108
|
-
/** Optional tag name. */
|
|
109
|
-
tagName?: string;
|
|
110
|
-
/** Optional ID selector. */
|
|
111
|
-
id?: string;
|
|
112
|
-
/** List of class names. */
|
|
113
|
-
classes: string[];
|
|
114
|
-
/** Attribute selectors. */
|
|
115
|
-
attributes: Record<string, string>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Input for the core embed function.
|
|
120
|
-
*/
|
|
121
|
-
export interface DesignEmbedInput {
|
|
122
|
-
/** The source design HTML. */
|
|
123
|
-
html: string;
|
|
124
|
-
/** Optional external CSS. */
|
|
125
|
-
css?: string;
|
|
126
|
-
/** Optional path to the config file (for resolution). */
|
|
127
|
-
configPath?: string;
|
|
128
|
-
/** The compiler configuration. */
|
|
129
|
-
config?: DesignEmbedConfig;
|
|
130
|
-
/** Working directory. */
|
|
131
|
-
cwd?: string;
|
|
132
|
-
/** List of transformer plugins to apply. */
|
|
133
|
-
transformers?: TransformerPlugin[];
|
|
134
|
-
/** The target emitter to use. */
|
|
135
|
-
targetEmitter: TargetEmitter;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Result of the embedding process.
|
|
140
|
-
*/
|
|
141
|
-
export interface DesignEmbedResult {
|
|
142
|
-
/** Generated files. */
|
|
143
|
-
files: GeneratedFile[];
|
|
144
|
-
/** Diagnostics reported during compilation. */
|
|
145
|
-
diagnostics: Diagnostic[];
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* The main compiler entry point.
|
|
150
|
-
* Parses HTML, applies component mappings, runs transformers, and emits files.
|
|
151
|
-
*
|
|
152
|
-
* @param input - The compilation input.
|
|
153
|
-
* @returns A promise resolving to the compilation result.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* const result = await embed({
|
|
157
|
-
* html: '<div class="btn">Click me</div>',
|
|
158
|
-
* config: myConfig,
|
|
159
|
-
* targetEmitter: reactEmitter
|
|
160
|
-
* });
|
|
161
|
-
*/
|
|
162
|
-
export async function embed(
|
|
163
|
-
input: DesignEmbedInput,
|
|
164
|
-
): Promise<DesignEmbedResult> {
|
|
165
|
-
let ast = parseHtml(input.html);
|
|
166
|
-
const diagnostics = validateComponentMappings(input.config?.components ?? []);
|
|
167
|
-
|
|
168
|
-
if (diagnostics.some((d) => d.severity === "error")) {
|
|
169
|
-
return { files: [], diagnostics };
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (input.transformers && input.transformers.length > 0) {
|
|
173
|
-
ast = await applyTransformers(
|
|
174
|
-
ast,
|
|
175
|
-
input.config ?? {},
|
|
176
|
-
input.transformers,
|
|
177
|
-
diagnostics,
|
|
178
|
-
);
|
|
179
|
-
if (diagnostics.some((d) => d.severity === "error")) {
|
|
180
|
-
return { files: [], diagnostics };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const { files } = input.targetEmitter.emit({
|
|
185
|
-
nodes: ast,
|
|
186
|
-
css: input.css,
|
|
187
|
-
config: input.config,
|
|
188
|
-
diagnostics,
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
return { files, diagnostics };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function applyTransformers(
|
|
195
|
-
ast: DesignNode[],
|
|
196
|
-
config: DesignEmbedConfig,
|
|
197
|
-
transformers: TransformerPlugin[],
|
|
198
|
-
diagnostics: Diagnostic[],
|
|
199
|
-
): Promise<DesignNode[]> {
|
|
200
|
-
let nextAst = ast;
|
|
201
|
-
|
|
202
|
-
for (const transformer of sortTransformers(transformers)) {
|
|
203
|
-
try {
|
|
204
|
-
const result = await transformer.transform({
|
|
205
|
-
ast: nextAst,
|
|
206
|
-
config,
|
|
207
|
-
diagnostics,
|
|
208
|
-
});
|
|
209
|
-
if (result.diagnostics) {
|
|
210
|
-
diagnostics.push(...result.diagnostics);
|
|
211
|
-
}
|
|
212
|
-
if (result.ast) {
|
|
213
|
-
nextAst = result.ast;
|
|
214
|
-
}
|
|
215
|
-
} catch (error) {
|
|
216
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
217
|
-
diagnostics.push({
|
|
218
|
-
code: "TRANSFORMER_FAILED",
|
|
219
|
-
message: `Transformer ${transformer.name} failed: ${message}`,
|
|
220
|
-
severity: "error",
|
|
221
|
-
details: { transformer: transformer.name },
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return nextAst;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function applyComponentMappings(
|
|
230
|
-
nodes: DesignNode[],
|
|
231
|
-
mappings: ComponentMapping[],
|
|
232
|
-
diagnostics: Diagnostic[] = [],
|
|
233
|
-
): DesignNode[] {
|
|
234
|
-
const parsedMappings = mappings
|
|
235
|
-
.map((mapping, index) => ({
|
|
236
|
-
index,
|
|
237
|
-
mapping,
|
|
238
|
-
selector: parseSelector(mapping.selector),
|
|
239
|
-
}))
|
|
240
|
-
.filter(({ selector }) => selector !== undefined) as Array<{
|
|
241
|
-
index: number;
|
|
242
|
-
mapping: ComponentMapping;
|
|
243
|
-
selector: ParsedSelector;
|
|
244
|
-
}>;
|
|
245
|
-
|
|
246
|
-
return nodes.map((node) => {
|
|
247
|
-
if (node.kind !== "element") {
|
|
248
|
-
return node;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const match = parsedMappings.find(({ selector }) =>
|
|
252
|
-
matchesSelector(node, selector),
|
|
253
|
-
);
|
|
254
|
-
if (match) {
|
|
255
|
-
const props = extractProps(node, match.mapping, diagnostics);
|
|
256
|
-
return {
|
|
257
|
-
kind: "component",
|
|
258
|
-
component: match.mapping.importName ?? inferImportName(match.mapping),
|
|
259
|
-
importName: match.mapping.importName ?? inferImportName(match.mapping),
|
|
260
|
-
importPath: match.mapping.component,
|
|
261
|
-
props,
|
|
262
|
-
children:
|
|
263
|
-
props.children?.kind === "children"
|
|
264
|
-
? undefined
|
|
265
|
-
: applyComponentMappings(
|
|
266
|
-
node.children ?? [],
|
|
267
|
-
mappings,
|
|
268
|
-
diagnostics,
|
|
269
|
-
),
|
|
270
|
-
source: node.source,
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
...node,
|
|
276
|
-
children: applyComponentMappings(
|
|
277
|
-
node.children ?? [],
|
|
278
|
-
mappings,
|
|
279
|
-
diagnostics,
|
|
280
|
-
),
|
|
281
|
-
};
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export function parseSelector(selector: string): ParsedSelector | undefined {
|
|
286
|
-
const trimmed = selector.trim();
|
|
287
|
-
if (!trimmed || /[\s>+~,:]/.test(trimmed)) {
|
|
288
|
-
return undefined;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const parsed: ParsedSelector = { classes: [], attributes: {} };
|
|
292
|
-
let rest = trimmed;
|
|
293
|
-
const tagMatch = rest.match(/^[a-zA-Z][a-zA-Z0-9-]*/);
|
|
294
|
-
if (tagMatch?.[0]) {
|
|
295
|
-
parsed.tagName = tagMatch[0].toLowerCase();
|
|
296
|
-
rest = rest.slice(tagMatch[0].length);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
while (rest) {
|
|
300
|
-
if (rest.startsWith(".")) {
|
|
301
|
-
const match = rest.match(/^\.([a-zA-Z_][a-zA-Z0-9_-]*)/);
|
|
302
|
-
if (!match?.[1]) {
|
|
303
|
-
return undefined;
|
|
304
|
-
}
|
|
305
|
-
parsed.classes.push(match[1]);
|
|
306
|
-
rest = rest.slice(match[0].length);
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (rest.startsWith("#")) {
|
|
311
|
-
const match = rest.match(/^#([a-zA-Z_][a-zA-Z0-9_-]*)/);
|
|
312
|
-
if (!match?.[1] || parsed.id) {
|
|
313
|
-
return undefined;
|
|
314
|
-
}
|
|
315
|
-
parsed.id = match[1];
|
|
316
|
-
rest = rest.slice(match[0].length);
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (rest.startsWith("[")) {
|
|
321
|
-
const match = rest.match(
|
|
322
|
-
/^\[([a-zA-Z_][a-zA-Z0-9_.:-]*)(?:=(?:"([^"]*)"|'([^']*)'|([^\]]+)))?\]/,
|
|
323
|
-
);
|
|
324
|
-
if (!match?.[1]) {
|
|
325
|
-
return undefined;
|
|
326
|
-
}
|
|
327
|
-
parsed.attributes[match[1]] = match[2] ?? match[3] ?? match[4] ?? "";
|
|
328
|
-
rest = rest.slice(match[0].length);
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return undefined;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return parsed;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
export function matchesSelector(
|
|
339
|
-
node: DesignNode,
|
|
340
|
-
selector: ParsedSelector,
|
|
341
|
-
): boolean {
|
|
342
|
-
if (node.kind !== "element") {
|
|
343
|
-
return false;
|
|
344
|
-
}
|
|
345
|
-
const attributes = node.attributes ?? {};
|
|
346
|
-
if (selector.tagName && node.tagName !== selector.tagName) {
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
if (selector.id && attributes.id !== selector.id) {
|
|
350
|
-
return false;
|
|
351
|
-
}
|
|
352
|
-
const classNames = new Set(
|
|
353
|
-
(attributes.class ?? "").split(/\s+/).filter(Boolean),
|
|
354
|
-
);
|
|
355
|
-
for (const className of selector.classes) {
|
|
356
|
-
if (!classNames.has(className)) {
|
|
357
|
-
return false;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
for (const [name, value] of Object.entries(selector.attributes)) {
|
|
361
|
-
if (!(name in attributes)) {
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
if (value !== "" && attributes[name] !== value) {
|
|
365
|
-
return false;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
return true;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
export function parseHtml(html: string): DesignNode[] {
|
|
373
|
-
const root: DesignNode = {
|
|
374
|
-
kind: "element",
|
|
375
|
-
tagName: "root",
|
|
376
|
-
attributes: {},
|
|
377
|
-
styles: {},
|
|
378
|
-
children: [],
|
|
379
|
-
};
|
|
380
|
-
const stack = [root];
|
|
381
|
-
const tokens = html.matchAll(/<!--[\s\S]*?-->|<\/?[a-zA-Z][^>]*>|[^<]+/g);
|
|
382
|
-
|
|
383
|
-
for (const token of tokens) {
|
|
384
|
-
const value = token[0];
|
|
385
|
-
const offset = token.index;
|
|
386
|
-
const source = getSourceLocation(html, offset);
|
|
387
|
-
|
|
388
|
-
if (value.startsWith("<!--")) {
|
|
389
|
-
continue;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (value.startsWith("</")) {
|
|
393
|
-
const tagName = value.slice(2, -1).trim().toLowerCase();
|
|
394
|
-
while (stack.length > 1) {
|
|
395
|
-
const node = stack.pop();
|
|
396
|
-
if (node?.tagName === tagName) {
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
if (value.startsWith("<")) {
|
|
404
|
-
const selfClosing = /\/>$/.test(value) || isVoidElement(value);
|
|
405
|
-
const node = parseElement(value, source);
|
|
406
|
-
currentParent(stack).children?.push(node);
|
|
407
|
-
if (!selfClosing) {
|
|
408
|
-
stack.push(node);
|
|
409
|
-
}
|
|
410
|
-
continue;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
if (value.trim()) {
|
|
414
|
-
currentParent(stack).children?.push({
|
|
415
|
-
kind: "text",
|
|
416
|
-
text: collapseWhitespace(value),
|
|
417
|
-
source,
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return root.children ?? [];
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
export function parseInlineStyle(
|
|
426
|
-
style: string | undefined,
|
|
427
|
-
): Record<string, string> {
|
|
428
|
-
const styles: Record<string, string> = {};
|
|
429
|
-
if (!style) {
|
|
430
|
-
return styles;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
for (const declaration of style.split(";")) {
|
|
434
|
-
const [property, ...valueParts] = declaration.split(":");
|
|
435
|
-
const value = valueParts.join(":").trim();
|
|
436
|
-
if (!property?.trim() || !value) {
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
styles[property.trim().toLowerCase()] = value;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
return styles;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function parseElement(rawTag: string, source: SourceLocation): DesignNode {
|
|
446
|
-
const tagBody = rawTag.replace(/^</, "").replace(/\/?>$/, "").trim();
|
|
447
|
-
const tagName = tagBody.split(/\s+/, 1)[0]?.toLowerCase() ?? "div";
|
|
448
|
-
const attributeSource = tagBody.slice(tagName.length).trim();
|
|
449
|
-
const attributes = parseAttributes(attributeSource);
|
|
450
|
-
const styles = parseInlineStyle(attributes.style);
|
|
451
|
-
|
|
452
|
-
return {
|
|
453
|
-
kind: "element",
|
|
454
|
-
tagName,
|
|
455
|
-
attributes,
|
|
456
|
-
styles,
|
|
457
|
-
children: [],
|
|
458
|
-
source,
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function parseAttributes(source: string): Record<string, string> {
|
|
463
|
-
const attributes: Record<string, string> = {};
|
|
464
|
-
const attributePattern =
|
|
465
|
-
/([:@a-zA-Z_][:@a-zA-Z0-9_.-]*)(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
466
|
-
|
|
467
|
-
for (const match of source.matchAll(attributePattern)) {
|
|
468
|
-
const name = match[1];
|
|
469
|
-
if (!name) {
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
attributes[name] = match[3] ?? match[4] ?? match[5] ?? "";
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return attributes;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
function validateComponentMappings(mappings: ComponentMapping[]): Diagnostic[] {
|
|
480
|
-
const diagnostics: Diagnostic[] = [];
|
|
481
|
-
for (const [index, mapping] of mappings.entries()) {
|
|
482
|
-
if (mapping.selector && !parseSelector(mapping.selector)) {
|
|
483
|
-
diagnostics.push({
|
|
484
|
-
code: "SELECTOR_UNSUPPORTED",
|
|
485
|
-
message: `Component mapping ${index} uses an unsupported selector: ${mapping.selector}`,
|
|
486
|
-
severity: "error",
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
for (const [propName, expression] of Object.entries(mapping.props ?? {})) {
|
|
491
|
-
if (!isSupportedPropExpression(expression)) {
|
|
492
|
-
diagnostics.push({
|
|
493
|
-
code: "PROP_EXPRESSION_UNSUPPORTED",
|
|
494
|
-
message: `Component mapping ${index} prop "${propName}" uses an unsupported expression: ${expression}`,
|
|
495
|
-
severity: "error",
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
return diagnostics;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
function isSupportedPropExpression(expression: string): boolean {
|
|
504
|
-
return (
|
|
505
|
-
!expression.startsWith("$") ||
|
|
506
|
-
expression === "$text" ||
|
|
507
|
-
expression === "$children" ||
|
|
508
|
-
/^\$attr\.[a-zA-Z_][a-zA-Z0-9_.:-]*$/.test(expression)
|
|
509
|
-
);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
function extractProps(
|
|
513
|
-
node: DesignNode,
|
|
514
|
-
mapping: ComponentMapping,
|
|
515
|
-
diagnostics: Diagnostic[],
|
|
516
|
-
): Record<string, PropValue> {
|
|
517
|
-
const props: Record<string, PropValue> = {};
|
|
518
|
-
for (const [propName, expression] of Object.entries(mapping.props ?? {})) {
|
|
519
|
-
if (expression === "$text") {
|
|
520
|
-
props[propName] = { kind: "text", value: collectText(node) };
|
|
521
|
-
continue;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
if (expression === "$children") {
|
|
525
|
-
props[propName] = { kind: "children", value: node.children ?? [] };
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (expression.startsWith("$attr.")) {
|
|
530
|
-
const attributeName = expression.slice("$attr.".length);
|
|
531
|
-
const value = node.attributes?.[attributeName];
|
|
532
|
-
if (value === undefined) {
|
|
533
|
-
diagnostics.push({
|
|
534
|
-
code: "PROP_ATTRIBUTE_MISSING",
|
|
535
|
-
message: `Attribute "${attributeName}" is missing for prop "${propName}".`,
|
|
536
|
-
severity: "warning",
|
|
537
|
-
});
|
|
538
|
-
continue;
|
|
539
|
-
}
|
|
540
|
-
props[propName] = { kind: "literal", value };
|
|
541
|
-
continue;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
props[propName] = { kind: "literal", value: expression };
|
|
545
|
-
}
|
|
546
|
-
return props;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
function collectText(node: DesignNode): string {
|
|
550
|
-
if (node.kind === "text") {
|
|
551
|
-
return node.text ?? "";
|
|
552
|
-
}
|
|
553
|
-
return (node.children ?? [])
|
|
554
|
-
.map((child) => collectText(child))
|
|
555
|
-
.filter(Boolean)
|
|
556
|
-
.join(" ")
|
|
557
|
-
.trim();
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
function inferImportName(mapping: ComponentMapping): string {
|
|
561
|
-
const lastSegment = mapping.component.split("/").filter(Boolean).at(-1);
|
|
562
|
-
return mapping.importName ?? lastSegment ?? "Component";
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
function currentParent(stack: DesignNode[]): DesignNode {
|
|
566
|
-
const parent = stack[stack.length - 1];
|
|
567
|
-
if (!parent) {
|
|
568
|
-
throw new Error("HTML parser stack is empty.");
|
|
569
|
-
}
|
|
570
|
-
return parent;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
function getSourceLocation(source: string, offset: number): SourceLocation {
|
|
574
|
-
const before = source.slice(0, offset);
|
|
575
|
-
const lines = before.split(/\r?\n/);
|
|
576
|
-
return {
|
|
577
|
-
offset,
|
|
578
|
-
line: lines.length,
|
|
579
|
-
column: (lines[lines.length - 1]?.length ?? 0) + 1,
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
function collapseWhitespace(value: string): string {
|
|
584
|
-
return value.replace(/\s+/g, " ").trim();
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
function isVoidElement(tag: string): boolean {
|
|
588
|
-
return /^<(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)(\s|>|\/)/i.test(
|
|
589
|
-
tag,
|
|
590
|
-
);
|
|
591
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
import type { Diagnostic } from "../diagnostics/diagnostic.ts";
|
|
3
|
-
import type { GeneratedFile } from "../index.ts";
|
|
4
|
-
|
|
5
|
-
export interface CheckModeInput {
|
|
6
|
-
files: GeneratedFile[];
|
|
7
|
-
cwd: string;
|
|
8
|
-
readFile(path: string): string | undefined;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface CheckModeResult {
|
|
12
|
-
ok: boolean;
|
|
13
|
-
diagnostics: Diagnostic[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function checkGeneratedFiles(input: CheckModeInput): CheckModeResult {
|
|
17
|
-
const diagnostics: Diagnostic[] = [];
|
|
18
|
-
|
|
19
|
-
for (const file of input.files) {
|
|
20
|
-
const absolutePath = resolve(input.cwd, file.path);
|
|
21
|
-
const current = input.readFile(absolutePath);
|
|
22
|
-
if (current === undefined) {
|
|
23
|
-
diagnostics.push({
|
|
24
|
-
code: "CHECK_FILE_MISSING",
|
|
25
|
-
message: `Generated file is missing: ${file.path}`,
|
|
26
|
-
severity: "error",
|
|
27
|
-
file: file.path,
|
|
28
|
-
});
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (current !== file.contents) {
|
|
33
|
-
diagnostics.push({
|
|
34
|
-
code: "CHECK_FILE_STALE",
|
|
35
|
-
message: `Generated file is stale: ${file.path}`,
|
|
36
|
-
severity: "error",
|
|
37
|
-
file: file.path,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
ok: diagnostics.length === 0,
|
|
44
|
-
diagnostics,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { DesignEmbedConfig } from "../../../config/src/index.ts";
|
|
2
|
-
import type { Diagnostic } from "../diagnostics/diagnostic.ts";
|
|
3
|
-
import type { DesignNode, GeneratedFile } from "../index.ts";
|
|
4
|
-
|
|
5
|
-
export interface GeneratedAsset {
|
|
6
|
-
path: string;
|
|
7
|
-
contents?: string | Uint8Array;
|
|
8
|
-
sourceUrl?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface SourcePlugin {
|
|
12
|
-
name: string;
|
|
13
|
-
run(input: SourcePluginInput): Promise<SourcePluginResult>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface SourcePluginInput {
|
|
17
|
-
cwd: string;
|
|
18
|
-
args: Record<string, string | boolean>;
|
|
19
|
-
config?: unknown;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface SourcePluginResult {
|
|
23
|
-
html?: string;
|
|
24
|
-
css?: string;
|
|
25
|
-
assets?: GeneratedAsset[];
|
|
26
|
-
files?: GeneratedFile[];
|
|
27
|
-
diagnostics: Diagnostic[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface TransformerPlugin {
|
|
31
|
-
name: string;
|
|
32
|
-
order?: number;
|
|
33
|
-
transform(
|
|
34
|
-
context: TransformContext,
|
|
35
|
-
): Promise<TransformResult> | TransformResult;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface TransformContext {
|
|
39
|
-
ast: DesignNode[];
|
|
40
|
-
config: DesignEmbedConfig;
|
|
41
|
-
diagnostics: Diagnostic[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface TransformResult {
|
|
45
|
-
ast?: DesignNode[];
|
|
46
|
-
diagnostics?: Diagnostic[];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface TargetEmitInput {
|
|
50
|
-
nodes: DesignNode[];
|
|
51
|
-
css?: string;
|
|
52
|
-
config?: DesignEmbedConfig;
|
|
53
|
-
diagnostics: Diagnostic[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface TargetEmitResult {
|
|
57
|
-
files: GeneratedFile[];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface TargetEmitter {
|
|
61
|
-
emit(input: TargetEmitInput): TargetEmitResult;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface TargetTestGenerateInput {
|
|
65
|
-
html: string;
|
|
66
|
-
css?: string;
|
|
67
|
-
config: DesignEmbedConfig;
|
|
68
|
-
diagnostics: Diagnostic[];
|
|
69
|
-
generatedFiles?: GeneratedFile[];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export interface TargetTestGenerateResult {
|
|
73
|
-
files: GeneratedFile[];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface TargetTestGenerator {
|
|
77
|
-
generateTests(input: TargetTestGenerateInput): TargetTestGenerateResult;
|
|
78
|
-
}
|