@teleporthq/teleport-plugin-html-base-component 0.29.0-alpha.0 → 0.29.0

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.
@@ -17,6 +17,8 @@ import {
17
17
  UIDLStyleValue,
18
18
  GeneratorOptions,
19
19
  UIDLRouteDefinitions,
20
+ ComponentPlugin,
21
+ ComponentStructure,
20
22
  } from '@teleporthq/teleport-types'
21
23
  import { HASTBuilders, HASTUtils } from '@teleporthq/teleport-plugin-common'
22
24
  import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
@@ -29,7 +31,10 @@ type NodeToHTML<NodeType, ReturnType> = (
29
31
  templatesLookUp: Record<string, unknown>,
30
32
  propDefinitions: Record<string, UIDLPropDefinition>,
31
33
  stateDefinitions: Record<string, UIDLStateDefinition>,
32
- externals: Record<string, ComponentUIDL>,
34
+ subComponentOptions: {
35
+ externals: Record<string, ComponentUIDL>
36
+ plugins: ComponentPlugin[]
37
+ },
33
38
  routeDefinitions: UIDLRouteDefinitions,
34
39
  structure: {
35
40
  chunks: ChunkDefinition[]
@@ -43,11 +48,12 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
43
48
  templatesLookUp,
44
49
  propDefinitions,
45
50
  stateDefinitions,
46
- externals,
51
+ subComponentOptions,
47
52
  routeDefinitions,
48
53
  structure
49
54
  ) => {
50
55
  switch (node.type) {
56
+ case 'inject':
51
57
  case 'raw':
52
58
  return HASTBuilders.createTextNode(node.content.toString())
53
59
 
@@ -63,7 +69,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
63
69
  templatesLookUp,
64
70
  propDefinitions,
65
71
  stateDefinitions,
66
- externals,
72
+ subComponentOptions,
67
73
  routeDefinitions,
68
74
  structure
69
75
  )
@@ -74,7 +80,7 @@ export const generateHtmlSynatx: NodeToHTML<UIDLNode, Promise<HastNode | HastTex
74
80
  templatesLookUp,
75
81
  propDefinitions,
76
82
  stateDefinitions,
77
- externals,
83
+ subComponentOptions,
78
84
  routeDefinitions,
79
85
  structure
80
86
  )
@@ -95,7 +101,7 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
95
101
  templatesLookUp,
96
102
  propDefinitions,
97
103
  stateDefinitions,
98
- externals,
104
+ subComponentOptions,
99
105
  routeDefinitions,
100
106
  structure
101
107
  ) => {
@@ -108,7 +114,6 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
108
114
  dependency,
109
115
  key,
110
116
  } = node.content
111
-
112
117
  const elementNode = HASTBuilders.createHTMLNode(elementType)
113
118
  templatesLookUp[key] = elementNode
114
119
 
@@ -122,7 +127,7 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
122
127
  node,
123
128
  propDefinitions,
124
129
  stateDefinitions,
125
- externals,
130
+ subComponentOptions,
126
131
  routeDefinitions,
127
132
  structure
128
133
  )
@@ -136,7 +141,7 @@ const generatElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTex
136
141
  templatesLookUp,
137
142
  propDefinitions,
138
143
  stateDefinitions,
139
- externals,
144
+ subComponentOptions,
140
145
  routeDefinitions,
141
146
  structure
142
147
  )
