@design-embed/target-react 0.1.0 → 1.0.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.
Files changed (31) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +66 -1
  3. package/dist/design-embed/src/core/diagnostics/diagnostic.d.mts +16 -0
  4. package/dist/design-embed/src/core/nodes.d.mts +63 -0
  5. package/dist/design-embed/src/core/plugins/pluginApi.d.mts +41 -0
  6. package/dist/design-embed/src/core/types.d.mts +98 -0
  7. package/dist/index.d.mts +20 -0
  8. package/dist/index.mjs +964 -0
  9. package/dist/index.test.d.mts +1 -0
  10. package/dist/index.test.mjs +75 -0
  11. package/package.json +11 -9
  12. package/src/index.ts +699 -64
  13. package/dist/index.js +0 -744
  14. package/node_modules/@design-embed/config/README.md +0 -5
  15. package/node_modules/@design-embed/config/dist/index.js +0 -283
  16. package/node_modules/@design-embed/config/package.json +0 -19
  17. package/node_modules/@design-embed/config/src/index.ts +0 -518
  18. package/node_modules/@design-embed/core/README.md +0 -5
  19. package/node_modules/@design-embed/core/dist/diagnostics/diagnostic.js +0 -3
  20. package/node_modules/@design-embed/core/dist/diagnostics/jsonDiagnostic.js +0 -35
  21. package/node_modules/@design-embed/core/dist/index.js +0 -351
  22. package/node_modules/@design-embed/core/dist/pipeline/checkMode.js +0 -29
  23. package/node_modules/@design-embed/core/dist/plugins/pluginApi.js +0 -1
  24. package/node_modules/@design-embed/core/dist/plugins/pluginRegistry.js +0 -25
  25. package/node_modules/@design-embed/core/package.json +0 -19
  26. package/node_modules/@design-embed/core/src/diagnostics/diagnostic.ts +0 -18
  27. package/node_modules/@design-embed/core/src/diagnostics/jsonDiagnostic.ts +0 -51
  28. package/node_modules/@design-embed/core/src/index.ts +0 -591
  29. package/node_modules/@design-embed/core/src/pipeline/checkMode.ts +0 -46
  30. package/node_modules/@design-embed/core/src/plugins/pluginApi.ts +0 -78
  31. package/node_modules/@design-embed/core/src/plugins/pluginRegistry.ts +0 -37
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Jin-Woo Lee
3
+ Copyright (c) 2026 Jin Woo Lee
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/README.md CHANGED
@@ -2,4 +2,69 @@
2
2
 
3
3
  React target support for design-embed.
4
4
 
