-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.tsx
164 lines (138 loc) · 4.82 KB
/
main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable import/first */
import './globals.css';
import { render, renderPlainText } from 'jsx-email';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { createBrowserRouter, type RouteObject, RouterProvider } from 'react-router-dom';
import { create } from 'superstruct';
import titleize from 'titleize';
import { Error } from './error.tsx';
import { Home } from './home.tsx';
import { Preview } from './preview.tsx';
import type { TemplatePart, TemplateData, TemplateExports } from './types.ts';
const { warn } = console;
const addSpacesForCamelCaseName = (str: string) => str.replace(/([a-z])([A-Z])/g, '$1 $2');
// Note: Disables annoying key errors. We're static so we don't need to worry about this.
/* eslint-disable no-console */
const og = console.error;
const re =
/^Warning: Each child in an array or iterator should have a unique "key" prop|^Warning: Each child in a list should have a unique "key" prop/;
console.error = (...args) => {
const [line] = args;
if (!re.test(line)) og(...args);
};
const Layout = ({ children }: { children: React.ReactNode }) => (
<div className="bg-dark-bg text-dark-bg-text">
<div>{children}</div>
</div>
);
const parseName = (path: string) => {
const chunks = path.replace('\\', '/').split('/');
const segment = chunks.at(-1);
const [basename] = segment!.split(/\.[^.]+$/);
return titleize(addSpacesForCamelCaseName(basename));
};
// @ts-expect-error
const relativePath = `${__JSX_EMAIL_RELATIVE_PATH__}/`;
const modules = import.meta.glob('@/**/*.{jsx,tsx}', { eager: true });
const sources = import.meta.glob('@/**/*.{jsx,tsx}', { as: 'raw', eager: true });
const pathLookup = Object.keys(modules).reduce((acc, path) => {
acc[path] = path.replace(relativePath, '');
return acc;
}, {});
const sortedModules = Object.keys(modules)
.sort((a, b) => {
const aa = a.split('/').length;
const bb = b.split('/').length;
if (aa > bb) return -1;
if (aa === bb) return 0;
if (aa < bb) return 1;
return 0;
})
.reduce((acc, curr) => {
return { ...acc, [curr]: modules[curr] };
}, {});
const templates = (
await Promise.all(
Object.entries(sortedModules).map<Promise<TemplateData>>(async ([path, mod]) => {
const component = mod as TemplateExports;
const Template = component.Template || (component as any).default;
if (!Template) return null;
const result: TemplateData = {
jsx: sources[path],
Name: component.Name || parseName(path),
path: pathLookup[path],
PreviewProps: component.PreviewProps || Template.PreviewProps,
Template,
TemplateStruct: component.TemplateStruct
};
return result;
})
)
).filter(Boolean);
const getPathParts = (path) => path.split('/');
const nestedTemplateParts: TemplatePart[] = templates.reduce((acc, template) => {
const parts = getPathParts(template.path);
let curr: TemplatePart = { children: acc, name: '' };
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
// Check if child with name exists
let child = curr.children?.find((c) => c.name === parseName(part));
if (!child) {
// If not, create it
child = { children: [], name: parseName(part), path: template.path.replace('.tsx', '') };
curr.children.push(child);
}
// Descend into child
curr = child;
}
// Add template data to leaf node
curr.template = template;
return acc;
}, []);
const templateRoutes = templates.map(async (template) => {
const { Name, PreviewProps, Template, TemplateStruct } = template;
let props: any;
if (TemplateStruct) props = create({}, TemplateStruct);
else if (typeof PreviewProps === 'function') props = PreviewProps();
else if (PreviewProps) props = PreviewProps;
else if ((Template as any).PreviewProps) {
warn(
`jsx-email: ${Name} → PreviewProps as a property of a component is deprecated. Please used a named export.`
);
props = (Template as any).PreviewProps;
}
const html = await render(<Template {...props} />, { pretty: true });
const plainText = await renderPlainText(<Template {...props} />);
const element = (
<Layout>
<Preview
{...{
html,
jsx: template.jsx,
plainText,
templateParts: nestedTemplateParts,
title: template.Name || template.path.replace('.tsx', '')
}}
/>
</Layout>
);
return { element, path: `/${template.path.replace('.tsx', '')}` } as RouteObject;
});
const router = createBrowserRouter([
{
element: (
<Layout>
<Home templateParts={nestedTemplateParts} />
</Layout>
),
errorElement: <Error />,
path: '/'
},
...(await Promise.all(templateRoutes))
]);
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);