@@ -178,7 +183,10 @@ const generateComponentContent = async (
178
183
  node: UIDLElementNode,
179
184
  propDefinitions: Record<string, UIDLPropDefinition>,
180
185
  stateDefinitions: Record<string, UIDLStateDefinition>,
181
- externals: Record<string, ComponentUIDL>,
186
+ subComponentOptions: {
187
+ externals: Record<string, ComponentUIDL>
188
+ plugins: ComponentPlugin[]
189
+ },
182
190
  routeDefinitions: UIDLRouteDefinitions,
183
191
  structure: {
184
192
  chunks: ChunkDefinition[]
@@ -186,8 +194,9 @@ const generateComponentContent = async (
186
194
  options: GeneratorOptions
187
195
  }
188
196
  ) => {
197
+ const { externals, plugins } = subComponentOptions
189
198
  const { elementType, attrs = {}, key, children = [] } = node.content
190
- const { dependencies, chunks, options } = structure
199
+ const { dependencies, chunks = [], options } = structure
191
200
  const comp = UIDLUtils.cloneObject(externals[elementType] || {}) as ComponentUIDL
192
201
  const lookUpTemplates: Record<string, unknown> = {}
193
202
  let compHasSlots: boolean = false
@@ -229,7 +238,6 @@ const generateComponentContent = async (
229
238
  }
230
239
 
231
240
  const combinedProps = { ...propDefinitions, ...(comp?.propDefinitions || {}) }
232
-
233
241
  const propsForInstance = Object.keys(combinedProps).reduce(
234
242
  (acc: Record<string, UIDLPropDefinition>, propKey) => {
235
243
  if (attrs[propKey]) {
@@ -262,7 +270,6 @@ const generateComponentContent = async (
262
270
  },
263
271
  {}
264
272
  )
265
-
266
273
  const elementNode = HASTBuilders.createHTMLNode(StringUtils.camelCaseToDashCase(elementType))
267
274
  lookUpTemplates[key] = elementNode
268
275
 
@@ -283,21 +290,21 @@ const generateComponentContent = async (
283
290
  lookUpTemplates,
284
291
  propsForInstance,
285
292
  statesForInstance,
286
- externals,
293
+ subComponentOptions,
287
294
  routeDefinitions,
288
295
  structure
289
296
  )) as unknown as HastNode
290
297
 
291
298
  const cssPlugin = createCSSPlugin({
292
299
  templateStyle: 'html',
293
- templateChunkName: 'html-template',
300
+ templateChunkName: DEFAULT_COMPONENT_CHUNK_NAME,
294
301
  declareDependency: 'import',
295
302
  forceScoping: true,
296
303
  chunkName: comp.name,
297
304
  staticPropReferences: true,
298
305
  })
299
306
 
300
- const result = await cssPlugin({
307
+ const initialStructure: ComponentStructure = {
301
308
  uidl: {
302
309
  ...comp,
303
310
  propDefinitions: propsForInstance,
@@ -317,7 +324,15 @@ const generateComponentContent = async (
317
324
  ],
318
325
  dependencies,
319
326
  options,
320
- })
327
+ }
328
+
329
+ const result = await [cssPlugin, ...plugins].reduce(
330
+ async (previousPluginOperation: Promise<ComponentStructure>, plugin) => {
331
+ const modifiedStructure = await previousPluginOperation
332
+ return plugin(modifiedStructure)
333
+ },
334
+ Promise.resolve(initialStructure)
335
+ )
321
336
 
322
337
  if (compHasSlots) {
323
338
  result.chunks.forEach((chunk) => {
@@ -328,7 +343,12 @@ const generateComponentContent = async (
328
343
  } else {
329
344
  const chunk = chunks.find((item) => item.name === comp.name)
330
345
  if (!chunk) {
331
- const styleChunk = result.chunks.find((item: ChunkDefinition) => item.name === comp.name)
346
+ const styleChunk = result.chunks.find(
347
+ (item: ChunkDefinition) => item.fileType === FileType.CSS
348
+ )
349
+ if (!styleChunk) {
350
+ return
351
+ }
332
352
  chunks.push(styleChunk)
333
353
  }
334
354
  }
@@ -409,7 +429,7 @@ const handleAttributes = (
409
429
  }
410
430
 
411
431
  if (attrValue.type === 'raw') {
412
- HASTUtils.addAttributeToNode(htmlNode, attrKey, String(attrValue.content))
432
+ HASTUtils.addAttributeToNode(htmlNode, attrKey, attrValue.content)
413
433
  return
414
434
  }
415
435
 
@@ -427,7 +447,7 @@ const getValueFromReference = (
427
447
  key: string,
428
448
  definitions: Record<string, UIDLPropDefinition>
429
449
  ): string => {
430
- const usedReferenceValue = definitions[key]
450
+ const usedReferenceValue = definitions[key.includes('.') ? key.split('.')[0] : key]
431
451
 
432
452
  if (!usedReferenceValue) {
433
453
  throw new HTMLComponentGeneratorError(
@@ -445,7 +465,7 @@ const getValueFromReference = (
445
465
  )
446
466
  }
447
467
 
448
- if (!['string', 'number'].includes(usedReferenceValue?.type)) {
468
+ if (!['string', 'number', 'object'].includes(usedReferenceValue?.type)) {
449
469
  throw new HTMLComponentGeneratorError(
450
470
  `Attribute is using dynamic value, but received of type ${JSON.stringify(
451
471
  usedReferenceValue,