5
- It provides React-oriented emission behavior for generated views. The target focuses on producing deterministic component output that can reference configured project components and style conventions, making design HTML embeddable in React codebases.
5
+ It provides React-oriented emission behavior for generated views. The target focuses on producing deterministic component output and visual regression tests, making design HTML embeddable in React codebases.
6
+
7
+ ## Usage
8
+
9
+ ```typescript
10
+ import { defineConfig } from "design-embed";
11
+ import { ReactTarget } from "@design-embed/target-react";
12
+
13
+ export default defineConfig({
14
+ output: {
15
+ target: new ReactTarget(),
16
+ viewName: "WelcomeHero",
17
+ viewsDir: "src/generated/views",
18
+ styleMode: "inline",
19
+ },
20
+ components: [
21
+ {
22
+ selector: "button[data-role='primary']",
23
+ component: "Button",
24
+ props: { variant: "primary", children: "$text" },
25
+ },
26
+ ],
27
+ });
28
+ ```
29
+
30
+ `ReactTarget` is a class — instantiate it with `new ReactTarget()` each time you configure an output target.
31
+
32
+ ## Generated output
33
+
34
+ For the config above, design-embed produces:
35
+
36
+ ```text
37
+ src/generated/views/
38
+ WelcomeHero.view.tsx ← main view, imports Button locally
39
+ Button.view.tsx ← Button component scaffold
40
+ ```
41
+
42
+ `WelcomeHero.view.tsx`:
43
+ ```tsx
44
+ import { Button } from "./Button.view";
45
+
46
+ export function WelcomeHero() {
47
+ return (
48
+ <section>
49
+ <Button variant="primary">Get Started</Button>
50
+ </section>
51
+ );
52
+ }
53
+ ```
54
+
55
+ Every component mapping produces a separate `{ComponentName}.view.tsx` file. The main view always imports from those local files.
56
+
57
+ ## Visual tests
58
+
59
+ When `generateTests: true` is passed to `embed()` (or via `generate-tests` CLI command), a Playwright component test is generated for each output file:
60
+
61
+ ```text
62
+ src/generated/views/tests/
63
+ WelcomeHero.reference.html ← source HTML snapshot
64
+ WelcomeHero.visual.spec.tsx ← full view screenshot + layout test
65
+ Button.visual.spec.tsx ← component-level test
66
+ ```
67
+
68
+ The **component spec** locates the matched HTML element in the reference snapshot using its CSS selector, screenshots it, then mounts the React component in isolation and compares. Re-running `embed` in CI updates the reference HTML — tests then fail for any component that no longer matches the updated design.
69
+
70
+ Requires `@playwright/experimental-ct-react` in the consuming project.
@@ -0,0 +1,16 @@
1
+ import { SourceLocation } from "../nodes.mjs";
2
+
3
+ //#region packages/design-embed/src/core/diagnostics/diagnostic.d.ts
4
+ type DiagnosticSeverity = "error" | "warning" | "info";
5
+ interface Diagnostic {
6
+ code: string;
7
+ message: string;
8
+ severity: DiagnosticSeverity;
9
+ file?: string;
10
+ source?: SourceLocation;
11
+ selector?: string;
12
+ property?: string;
13
+ details?: Record<string, unknown>;
14
+ }
15
+ //#endregion
16
+ export { Diagnostic };
@@ -0,0 +1,63 @@
1
+ //#region packages/design-embed/src/core/nodes.d.ts
2
+ /**
3
+ * Location in the source HTML file.
4
+ */
5
+ interface SourceLocation {
6
+ /** Absolute offset in characters. */
7
+ offset: number;
8
+ /** 1-based line number. */
9
+ line: number;
10
+ /** 1-based column number. */
11
+ column: number;
12
+ }
13
+ /**
14
+ * A normalized node in the design AST.
15
+ */
16
+ interface DesignNode {
17
+ /** The type of node. */
18
+ kind: "element" | "text" | "component";
19
+ /** HTML tag name (for element kind). */
20
+ tagName?: string;
21
+ /** HTML attributes (for element kind). */
22
+ attributes?: Record<string, string>;
23
+ /** Parsed inline styles (for element kind). */
24
+ styles?: Record<string, string>;
25
+ /** Utility classes to apply. */
26
+ generatedClassNames?: string[];
27
+ /** Child nodes. */
28
+ children?: DesignNode[];
29
+ /** Inner text content (for text kind). */
30
+ text?: string;
31
+ /** Original location in the source HTML. */
32
+ source?: SourceLocation;
33
+ /** Component name (for component kind). */
34
+ component?: string;
35
+ /** Named export of the component. */
36
+ importName?: string;
37
+ /** Mapped prop values for the component. */
38
+ props?: Record<string, PropValue>;
39
+ /** Import path of the component. */
40
+ importPath?: string;
41
+ /**
42
+ * The original element node a component was mapped from. Retained so
43
+ * targets can reconstruct the element's structure when emitting the
44
+ * component implementation.
45
+ */
46
+ sourceElement?: DesignNode;
47
+ }
48
+ /**
49
+ * A value passed to a component prop.
50
+ */
51
+ type PropValue = {
52
+ kind: "literal";
53
+ value: string | number | boolean; /** Source attribute name when the prop is bound to `$attr.*`. */
54
+ attribute?: string;
55
+ } | {
56
+ kind: "text";
57
+ value: string;
58
+ } | {
59
+ kind: "children";
60
+ value: DesignNode[];
61
+ };
62
+ //#endregion
63
+ export { DesignNode, SourceLocation };
@@ -0,0 +1,41 @@
1
+ import { Diagnostic } from "../diagnostics/diagnostic.mjs";
2
+
3
+ //#region packages/design-embed/src/core/plugins/pluginApi.d.ts
4
+ /**
5
+ * Represents a file generated by the compiler.
6
+ */
7
+ interface GeneratedFile {
8
+ /** Relative path from the output directory. */
9
+ path: string;
10
+ /** File content. */
11
+ contents: string;
12
+ }
13
+ interface GeneratedAsset {
14
+ path: string;
15
+ contents?: string | Uint8Array;
16
+ sourceUrl?: string;
17
+ }
18
+ interface SourcePlugin {
19
+ name: string;
20
+ run(input: SourcePluginInput): Promise<SourcePluginResult>;
21
+ }
22
+ interface SourcePluginInput {
23
+ cwd: string;
24
+ config?: unknown;
25
+ }
26
+ interface SourcePluginResult {
27
+ html?: string;
28
+ css?: string;
29
+ assets?: GeneratedAsset[];
30
+ files?: GeneratedFile[];
31
+ diagnostics: Diagnostic[];
32
+ }
33
+ interface TargetEmitResult {
34
+ files: GeneratedFile[];
35
+ }
36
+ interface TargetTestGenerateResult {
37
+ files: GeneratedFile[];
38
+ diagnostics: Diagnostic[];
39
+ }
40
+ //#endregion
41
+ export { SourcePlugin, TargetEmitResult, TargetTestGenerateResult };
@@ -0,0 +1,98 @@
1
+ import { DesignNode } from "./nodes.mjs";
2
+ import { SourcePlugin, TargetEmitResult, TargetTestGenerateResult } from "./plugins/pluginApi.mjs";
3
+ import { Diagnostic } from "./diagnostics/diagnostic.mjs";
4
+
5
+ //#region packages/design-embed/src/core/types.d.ts
6
+ interface TargetEmitInput {
7
+ nodes: DesignNode[];
8
+ css?: string;
9
+ config?: DesignEmbedConfig;
10
+ diagnostics: Diagnostic[];
11
+ }
12
+ interface TargetEmitter {
13
+ emit(input: TargetEmitInput): TargetEmitResult;
14
+ }
15
+ interface TargetTestGenerateInput {
16
+ html: string;
17
+ css?: string;
18
+ config: DesignEmbedConfig;
19
+ }
20
+ interface TargetTestGenerator {
21
+ generateTests(input: TargetTestGenerateInput): TargetTestGenerateResult;
22
+ }
23
+ type StyleMode = "inline" | "css-modules" | "tailwind";
24
+ interface ComponentMapping {
25
+ selector: string;
26
+ component: string;
27
+ props?: Record<string, string>;
28
+ }
29
+ interface TokenConfig {
30
+ spacing?: {
31
+ unit?: "px" | "rem";
32
+ threshold?: number;
33
+ values?: Record<string, number>;
34
+ };
35
+ sizing?: NumericTokenGroup;
36
+ typography?: NumericTokenGroup;
37
+ radius?: Record<string, number>;
38
+ borderWidth?: Record<string, number>;
39
+ shadow?: Record<string, string>;
40
+ colors?: Record<string, string>;
41
+ colorThreshold?: number;
42
+ }
43
+ interface NumericTokenGroup {
44
+ unit?: "px" | "rem";
45
+ threshold?: number;
46
+ values?: Record<string, number>;
47
+ }
48
+ type StyleMappings = Record<string, Record<string, string>>;
49
+ interface TestGenerationConfig {
50
+ outputDir?: string;
51
+ runner?: "playwright";
52
+ viewports?: TestViewport[];
53
+ states?: TestState[];
54
+ assertions?: TestAssertions;
55
+ }
56
+ interface TestViewport {
57
+ name?: string;
58
+ width: number;
59
+ height: number;
60
+ }
61
+ interface TestState {
62
+ name: string;
63
+ hover?: string;
64
+ focus?: string;
65
+ click?: string;
66
+ waitFor?: string;
67
+ }
68
+ interface TestAssertions {
69
+ screenshot?: boolean;
70
+ layout?: boolean;
71
+ layoutTolerance?: number;
72
+ selectors?: string[];
73
+ /**
74
+ * Per-pixel color sensitivity (0-1) for the screenshot comparison. Smaller
75
+ * is stricter. Defaults to 0.2.
76
+ */
77
+ screenshotThreshold?: number;
78
+ /**
79
+ * Maximum number of differing pixels tolerated in the screenshot
80
+ * comparison. Defaults to 0 (byte-exact).
81
+ */
82
+ screenshotMaxDiffPixels?: number;
83
+ }
84
+ interface DesignEmbedConfig {
85
+ output?: {
86
+ viewsDir?: string | URL;
87
+ target?: "html" | TargetEmitter;
88
+ viewName?: string;
89
+ styleMode?: StyleMode;
90
+ };
91
+ components?: ComponentMapping[];
92
+ tokens?: TokenConfig;
93
+ styleMappings?: StyleMappings;
94
+ source?: SourcePlugin;
95
+ tests?: TestGenerationConfig;
96
+ }
97
+ //#endregion
98
+ export { TargetEmitInput, TargetEmitter, TargetTestGenerateInput, TargetTestGenerator };
@@ -0,0 +1,20 @@
1
+ import { DesignNode } from "./design-embed/src/core/nodes.mjs";
2
+ import { TargetEmitResult, TargetTestGenerateResult } from "./design-embed/src/core/plugins/pluginApi.mjs";
3
+ import { TargetEmitInput, TargetEmitter, TargetTestGenerateInput, TargetTestGenerator } from "./design-embed/src/core/types.mjs";
4
+
5
+ //#region packages/target-react/src/index.d.ts
6
+ declare class ReactTarget implements TargetEmitter, TargetTestGenerator {
7
+ emit({
8
+ nodes,
9
+ css,
10
+ config,
11
+ diagnostics
12
+ }: TargetEmitInput): TargetEmitResult;
13
+ generateTests(input: TargetTestGenerateInput): TargetTestGenerateResult;
14
+ }
15
+ declare const reactTestGenerator: TargetTestGenerator;
16
+ declare function emitReactView(nodes: DesignNode[], viewName: string, options?: {
17
+ cssModulePath?: string;
18
+ }): string;
19
+ //#endregion
20
+ export { ReactTarget, emitReactView, reactTestGenerator };