create-visualbuild-app 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/README.md +919 -58
- package/docs/user-guide.md +759 -0
- package/package.json +92 -85
- package/scripts/create-builder-app.mjs +513 -501
- package/scripts/vb-dev.mjs +41 -32
- package/scripts/vb-generate.mjs +332 -224
- package/templates/default/app/.vercelignore +6 -0
- package/templates/default/app/README.md +56 -21
- package/templates/default/app/visualbuild/components.json +3 -0
- package/templates/default/app/visualbuild/config.d.ts +48 -0
- package/templates/default/app/visualbuild.config.ts +38 -0
- package/templates/default/builder/README.md +37 -21
- package/visual-app-builder/server/parseReactPage.ts +776 -571
- package/visual-app-builder/shared/createReactComponentSource.d.mts +2 -0
- package/visual-app-builder/shared/createReactComponentSource.mjs +45 -0
- package/visual-app-builder/shared/generateReactSource.d.mts +57 -37
- package/visual-app-builder/shared/generateReactSource.mjs +608 -443
- package/visual-app-builder/shared/npmBuildCommand.d.mts +17 -0
- package/visual-app-builder/shared/npmBuildCommand.mjs +26 -0
- package/visual-app-builder/shared/syncCustomComponentSource.d.mts +13 -0
- package/visual-app-builder/shared/syncCustomComponentSource.mjs +345 -0
- package/visual-app-builder/shared/vercelDeployment.d.mts +31 -0
- package/visual-app-builder/shared/vercelDeployment.mjs +266 -0
- package/visual-app-builder/shared/visualbuildConfig.d.mts +144 -0
- package/visual-app-builder/shared/visualbuildConfig.mjs +578 -0
- package/visual-app-builder/src/App.tsx +1090 -874
- package/visual-app-builder/src/components/Canvas.tsx +1116 -1059
- package/visual-app-builder/src/components/CodePanel.tsx +1147 -812
- package/visual-app-builder/src/components/ComponentSidebar.tsx +365 -302
- package/visual-app-builder/src/components/DeploymentModal.tsx +295 -0
- package/visual-app-builder/src/components/PageSwitcher.tsx +133 -133
- package/visual-app-builder/src/components/ProjectExplorer.tsx +1054 -1054
- package/visual-app-builder/src/components/PropertiesPanel.tsx +792 -692
- package/visual-app-builder/src/components/Topbar.tsx +257 -128
- package/visual-app-builder/src/data/componentRegistry.tsx +613 -292
- package/visual-app-builder/src/data/tailwindClassCatalog.ts +95 -0
- package/visual-app-builder/src/index.css +383 -111
- package/visual-app-builder/src/stores/useAppStore.ts +2385 -1265
- package/visual-app-builder/src/theme.ts +71 -0
- package/visual-app-builder/src/types/index.ts +350 -261
- package/visual-app-builder/src/utils/codegen.ts +90 -66
- package/visual-app-builder/src/utils/deployment.ts +54 -0
- package/visual-app-builder/src/utils/projectPersistence.ts +218 -177
- package/visual-app-builder/vite.config.ts +1946 -1479
|
@@ -1,443 +1,608 @@
|
|
|
1
|
-
import { generate } from '@babel/generator'
|
|
2
|
-
import * as t from '@babel/types'
|
|
3
|
-
|
|
4
|
-
const voidElements = new Set(['br', 'hr', 'img', 'input', 'source', 'col'])
|
|
5
|
-
|
|
6
|
-
export function generatePageSource(page) {
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
t.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
[
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export function
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
.
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
1
|
+
import { generate } from '@babel/generator'
|
|
2
|
+
import * as t from '@babel/types'
|
|
3
|
+
|
|
4
|
+
const voidElements = new Set(['br', 'hr', 'img', 'input', 'source', 'col'])
|
|
5
|
+
|
|
6
|
+
export function generatePageSource(page, customComponents = []) {
|
|
7
|
+
customComponents = Array.isArray(customComponents) ? customComponents : []
|
|
8
|
+
const root = {
|
|
9
|
+
...(page.root ?? {
|
|
10
|
+
id: `${page.id}:root`,
|
|
11
|
+
type: 'div',
|
|
12
|
+
props: { className: 'min-h-screen' },
|
|
13
|
+
children: [],
|
|
14
|
+
}),
|
|
15
|
+
children: page.components ?? [],
|
|
16
|
+
}
|
|
17
|
+
const declaration = t.functionDeclaration(
|
|
18
|
+
t.identifier(toPageComponentName(page.name)),
|
|
19
|
+
[],
|
|
20
|
+
t.blockStatement([
|
|
21
|
+
t.returnStatement(createJsxElement(root, 1, customComponents)),
|
|
22
|
+
])
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
return printProgram([
|
|
26
|
+
...createCustomComponentImports(
|
|
27
|
+
[root],
|
|
28
|
+
getPageSourcePath(page),
|
|
29
|
+
customComponents
|
|
30
|
+
),
|
|
31
|
+
t.exportDefaultDeclaration(declaration),
|
|
32
|
+
])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function generateAppSource(
|
|
36
|
+
pages,
|
|
37
|
+
appShell = { header: null, footer: null },
|
|
38
|
+
options = {}
|
|
39
|
+
) {
|
|
40
|
+
const appFile = options.appFile ?? 'src/App.tsx'
|
|
41
|
+
const layoutFile =
|
|
42
|
+
options.layoutFile ?? 'src/layouts/AppLayout.tsx'
|
|
43
|
+
const hasLayout =
|
|
44
|
+
options.emitLayout !== false &&
|
|
45
|
+
Boolean(appShell.header || appShell.footer)
|
|
46
|
+
const statements = pages.map((page) =>
|
|
47
|
+
t.importDeclaration(
|
|
48
|
+
[
|
|
49
|
+
t.importDefaultSpecifier(
|
|
50
|
+
t.identifier(toPageComponentName(page.name))
|
|
51
|
+
),
|
|
52
|
+
],
|
|
53
|
+
t.stringLiteral(toPageImportPath(page, appFile))
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if (hasLayout) {
|
|
58
|
+
statements.push(
|
|
59
|
+
t.importDeclaration(
|
|
60
|
+
[t.importDefaultSpecifier(t.identifier('AppLayout'))],
|
|
61
|
+
t.stringLiteral(toRelativeImportPath(appFile, layoutFile))
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
statements.push(
|
|
67
|
+
t.variableDeclaration('const', [
|
|
68
|
+
t.variableDeclarator(
|
|
69
|
+
t.identifier('routes'),
|
|
70
|
+
t.arrayExpression(
|
|
71
|
+
pages.map((page) =>
|
|
72
|
+
t.objectExpression([
|
|
73
|
+
t.objectProperty(
|
|
74
|
+
t.identifier('path'),
|
|
75
|
+
t.stringLiteral(page.route)
|
|
76
|
+
),
|
|
77
|
+
t.objectProperty(
|
|
78
|
+
t.identifier('component'),
|
|
79
|
+
t.identifier(toPageComponentName(page.name))
|
|
80
|
+
),
|
|
81
|
+
])
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
),
|
|
85
|
+
])
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const fallbackPage = t.identifier(
|
|
89
|
+
toPageComponentName(pages[0]?.name ?? 'Home')
|
|
90
|
+
)
|
|
91
|
+
const appBody = [
|
|
92
|
+
t.variableDeclaration('const', [
|
|
93
|
+
t.variableDeclarator(
|
|
94
|
+
t.identifier('path'),
|
|
95
|
+
t.memberExpression(
|
|
96
|
+
t.memberExpression(
|
|
97
|
+
t.identifier('window'),
|
|
98
|
+
t.identifier('location')
|
|
99
|
+
),
|
|
100
|
+
t.identifier('pathname')
|
|
101
|
+
)
|
|
102
|
+
),
|
|
103
|
+
]),
|
|
104
|
+
t.variableDeclaration('const', [
|
|
105
|
+
t.variableDeclarator(
|
|
106
|
+
t.identifier('match'),
|
|
107
|
+
t.callExpression(
|
|
108
|
+
t.memberExpression(
|
|
109
|
+
t.identifier('routes'),
|
|
110
|
+
t.identifier('find')
|
|
111
|
+
),
|
|
112
|
+
[
|
|
113
|
+
t.arrowFunctionExpression(
|
|
114
|
+
[t.identifier('route')],
|
|
115
|
+
t.binaryExpression(
|
|
116
|
+
'===',
|
|
117
|
+
t.memberExpression(
|
|
118
|
+
t.identifier('route'),
|
|
119
|
+
t.identifier('path')
|
|
120
|
+
),
|
|
121
|
+
t.identifier('path')
|
|
122
|
+
)
|
|
123
|
+
),
|
|
124
|
+
]
|
|
125
|
+
)
|
|
126
|
+
),
|
|
127
|
+
]),
|
|
128
|
+
t.variableDeclaration('const', [
|
|
129
|
+
t.variableDeclarator(
|
|
130
|
+
t.identifier('Page'),
|
|
131
|
+
t.logicalExpression(
|
|
132
|
+
'??',
|
|
133
|
+
t.optionalMemberExpression(
|
|
134
|
+
t.identifier('match'),
|
|
135
|
+
t.identifier('component'),
|
|
136
|
+
false,
|
|
137
|
+
true
|
|
138
|
+
),
|
|
139
|
+
fallbackPage
|
|
140
|
+
)
|
|
141
|
+
),
|
|
142
|
+
]),
|
|
143
|
+
t.returnStatement(
|
|
144
|
+
hasLayout
|
|
145
|
+
? t.jsxElement(
|
|
146
|
+
t.jsxOpeningElement(t.jsxIdentifier('AppLayout'), [], false),
|
|
147
|
+
t.jsxClosingElement(t.jsxIdentifier('AppLayout')),
|
|
148
|
+
withLineBreaks([
|
|
149
|
+
t.jsxElement(
|
|
150
|
+
t.jsxOpeningElement(t.jsxIdentifier('Page'), [], true),
|
|
151
|
+
null,
|
|
152
|
+
[],
|
|
153
|
+
true
|
|
154
|
+
),
|
|
155
|
+
], 1),
|
|
156
|
+
false
|
|
157
|
+
)
|
|
158
|
+
: t.jsxElement(
|
|
159
|
+
t.jsxOpeningElement(t.jsxIdentifier('Page'), [], true),
|
|
160
|
+
null,
|
|
161
|
+
[],
|
|
162
|
+
true
|
|
163
|
+
)
|
|
164
|
+
),
|
|
165
|
+
]
|
|
166
|
+
const appDeclaration = t.functionDeclaration(
|
|
167
|
+
t.identifier('App'),
|
|
168
|
+
[],
|
|
169
|
+
t.blockStatement(appBody)
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
statements.push(t.exportDefaultDeclaration(appDeclaration))
|
|
173
|
+
return printProgram(statements)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function generateLayoutSource(
|
|
177
|
+
appShell,
|
|
178
|
+
customComponents = [],
|
|
179
|
+
options = {}
|
|
180
|
+
) {
|
|
181
|
+
customComponents = Array.isArray(customComponents) ? customComponents : []
|
|
182
|
+
const reactImport = t.importDeclaration(
|
|
183
|
+
[
|
|
184
|
+
t.importSpecifier(
|
|
185
|
+
t.identifier('ReactNode'),
|
|
186
|
+
t.identifier('ReactNode')
|
|
187
|
+
),
|
|
188
|
+
],
|
|
189
|
+
t.stringLiteral('react')
|
|
190
|
+
)
|
|
191
|
+
reactImport.importKind = 'type'
|
|
192
|
+
|
|
193
|
+
const childrenProperty = t.objectProperty(
|
|
194
|
+
t.identifier('children'),
|
|
195
|
+
t.identifier('children'),
|
|
196
|
+
false,
|
|
197
|
+
true
|
|
198
|
+
)
|
|
199
|
+
const childrenPattern = t.objectPattern([childrenProperty])
|
|
200
|
+
const childrenType = t.tsPropertySignature(
|
|
201
|
+
t.identifier('children'),
|
|
202
|
+
t.tsTypeAnnotation(
|
|
203
|
+
t.tsTypeReference(t.identifier('ReactNode'))
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
childrenPattern.typeAnnotation = t.tsTypeAnnotation(
|
|
207
|
+
t.tsTypeLiteral([childrenType])
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
const wrapperChildren = []
|
|
211
|
+
|
|
212
|
+
if (appShell.header) {
|
|
213
|
+
wrapperChildren.push(createJsxElement(appShell.header, 2, customComponents))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
wrapperChildren.push(
|
|
217
|
+
t.jsxExpressionContainer(t.identifier('children'))
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
if (appShell.footer) {
|
|
221
|
+
wrapperChildren.push(createJsxElement(appShell.footer, 2, customComponents))
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const declaration = t.functionDeclaration(
|
|
225
|
+
t.identifier('AppLayout'),
|
|
226
|
+
[childrenPattern],
|
|
227
|
+
t.blockStatement([
|
|
228
|
+
t.returnStatement(
|
|
229
|
+
t.jsxElement(
|
|
230
|
+
t.jsxOpeningElement(t.jsxIdentifier('div'), [], false),
|
|
231
|
+
t.jsxClosingElement(t.jsxIdentifier('div')),
|
|
232
|
+
withLineBreaks(wrapperChildren, 1),
|
|
233
|
+
false
|
|
234
|
+
)
|
|
235
|
+
),
|
|
236
|
+
])
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
return printProgram([
|
|
240
|
+
reactImport,
|
|
241
|
+
...createCustomComponentImports(
|
|
242
|
+
[appShell.header, appShell.footer].filter(Boolean),
|
|
243
|
+
options.layoutFile ?? 'src/layouts/AppLayout.tsx',
|
|
244
|
+
customComponents
|
|
245
|
+
),
|
|
246
|
+
t.exportDefaultDeclaration(declaration),
|
|
247
|
+
])
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function generateNodeSource(node, indent = '') {
|
|
251
|
+
const source = generate(createJsxElement(node), generatorOptions).code
|
|
252
|
+
return source
|
|
253
|
+
.split('\n')
|
|
254
|
+
.map((line) => `${indent}${line}`)
|
|
255
|
+
.join('\n')
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function toPageComponentName(name) {
|
|
259
|
+
const pascalName = String(name)
|
|
260
|
+
.split(/[^a-zA-Z0-9]+/)
|
|
261
|
+
.filter(Boolean)
|
|
262
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
263
|
+
.join('')
|
|
264
|
+
|
|
265
|
+
return `${pascalName || 'Untitled'}Page`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function getPageSourcePath(page) {
|
|
269
|
+
return normalizeProjectPath(
|
|
270
|
+
page.sourcePath || `src/pages/${toPageComponentName(page.name)}.tsx`
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function createPageSourcePath(name, pagesDir = 'src/pages') {
|
|
275
|
+
return `${normalizeProjectPath(pagesDir)}/${toPageComponentName(name)}.tsx`
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function normalizeProjectPath(value) {
|
|
279
|
+
return String(value).replace(/\\/g, '/').replace(/^\.\/+/, '')
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function createJsxElement(
|
|
283
|
+
node,
|
|
284
|
+
depth = 0,
|
|
285
|
+
customComponents = [],
|
|
286
|
+
skipCustomWrapper = false
|
|
287
|
+
) {
|
|
288
|
+
const tagName = String(node.type)
|
|
289
|
+
const customDefinition = customComponents.find(
|
|
290
|
+
(component) => component.name === tagName
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
if (customDefinition && !skipCustomWrapper) {
|
|
294
|
+
return createWrappedCustomElement(node, depth, customComponents)
|
|
295
|
+
}
|
|
296
|
+
const selfClosing = voidElements.has(tagName) || tagName === 'textarea'
|
|
297
|
+
const opening = t.jsxOpeningElement(
|
|
298
|
+
t.jsxIdentifier(tagName),
|
|
299
|
+
createJsxAttributes(node, customComponents),
|
|
300
|
+
selfClosing
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
if (selfClosing) {
|
|
304
|
+
return t.jsxElement(opening, null, [], true)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const children = Array.isArray(node.children)
|
|
308
|
+
? node.children.map((child) =>
|
|
309
|
+
createJsxElement(child, depth + 1, customComponents)
|
|
310
|
+
)
|
|
311
|
+
: []
|
|
312
|
+
const text = String(node.props?.text ?? '')
|
|
313
|
+
|
|
314
|
+
if (children.length === 0 && text) {
|
|
315
|
+
children.push(t.jsxText(escapeJsxText(text)))
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return t.jsxElement(
|
|
319
|
+
opening,
|
|
320
|
+
t.jsxClosingElement(t.jsxIdentifier(tagName)),
|
|
321
|
+
children.length > 0 ? withLineBreaks(children, depth) : children,
|
|
322
|
+
false
|
|
323
|
+
)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function createWrappedCustomElement(node, depth, customComponents) {
|
|
327
|
+
const props = node.props ?? {}
|
|
328
|
+
const componentProps = Object.fromEntries(
|
|
329
|
+
Object.entries(props).filter(
|
|
330
|
+
([name]) =>
|
|
331
|
+
![
|
|
332
|
+
'wrapperTag',
|
|
333
|
+
'id',
|
|
334
|
+
'className',
|
|
335
|
+
'attributes',
|
|
336
|
+
'componentTree',
|
|
337
|
+
].includes(name)
|
|
338
|
+
)
|
|
339
|
+
)
|
|
340
|
+
const opening = t.jsxOpeningElement(
|
|
341
|
+
t.jsxIdentifier(String(node.type)),
|
|
342
|
+
createJsxAttributes(
|
|
343
|
+
{ ...node, props: componentProps },
|
|
344
|
+
customComponents
|
|
345
|
+
),
|
|
346
|
+
true
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
return t.jsxElement(opening, null, [], true)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function withLineBreaks(children, depth) {
|
|
353
|
+
const childIndent = ' '.repeat(depth + 1)
|
|
354
|
+
const closingIndent = ' '.repeat(depth)
|
|
355
|
+
|
|
356
|
+
return [
|
|
357
|
+
t.jsxText(`\n${childIndent}`),
|
|
358
|
+
...children.flatMap((child, index) => [
|
|
359
|
+
child,
|
|
360
|
+
t.jsxText(
|
|
361
|
+
index === children.length - 1
|
|
362
|
+
? `\n${closingIndent}`
|
|
363
|
+
: `\n${childIndent}`
|
|
364
|
+
),
|
|
365
|
+
]),
|
|
366
|
+
]
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function createJsxAttributes(node, customComponents = []) {
|
|
370
|
+
const props = node.props ?? {}
|
|
371
|
+
const isCustom = customComponents.some(
|
|
372
|
+
(component) => component.name === node.type
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
if (isCustom) {
|
|
376
|
+
const additionalAttributes =
|
|
377
|
+
props.attributes &&
|
|
378
|
+
typeof props.attributes === 'object' &&
|
|
379
|
+
!Array.isArray(props.attributes)
|
|
380
|
+
? Object.entries(props.attributes)
|
|
381
|
+
: []
|
|
382
|
+
|
|
383
|
+
return [
|
|
384
|
+
...Object.entries(props).filter(
|
|
385
|
+
([name, value]) =>
|
|
386
|
+
name !== 'attributes' && value !== undefined && value !== ''
|
|
387
|
+
),
|
|
388
|
+
...additionalAttributes.filter(
|
|
389
|
+
([name, value]) =>
|
|
390
|
+
isSafeJsxAttributeName(name) &&
|
|
391
|
+
name !== 'children' &&
|
|
392
|
+
name !== 'key' &&
|
|
393
|
+
name !== 'ref' &&
|
|
394
|
+
!Object.prototype.hasOwnProperty.call(props, name) &&
|
|
395
|
+
isSerializableAttributeValue(value)
|
|
396
|
+
),
|
|
397
|
+
]
|
|
398
|
+
.map(([name, value]) => createValueAttribute(name, value))
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const attributes = [
|
|
402
|
+
props.id ? createValueAttribute('id', String(props.id)) : null,
|
|
403
|
+
props.className
|
|
404
|
+
? createValueAttribute('className', String(props.className))
|
|
405
|
+
: null,
|
|
406
|
+
node.type === 'a'
|
|
407
|
+
? createValueAttribute('href', String(props.href ?? '#'))
|
|
408
|
+
: null,
|
|
409
|
+
node.type === 'a'
|
|
410
|
+
? createValueAttribute('target', String(props.target ?? '_self'))
|
|
411
|
+
: null,
|
|
412
|
+
node.type === 'button'
|
|
413
|
+
? createValueAttribute('type', String(props.type ?? 'button'))
|
|
414
|
+
: null,
|
|
415
|
+
node.type === 'img'
|
|
416
|
+
? createValueAttribute('src', String(props.src ?? ''))
|
|
417
|
+
: null,
|
|
418
|
+
node.type === 'img'
|
|
419
|
+
? createValueAttribute('alt', String(props.alt ?? ''))
|
|
420
|
+
: null,
|
|
421
|
+
node.type === 'input'
|
|
422
|
+
? createValueAttribute('type', String(props.type ?? 'text'))
|
|
423
|
+
: null,
|
|
424
|
+
node.type === 'input' || node.type === 'textarea'
|
|
425
|
+
? props.name
|
|
426
|
+
? createValueAttribute('name', String(props.name))
|
|
427
|
+
: null
|
|
428
|
+
: null,
|
|
429
|
+
node.type === 'input' || node.type === 'textarea'
|
|
430
|
+
? props.placeholder
|
|
431
|
+
? createValueAttribute('placeholder', String(props.placeholder))
|
|
432
|
+
: null
|
|
433
|
+
: null,
|
|
434
|
+
node.type === 'textarea'
|
|
435
|
+
? createValueAttribute('defaultValue', String(props.text ?? ''))
|
|
436
|
+
: null,
|
|
437
|
+
node.type === 'label' && props.htmlFor
|
|
438
|
+
? createValueAttribute('htmlFor', String(props.htmlFor))
|
|
439
|
+
: null,
|
|
440
|
+
node.type === 'iframe'
|
|
441
|
+
? createValueAttribute('src', String(props.src ?? ''))
|
|
442
|
+
: null,
|
|
443
|
+
node.type === 'iframe'
|
|
444
|
+
? createValueAttribute(
|
|
445
|
+
'title',
|
|
446
|
+
String(props.title ?? 'Embedded content')
|
|
447
|
+
)
|
|
448
|
+
: null,
|
|
449
|
+
node.type === 'source'
|
|
450
|
+
? createValueAttribute('src', String(props.src ?? ''))
|
|
451
|
+
: null,
|
|
452
|
+
node.type === 'source' && props.type
|
|
453
|
+
? createValueAttribute('type', String(props.type))
|
|
454
|
+
: null,
|
|
455
|
+
node.type === 'video' || node.type === 'audio'
|
|
456
|
+
? props.src
|
|
457
|
+
? createValueAttribute('src', String(props.src))
|
|
458
|
+
: null
|
|
459
|
+
: null,
|
|
460
|
+
node.type === 'video' || node.type === 'audio'
|
|
461
|
+
? props.controls !== false
|
|
462
|
+
? createBooleanAttribute('controls')
|
|
463
|
+
: null
|
|
464
|
+
: null,
|
|
465
|
+
node.type === 'option' && props.value
|
|
466
|
+
? createValueAttribute('value', String(props.value))
|
|
467
|
+
: null,
|
|
468
|
+
node.type === 'optgroup'
|
|
469
|
+
? createValueAttribute('label', String(props.label ?? 'Group'))
|
|
470
|
+
: null,
|
|
471
|
+
node.type === 'progress' || node.type === 'meter'
|
|
472
|
+
? props.value !== undefined
|
|
473
|
+
? createValueAttribute('value', Number(props.value))
|
|
474
|
+
: null
|
|
475
|
+
: null,
|
|
476
|
+
node.type === 'meter'
|
|
477
|
+
? props.min !== undefined
|
|
478
|
+
? createValueAttribute('min', Number(props.min))
|
|
479
|
+
: null
|
|
480
|
+
: null,
|
|
481
|
+
node.type === 'progress' || node.type === 'meter'
|
|
482
|
+
? props.max !== undefined
|
|
483
|
+
? createValueAttribute('max', Number(props.max))
|
|
484
|
+
: null
|
|
485
|
+
: null,
|
|
486
|
+
]
|
|
487
|
+
|
|
488
|
+
return attributes.filter(Boolean)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function isSafeJsxAttributeName(name) {
|
|
492
|
+
return /^[A-Za-z_:][A-Za-z0-9_.:-]*$/.test(name)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function isSerializableAttributeValue(value) {
|
|
496
|
+
return (
|
|
497
|
+
typeof value === 'string' ||
|
|
498
|
+
typeof value === 'number' ||
|
|
499
|
+
typeof value === 'boolean' ||
|
|
500
|
+
Array.isArray(value)
|
|
501
|
+
)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function createValueAttribute(name, value) {
|
|
505
|
+
return t.jsxAttribute(
|
|
506
|
+
t.jsxIdentifier(name),
|
|
507
|
+
t.jsxExpressionContainer(
|
|
508
|
+
typeof value === 'number'
|
|
509
|
+
? Number.isFinite(value)
|
|
510
|
+
? t.numericLiteral(value)
|
|
511
|
+
: t.nullLiteral()
|
|
512
|
+
: typeof value === 'boolean'
|
|
513
|
+
? t.booleanLiteral(value)
|
|
514
|
+
: Array.isArray(value)
|
|
515
|
+
? t.arrayExpression(
|
|
516
|
+
value.map((item) =>
|
|
517
|
+
typeof item === 'number'
|
|
518
|
+
? t.numericLiteral(item)
|
|
519
|
+
: typeof item === 'boolean'
|
|
520
|
+
? t.booleanLiteral(item)
|
|
521
|
+
: t.stringLiteral(String(item))
|
|
522
|
+
)
|
|
523
|
+
)
|
|
524
|
+
: t.stringLiteral(String(value))
|
|
525
|
+
)
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function createCustomComponentImports(nodes, ownerPath, definitions) {
|
|
530
|
+
const usedNames = new Set()
|
|
531
|
+
const visit = (node) => {
|
|
532
|
+
if (!node) return
|
|
533
|
+
if (definitions.some((definition) => definition.name === node.type)) {
|
|
534
|
+
usedNames.add(node.type)
|
|
535
|
+
}
|
|
536
|
+
for (const child of node.children ?? []) visit(child)
|
|
537
|
+
}
|
|
538
|
+
nodes.forEach(visit)
|
|
539
|
+
|
|
540
|
+
return definitions
|
|
541
|
+
.filter((definition) => usedNames.has(definition.name))
|
|
542
|
+
.map((definition) => {
|
|
543
|
+
const specifier =
|
|
544
|
+
definition.exportName === 'default'
|
|
545
|
+
? t.importDefaultSpecifier(t.identifier(definition.name))
|
|
546
|
+
: t.importSpecifier(
|
|
547
|
+
t.identifier(definition.name),
|
|
548
|
+
t.identifier(definition.exportName)
|
|
549
|
+
)
|
|
550
|
+
|
|
551
|
+
return t.importDeclaration(
|
|
552
|
+
[specifier],
|
|
553
|
+
t.stringLiteral(
|
|
554
|
+
toRelativeImportPath(ownerPath, definition.importPath)
|
|
555
|
+
)
|
|
556
|
+
)
|
|
557
|
+
})
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function toRelativeImportPath(ownerPath, targetPath) {
|
|
561
|
+
const ownerSegments = normalizeProjectPath(ownerPath).split('/')
|
|
562
|
+
ownerSegments.pop()
|
|
563
|
+
const targetSegments = normalizeProjectPath(targetPath)
|
|
564
|
+
.replace(/\.[cm]?[jt]sx?$/, '')
|
|
565
|
+
.split('/')
|
|
566
|
+
|
|
567
|
+
while (
|
|
568
|
+
ownerSegments.length > 0 &&
|
|
569
|
+
targetSegments.length > 0 &&
|
|
570
|
+
ownerSegments[0] === targetSegments[0]
|
|
571
|
+
) {
|
|
572
|
+
ownerSegments.shift()
|
|
573
|
+
targetSegments.shift()
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const relative = [
|
|
577
|
+
...ownerSegments.map(() => '..'),
|
|
578
|
+
...targetSegments,
|
|
579
|
+
].join('/')
|
|
580
|
+
return relative.startsWith('.') ? relative : `./${relative}`
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function createBooleanAttribute(name) {
|
|
584
|
+
return t.jsxAttribute(t.jsxIdentifier(name), null)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function toPageImportPath(page, appFile = 'src/App.tsx') {
|
|
588
|
+
return toRelativeImportPath(appFile, getPageSourcePath(page))
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function escapeJsxText(value) {
|
|
592
|
+
return value
|
|
593
|
+
.replace(/&/g, '&')
|
|
594
|
+
.replace(/</g, '<')
|
|
595
|
+
.replace(/>/g, '>')
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function printProgram(statements) {
|
|
599
|
+
const file = t.file(t.program(statements))
|
|
600
|
+
return `${generate(file, generatorOptions).code}\n`
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const generatorOptions = {
|
|
604
|
+
comments: false,
|
|
605
|
+
compact: false,
|
|
606
|
+
concise: false,
|
|
607
|
+
jsescOption: { minimal: true },
|
|
608
|
+
}
|