@@ -3,17 +3,23 @@ import path from "node:path";
3
3
import { traverse } from "@babel/core" ;
4
4
import { parse } from "@babel/parser" ;
5
5
import { z } from "zod" ;
6
+ import { render } from "@react-email/components" ;
6
7
import type { Category , Component } from "../../../components/structure" ;
7
8
import {
8
9
getComponentPathFromSlug ,
9
10
pathToComponents ,
10
11
} from "../../../components/structure" ;
12
+ import { Layout } from "../../../components/_components/layout" ;
11
13
12
- export type CodeVariant = "tailwind" | "inline-styles" ;
14
+ /**
15
+ * Tailwind and Inline Styles are both with React, but the React
16
+ * option is meant for where Tailwind nor Inline Styles are used
17
+ * at all in the markup.
18
+ */
19
+ export type CodeVariant = "tailwind" | "inline-styles" | "react" | "html" ;
13
20
14
21
export interface ImportedComponent extends Component {
15
- element : React . ReactElement ;
16
- code : Partial < Record < CodeVariant , string > > | string ;
22
+ code : Partial < Record < CodeVariant , string > > & { html : string } ;
17
23
}
18
24
19
25
const ComponentModule = z . object ( {
@@ -77,16 +83,22 @@ export const getImportedComponent = async (
77
83
78
84
if ( variantFilenames . length === 1 && variantFilenames [ 0 ] === "index.tsx" ) {
79
85
const filePath = path . join ( dirpath , "index.tsx" ) ;
86
+ const element = < Layout > { await getComponentElement ( filePath ) } </ Layout > ;
87
+ const html = await render ( element , {
88
+ pretty : true ,
89
+ } ) ;
80
90
const fileContent = await fs . readFile ( filePath , "utf8" ) ;
81
91
const code = getComponentCodeFrom ( fileContent ) ;
82
92
return {
83
93
...component ,
84
- element : await getComponentElement ( filePath ) ,
85
- code,
94
+ code : {
95
+ react : code ,
96
+ html,
97
+ } ,
86
98
} ;
87
99
}
88
100
89
- const codePerVariant : Partial < Record < CodeVariant , string > > = { } ;
101
+ const codePerVariant : ImportedComponent [ "code" ] = { html : "" } ;
90
102
91
103
const elements = await Promise . all (
92
104
variantFilenames . map ( async ( variantFilename ) => {
@@ -107,9 +119,14 @@ export const getImportedComponent = async (
107
119
codePerVariant [ variantKey ] = getComponentCodeFrom ( fileContents [ index ] ) ;
108
120
} ) ;
109
121
122
+ const element = < Layout > { elements [ 0 ] } </ Layout > ;
123
+
124
+ codePerVariant . html = await render ( element , {
125
+ pretty : true ,
126
+ } ) ;
127
+
110
128
return {
111
129
...component ,
112
- element : elements [ 0 ] ,
113
130
code : codePerVariant ,
114
131
} ;
115
132
} ;
0 commit comments