@teleporthq/teleport-plugin-html-base-component 0.33.3 → 0.33.6

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.
@@ -41,7 +41,7 @@ const isValidURL = (url: string) => {
41
41
 
42
42
  type NodeToHTML<NodeType, ReturnType> = (
43
43
  node: NodeType,
44
- templatesLookUp: Record<string, unknown>,
44
+ nodesLookup: Record<string, HastNode | HastText>,
45
45
  propDefinitions: Record<string, UIDLPropDefinition>,
46
46
  stateDefinitions: Record<string, UIDLStateDefinition>,
47
47
  subComponentOptions: {
@@ -56,9 +56,9 @@ type NodeToHTML<NodeType, ReturnType> = (
56
56
  }
57
57
  ) => ReturnType
58
58
 
59
- export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastText>> = async (
59
+ export const generateHtmlSyntax: NodeToHTML<UIDLNode, Promise<HastNode | HastText>> = async (
60
60
  node,
61
- templatesLookUp,
61
+ nodesLookup,
62
62
  propDefinitions,
63
63
  stateDefinitions,
64
64
  subComponentOptions,
@@ -78,7 +78,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
78
78
  case 'element':
79
79
  const elementNode = await generateElementNode(
80
80
  node,
81
- templatesLookUp,
81
+ nodesLookup,
82
82
  propDefinitions,
83
83
  stateDefinitions,
84
84
  subComponentOptions,
@@ -89,7 +89,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
89
89
  case 'dynamic':
90
90
  const dynamicNode = await generateDynamicNode(
91
91
  node,
92
- templatesLookUp,
92
+ nodesLookup,
93
93
  propDefinitions,
94
94
  stateDefinitions,
95
95
  subComponentOptions,
@@ -111,7 +111,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
111
111
 
112
112
  const generateElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastText>> = async (
113
113
  node,
114
- templatesLookUp,
114
+ nodesLookup,
115
115
  propDefinitions,
116
116
  stateDefinitions,
117
117
  subComponentOptions,
@@ -126,42 +126,51 @@ const generateElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTe
126
126
  dependency,
127
127
  key,
128
128
  } = node.content
129
-
130
- const elementNode = HASTBuilders.createHTMLNode(elementType)
131
- templatesLookUp[key] = elementNode
132
-
133
129
  const { dependencies } = structure
134
130
  if (dependency && (dependency as UIDLDependency)?.type !== 'local') {
135
131
  dependencies[dependency.path] = dependency
136
132
  }
137
133
 
138
134
  if (dependency && (dependency as UIDLDependency)?.type === 'local') {
135
+ for (const attrKey of Object.keys(attrs)) {
136
+ const attr = attrs[attrKey]
137
+ if (attr.type === 'element') {
138
+ await generateElementNode(
139
+ attr,
140
+ nodesLookup,
141
+ propDefinitions,
142
+ stateDefinitions,
143
+ subComponentOptions,
144
+ structure
145
+ )
146
+ }
147
+ }
148
+
139
149
  const compTag = await generateComponentContent(
140
150
  node,
151
+ nodesLookup,
141
152
  propDefinitions,
142
153
  stateDefinitions,
143
- templatesLookUp,
144
154
  subComponentOptions,
145
155
  structure
146
156
  )
157
+
147
158
  return compTag
148
159
  }
149
160
 
161
+ const elementNode = HASTBuilders.createHTMLNode(elementType)
162
+
150
163
  if (children) {
151
164
  for (const child of children) {
152
- const childTag = await generateHtmlSynatx(
165
+ const childTag = await generateHtmlSyntax(
153
166
  child,
154
- templatesLookUp,
167
+ nodesLookup,
155
168
  propDefinitions,
156
169
  stateDefinitions,
157
170
  subComponentOptions,
158
171
  structure
159
172
  )
160
173
 
161
- if (!childTag) {
162
- return
163
- }
164
-
165
174
  if (typeof childTag === 'string') {
166
175
  HASTUtils.addTextNode(elementNode, childTag)
167
176
  } else {
@@ -194,14 +203,15 @@ const generateElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTe
194
203
  structure.outputOptions
195
204
  )
196
205
 
206
+ nodesLookup[key] = elementNode
197
207
  return elementNode
198
208
  }
199
209
 
200
210
  const generateComponentContent = async (
201
211
  node: UIDLElementNode,
212
+ nodesLookup: Record<string, HastNode | HastText>,
202
213
  propDefinitions: Record<string, UIDLPropDefinition>,
203
214
  stateDefinitions: Record<string, UIDLStateDefinition>,
204
- templateLookup: Record<string, unknown>,
205
215
  subComponentOptions: {
206
216
  externals: Record<string, ComponentUIDL>
207
217
  plugins: ComponentPlugin[]
@@ -218,17 +228,17 @@ const generateComponentContent = async (
218
228
  const { dependencies, chunks = [], options } = structure
219
229
  // "Component" will not exist when generating a component because the resolver checks for illegal class names
220
230
  const compName = elementType === 'Component' ? 'AppComponent' : elementType
221
- const comp = UIDLUtils.cloneObject(externals[compName] || {}) as ComponentUIDL
222
- let compHasSlots: boolean = false
223
-
224
- if (!comp || !comp?.node) {
225
- throw new HTMLComponentGeneratorError(`${elementType} is not found from the externals. \n
226
- Received ${JSON.stringify(Object.keys(externals), null, 2)}`)
231
+ const component = externals[compName]
232
+ if (component === undefined) {
233
+ throw new HTMLComponentGeneratorError(`${compName} is missing from externals object`)
227
234
  }
228
235
 
236
+ const componentClone = UIDLUtils.cloneObject(component) as ComponentUIDL
237
+ let compHasSlots: boolean = false
238
+
229
239
  if (children.length) {
230
240
  compHasSlots = true
231
- UIDLUtils.traverseNodes(comp.node, (childNode, parentNode) => {
241
+ UIDLUtils.traverseNodes(componentClone.node, (childNode, parentNode) => {
232
242
  if (childNode.type === 'slot' && parentNode.type === 'element') {
233
243
  const nonSlotNodes = parentNode.content?.children?.filter((n) => n.type !== 'slot')
234
244
  parentNode.content.children = [
@@ -257,8 +267,9 @@ const generateComponentContent = async (
257
267
  node.content.children = []
258
268
  }
259
269
 
260
- const combinedProps = { ...propDefinitions, ...(comp?.propDefinitions || {}) }
270
+ const combinedProps = { ...propDefinitions, ...(componentClone?.propDefinitions || {}) }
261
271
  const propsForInstance: Record<string, UIDLPropDefinition> = {}
272
+
262
273
  for (const propKey of Object.keys(combinedProps)) {
263
274
  // If the attribute is a named-slot, then we can directly pass the value instead of just the content
264
275
  if (attrs[propKey]?.type === 'element') {
@@ -266,10 +277,7 @@ const generateComponentContent = async (
266
277
  ...combinedProps[propKey],
267
278
  defaultValue: attrs[propKey],
268
279
  }
269
- continue
270
- }
271
-
272
- if (attrs[propKey]) {
280
+ } else if (attrs[propKey]) {
273
281
  propsForInstance[propKey] = {
274
282
  ...combinedProps[propKey],
275
283
  defaultValue: attrs[propKey]?.content || combinedProps[propKey]?.defaultValue,
@@ -279,7 +287,7 @@ const generateComponentContent = async (
279
287
  }
280
288
  }
281
289
 
282
- const combinedStates = { ...stateDefinitions, ...(comp?.stateDefinitions || {}) }
290
+ const combinedStates = { ...stateDefinitions, ...(componentClone?.stateDefinitions || {}) }
283
291
  const statesForInstance = Object.keys(combinedStates).reduce(
284
292
  (acc: Record<string, UIDLStateDefinition>, propKey) => {
285
293
  if (attrs[propKey]) {
@@ -296,42 +304,48 @@ const generateComponentContent = async (
296
304
  {}
297
305
  )
298
306
 
299
- const elementNode = HASTBuilders.createHTMLNode(StringUtils.camelCaseToDashCase(elementType))
300
- templateLookup[key] = elementNode
301
-
302
- const compTag = (await generateHtmlSynatx(
303
- {
304
- ...comp.node,
305
- content: {
306
- ...comp.node.content,
307
- style: {
308
- ...(comp.node.content?.style || {}),
309
- display: {
310
- type: 'static',
311
- content: 'contents',
312
- },
307
+ let componentWrapper = StringUtils.camelCaseToDashCase(`${compName}-wrapper`)
308
+ const isExistingNode = nodesLookup[componentWrapper]
309
+ if (isExistingNode !== undefined) {
310
+ componentWrapper = `${componentWrapper}-${StringUtils.generateRandomString()}`
311
+ }
312
+
313
+ const componentInstanceToGenerate: UIDLElementNode = {
314
+ type: 'element',
315
+ content: {
316
+ elementType: componentWrapper,
317
+ key: componentWrapper,
318
+ children: [componentClone.node],
319
+ style: {
320
+ display: {
321
+ type: 'static',
322
+ content: 'contents',
313
323
  },
314
324
  },
315
325
  },
316
- templateLookup,
326
+ }
327
+
328
+ const compTag = await generateHtmlSyntax(
329
+ componentInstanceToGenerate,
330
+ nodesLookup,
317
331
  propsForInstance,
318
332
  statesForInstance,
319
333
  subComponentOptions,
320
334
  structure
321
- )) as unknown as HastNode
335
+ )
322
336
 
323
337
  const cssPlugin = createCSSPlugin({
324
338
  templateStyle: 'html',
325
339
  templateChunkName: DEFAULT_COMPONENT_CHUNK_NAME,
326
340
  declareDependency: 'import',
327
341
  forceScoping: true,
328
- chunkName: comp.name,
342
+ chunkName: componentClone.name,
329
343
  staticPropReferences: true,
330
344
  })
331
345
 
332
346
  const initialStructure: ComponentStructure = {
333
347
  uidl: {
334
- ...comp,
348
+ ...componentClone,
335
349
  propDefinitions: propsForInstance,
336
350
  stateDefinitions: statesForInstance,
337
351
  },
@@ -343,7 +357,7 @@ const generateComponentContent = async (
343
357
  linkAfter: [],
344
358
  content: compTag,
345
359
  meta: {
346
- nodesLookup: templateLookup,
360
+ nodesLookup,
347
361
  },
348
362
  },
349
363
  ],
@@ -366,7 +380,7 @@ const generateComponentContent = async (
366
380
  }
367
381
  })
368
382
  } else {
369
- const chunk = chunks.find((item) => item.name === comp.name)
383
+ const chunk = chunks.find((item) => item.name === componentClone.name)
370
384
  if (!chunk) {
371
385
  const styleChunk = result.chunks.find(
372
386
  (item: ChunkDefinition) => item.fileType === FileType.CSS
@@ -378,12 +392,13 @@ const generateComponentContent = async (
378
392
  }
379
393
  }
380
394
 
395
+ nodesLookup[key] = compTag
381
396
  return compTag
382
397
  }
383
398
 
384
399
  const generateDynamicNode: NodeToHTML<UIDLDynamicReference, Promise<HastNode>> = async (
385
400
  node,
386
- templateLookup,
401
+ nodesLookup,
387
402
  propDefinitions,
388
403
  stateDefinitions,
389
404
  subComponentOptions,
@@ -397,7 +412,7 @@ const generateDynamicNode: NodeToHTML<UIDLDynamicReference, Promise<HastNode>> =
397
412
  if (usedReferenceValue.type === 'element' && usedReferenceValue.defaultValue) {
398
413
  const slotNode = await generateElementNode(
399
414
  usedReferenceValue.defaultValue as UIDLElementNode,
400
- templateLookup,
415
+ nodesLookup,
401
416
  propDefinitions,
402
417
  stateDefinitions,
403
418
  subComponentOptions,
@@ -538,6 +553,7 @@ const handleAttributes = (
538
553
 
539
554
  case 'element':
540
555
  case 'import':
556
+ case 'expr':
541
557
  break
542
558
 
543
559
  default: {