@teleporthq/teleport-plugin-html-base-component 0.22.0 → 0.22.2

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.
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  import {
3
2
  UIDLNode,
4
3
  UIDLElementNode,
@@ -17,10 +16,11 @@ import {
17
16
  UIDLDependency,
18
17
  UIDLStyleValue,
19
18
  GeneratorOptions,
19
+ UIDLRouteDefinitions,
20
20
  } from '@teleporthq/teleport-types'
21
21
  import { HASTBuilders, HASTUtils } from '@teleporthq/teleport-plugin-common'
22
22
  import { StringUtils } from '@teleporthq/teleport-shared'
23
- import { element, staticNode } from '@teleporthq/teleport-uidl-builders'
23
+ import { staticNode } from '@teleporthq/teleport-uidl-builders'
24
24
  import { createCSSPlugin } from '@teleporthq/teleport-plugin-css'
25
25
  import { DEFAULT_COMPONENT_CHUNK_NAME } from './constants'
26
26
 
@@ -30,6 +30,7 @@ type NodeToHTML<NodeType, ReturnType> = (
30
30
  propDefinitions: Record<string, UIDLPropDefinition>,
31
31
  stateDefinitions: Record<string, UIDLStateDefinition>,
32
32
  externals: Record<string, ComponentUIDL>,
33
+ routeDefinitions: UIDLRouteDefinitions,
33
34
  structure: {
34
35
  chunks: ChunkDefinition[]
35
36
  dependencies: Record<string, UIDLDependency>
@@ -43,6 +44,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
43
44
  propDefinitions,
44
45
  stateDefinitions,
45
46
  externals,
47
+ routeDefinitions,
46
48
  structure
47
49
  ) => {
48
50
  switch (node.type) {
@@ -59,6 +61,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
59
61
  propDefinitions,
60
62
  stateDefinitions,
61
63
  externals,
64
+ routeDefinitions,
62
65
  structure
63
66
  )
64
67
 
@@ -69,6 +72,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
69
72
  propDefinitions,
70
73
  stateDefinitions,
71
74
  externals,
75
+ routeDefinitions,
72
76
  structure
73
77
  )
74
78
 
@@ -89,6 +93,7 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
89
93
  propDefinitions,
90
94
  stateDefinitions,
91
95
  externals,
96
+ routeDefinitions,
92
97
  structure
93
98
  ) => {
94
99
  const {
@@ -103,104 +108,23 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
103
108
  const elementNode = HASTBuilders.createHTMLNode(elementType)
104
109
  templatesLookUp[key] = elementNode
105
110
 
106
- const { dependencies, chunks, options } = structure
107
-
108
- if (dependency?.type === 'local') {
109
- const comp = externals[elementType]
110
-
111
- if (!comp) {
112
- throw new HTMLComponentGeneratorError(`${elementType} is not found from the externals. \n
113
- Received ${JSON.stringify(Object.keys(externals), null, 2)}`)
114
- }
115
-
116
- const combinedProps = { ...propDefinitions, ...(comp?.propDefinitions || {}) }
117
- const propsForInstance = Object.keys(combinedProps).reduce(
118
- (acc: Record<string, UIDLPropDefinition>, propKey) => {
119
- if (attrs[propKey]) {
120
- acc[propKey] = {
121
- ...combinedProps[propKey],
122
- defaultValue: attrs[propKey].content,
123
- }
124
- } else {
125
- acc[propKey] = combinedProps[propKey]
126
- }
127
-
128
- return acc
129
- },
130
- {}
131
- )
132
-
133
- const combinedStates = { ...stateDefinitions, ...(comp?.stateDefinitions || {}) }
134
- const statesForInstance = Object.keys(combinedStates).reduce(
135
- (acc: Record<string, UIDLStateDefinition>, propKey) => {
136
- if (attrs[propKey]) {
137
- acc[propKey] = {
138
- ...combinedStates[propKey],
139
- defaultValue: attrs[propKey].content,
140
- }
141
- } else {
142
- acc[propKey] = combinedStates[propKey]
143
- }
144
-
145
- return acc
146
- },
147
- {}
148
- )
111
+ const { dependencies } = structure
112
+ if (dependency && (dependency as UIDLDependency)?.type !== 'local') {
113
+ dependencies[dependency.path] = dependency
114
+ }
149
115
 
150
- const lookupTemplate: Record<string, unknown> = {}
151
- const compTag = await generateHtmlSynatx(
152
- comp.node,
153
- lookupTemplate,
154
- propsForInstance,
155
- statesForInstance,
116
+ if (dependency && (dependency as UIDLDependency)?.type === 'local') {
117
+ const compTag = await generateComponentContent(
118
+ node,
119
+ propDefinitions,
120
+ stateDefinitions,
156
121
  externals,
122
+ routeDefinitions,
157
123
  structure
158
124
  )
159
-
160
- const cssPlugin = createCSSPlugin({
161
- templateStyle: 'html',
162
- templateChunkName: 'html-template',
163
- declareDependency: 'import',
164
- forceScoping: true,
165
- chunkName: comp.name,
166
- staticPropReferences: true,
167
- })
168
-
169
- const result = await cssPlugin({
170
- uidl: {
171
- ...comp,
172
- propDefinitions: propsForInstance,
173
- stateDefinitions: statesForInstance,
174
- },
175
- chunks: [
176
- {
177
- type: ChunkType.HAST,
178
- fileType: FileType.HTML,
179
- name: DEFAULT_COMPONENT_CHUNK_NAME,
180
- linkAfter: [],
181
- content: compTag,
182
- meta: {
183
- nodesLookup: lookupTemplate,
184
- },
185
- },
186
- ],
187
- dependencies,
188
- options,
189
- })
190
-
191
- const chunk = chunks.find((item) => item.name === comp.name)
192
- if (!chunk) {
193
- const styleChunk = result.chunks.find((item: ChunkDefinition) => item.name === comp.name)
194
- chunks.push(styleChunk)
195
- }
196
-
197
125
  return compTag
198
126
  }
199
127
 
200
- if (dependency && dependency?.type !== 'local') {
201
- dependencies[dependency.path] = dependency
202
- }
203
-
204
128
  if (children) {
205
129
  for (const child of children) {
206
130
  const childTag = await generateHtmlSynatx(
@@ -209,6 +133,7 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
209
133
  propDefinitions,
210
134
  stateDefinitions,
211
135
  externals,
136
+ routeDefinitions,
212
137
  structure
213
138
  )
214
139
 
@@ -239,12 +164,121 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
239
164
  }
240
165
 
241
166
  if (Object.keys(attrs).length > 0) {
242
- handleAttributes(elementNode, attrs, propDefinitions, stateDefinitions)
167
+ handleAttributes(elementNode, attrs, propDefinitions, stateDefinitions, routeDefinitions)
243
168
  }
244
169
 
245
170
  return elementNode
246
171
  }
247
172
 
173
+ const generateComponentContent = async (
174
+ node: UIDLElementNode,
175
+ propDefinitions: Record<string, UIDLPropDefinition>,
176
+ stateDefinitions: Record<string, UIDLStateDefinition>,
177
+ externals: Record<string, ComponentUIDL>,
178
+ routeDefinitions: UIDLRouteDefinitions,
179
+ structure: {
180
+ chunks: ChunkDefinition[]
181
+ dependencies: Record<string, UIDLDependency>
182
+ options: GeneratorOptions
183
+ }
184
+ ) => {
185
+ const { elementType, attrs = {}, key } = node.content
186
+ const { dependencies, chunks, options } = structure
187
+ const comp = externals[elementType]
188
+ const lookUpTemplates: Record<string, unknown> = {}
189
+
190
+ if (!comp) {
191
+ throw new HTMLComponentGeneratorError(`${elementType} is not found from the externals. \n
192
+ Received ${JSON.stringify(Object.keys(externals), null, 2)}`)
193
+ }
194
+
195
+ const combinedProps = { ...propDefinitions, ...(comp?.propDefinitions || {}) }
196
+ const propsForInstance = Object.keys(combinedProps).reduce(
197
+ (acc: Record<string, UIDLPropDefinition>, propKey) => {
198
+ if (attrs[propKey]) {
199
+ acc[propKey] = {
200
+ ...combinedProps[propKey],
201
+ defaultValue: attrs[propKey]?.content || combinedProps[propKey]?.defaultValue,
202
+ }
203
+ } else {
204
+ acc[propKey] = combinedProps[propKey]
205
+ }
206
+
207
+ return acc
208
+ },
209
+ {}
210
+ )
211
+
212
+ const combinedStates = { ...stateDefinitions, ...(comp?.stateDefinitions || {}) }
213
+ const statesForInstance = Object.keys(combinedStates).reduce(
214
+ (acc: Record<string, UIDLStateDefinition>, propKey) => {
215
+ if (attrs[propKey]) {
216
+ acc[propKey] = {
217
+ ...combinedStates[propKey],
218
+ defaultValue: attrs[propKey]?.content || combinedStates[propKey]?.defaultValue,
219
+ }
220
+ } else {
221
+ acc[propKey] = combinedStates[propKey]
222
+ }
223
+
224
+ return acc
225
+ },
226
+ {}
227
+ )
228
+ const elementNode = HASTBuilders.createHTMLNode(elementType)
229
+ lookUpTemplates[key] = elementNode
230
+
231
+ const compTag = (await generateHtmlSynatx(
232
+ comp.node,
233
+ lookUpTemplates,
234
+ propsForInstance,
235
+ statesForInstance,
236
+ externals,
237
+ routeDefinitions,
238
+ structure
239
+ )) as unknown as HastNode
240
+
241
+ const cssPlugin = createCSSPlugin({
242
+ templateStyle: 'html',
243
+ templateChunkName: 'html-template',
244
+ declareDependency: 'import',
245
+ forceScoping: true,
246
+ chunkName: comp.name,
247
+ staticPropReferences: true,
248
+ })
249
+
250
+ const result = await cssPlugin({
251
+ uidl: {
252
+ ...comp,
253
+ propDefinitions: propsForInstance,
254
+ stateDefinitions: statesForInstance,
255
+ },
256
+ chunks: [
257
+ {
258
+ type: ChunkType.HAST,
259
+ fileType: FileType.HTML,
260
+ name: DEFAULT_COMPONENT_CHUNK_NAME,
261
+ linkAfter: [],
262
+ content: compTag,
263
+ meta: {
264
+ nodesLookup: lookUpTemplates,
265
+ },
266
+ },
267
+ ],
268
+ dependencies,
269
+ options,
270
+ })
271
+
272
+ const chunk = chunks.find((item) => item.name === comp.name)
273
+ if (!chunk) {
274
+ const styleChunk = result.chunks.find((item: ChunkDefinition) => item.name === comp.name)
275
+ chunks.push(styleChunk)
276
+ }
277
+
278
+ HASTUtils.addChildNode(elementNode, compTag)
279
+ return elementNode
280
+ }
281
+
248
282
  const generateDynamicNode: NodeToHTML<UIDLDynamicReference, HastNode> = (
249
283
  node,
250
284
  _,
@@ -284,7 +318,8 @@ const handleAttributes = (
284
318
  htmlNode: HastNode,
285
319
  attrs: Record<string, UIDLAttributeValue>,
286
320
  propDefinitions: Record<string, UIDLPropDefinition>,
287
- stateDefinitions: Record<string, UIDLStateDefinition>
321
+ stateDefinitions: Record<string, UIDLStateDefinition>,
322
+ routeDefinitions: UIDLRouteDefinitions
288
323
  ) => {
289
324
  Object.keys(attrs).forEach((attrKey) => {
290
325
  let attrValue = attrs[attrKey]
@@ -296,9 +331,15 @@ const handleAttributes = (
296
331
  attrValue.content.startsWith('/')
297
332
  ) {
298
333
  attrValue =
299
- attrValue.content === '/'
334
+ attrValue.content === '/' ||
335
+ attrValue.content ===
336
+ `/${StringUtils.camelCaseToDashCase(
337
+ StringUtils.removeIllegalCharacters(routeDefinitions?.defaultValue || '')
338
+ )}`
300
339
  ? staticNode('index.html')
301
340
  : staticNode(`${attrValue.content.split('/').pop()}.html`)
341
+ HASTUtils.addAttributeToNode(htmlNode, attrKey, String(attrValue.content))
342
+ return
302
343
  }
303
344
 
304
345
  if (attrValue.type === 'dynamic') {
@@ -312,8 +353,10 @@ const handleAttributes = (
312
353
 
313
354
  if (typeof attrValue.content === 'boolean') {
314
355
  HASTUtils.addBooleanAttributeToNode(htmlNode, attrKey)
356
+ return
315
357
  } else if (typeof attrValue.content === 'string' || typeof attrValue.content === 'number') {
316
358
  HASTUtils.addAttributeToNode(htmlNode, attrKey, StringUtils.encode(String(attrValue.content)))
359
+ return
317
360
  }
318
361
  })
319
362
  }