Skip to content

Commit

Permalink
fix bugs with re-using components with html components
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed Sep 4, 2024
1 parent f9a3a23 commit 5bf5d9e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createCSSModulesPlugin } from '../src'
import { staticNode, elementNode, component } from '@teleporthq/teleport-uidl-builders'
import { ComponentStructure } from '@teleporthq/teleport-types'
import { createComponentChunk } from './mocks'
import { JSXElement } from '@babel/types'

describe('Component Scoped Styles', () => {
const uidl = component('MYComponent', elementNode('container', {}, [], null, {}), {}, {})
Expand Down Expand Up @@ -85,8 +86,8 @@ describe('Component Scoped Styles', () => {

const { chunks } = await plugin(structure)
const jsxComponent = chunks.find((chunk) => chunk.name === 'jsx-component')
const jsxExpressions =
jsxComponent.meta.nodesLookup.container.openingElement.attributes[0].value.expression
const jsxExpressions = (jsxComponent?.meta?.nodesLookup?.container as unknown as JSXElement)
.openingElement.attributes[0]?.value.expression

expect(jsxExpressions.quasis.length).toBe(4)
expect(jsxExpressions.expressions[0]?.value).toBe('md-8')
Expand Down
6 changes: 3 additions & 3 deletions packages/teleport-plugin-css-modules/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ describe('plugin-css-modules', () => {
const jsxChunk = result.chunks.find((chunk) => chunk.fileType === 'js')
const cssFile = result.chunks.find((file) => file.fileType === 'css')

const nodeReference = jsxChunk.meta.nodesLookup.container
const styleAttr = nodeReference.openingElement.attributes[0]
const nodeReference = jsxChunk?.meta?.nodesLookup?.container
const styleAttr = nodeReference?.openingElement?.attributes?.[0]

expect(cssFile).toBeDefined()
expect(jsxChunk).toBeDefined()
expect(nodeReference.openingElement.attributes.length).toBe(1)
expect(nodeReference?.openingElement?.attributes?.length).toBe(1)
expect(styleAttr.value.expression.quasis.length).toBe(3)
expect(styleAttr.value.expression.expressions.length).toBe(2)
})
Expand Down
9 changes: 7 additions & 2 deletions packages/teleport-plugin-css-modules/__tests__/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export const createComponentChunk = (elementKey: string = 'container') => {
meta: {
nodesLookup: {
[elementKey]: {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {
type: 'JSXIdentifier',
name: '',
},
selfClosing: false,
attributes: [],
},
children: [],
},
},
dynamicRefPrefix: {
Expand All @@ -36,12 +41,12 @@ export const createComponentChunk = (elementKey: string = 'container') => {

export const setupPluginStructure = (
elementKey: string = 'container',
styleDefinition: UIDLStyleDefinitions = null
styleDefinition?: UIDLStyleDefinitions
) => {
const style = styleDefinition || {
height: staticNode('100px'),
}
const element = elementNode('container', {}, [], null, style)
const element = elementNode('container', {}, [], undefined, style)
element.content.key = elementKey
const uidlSample = component('CSSModules', element)

Expand Down
29 changes: 5 additions & 24 deletions packages/teleport-plugin-html-base-component/src/node-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ const generateElementNode: NodeToHTML<UIDLElementNode, Promise<HastNode | HastTe
} = node.content
const { dependencies } = structure
if (dependency && (dependency as UIDLDependency)?.type === 'local') {
if (nodesLookup[elementType]) {
return nodesLookup[elementType]
}

const compTag = await generateComponentContent(
node,
compName,
Expand Down Expand Up @@ -278,9 +274,7 @@ const generateComponentContent = async (

const componentClone = UIDLUtils.cloneObject<ComponentUIDL>(component)

let compHasSlots: boolean = false
if (children.length) {
compHasSlots = true
UIDLUtils.traverseNodes(componentClone.node, (childNode, parentNode) => {
if (childNode.type === 'slot' && parentNode.type === 'element') {
const nonSlotNodes = parentNode.content?.children?.filter((n) => n.type !== 'slot')
Expand Down Expand Up @@ -489,26 +483,13 @@ const generateComponentContent = async (
Promise.resolve(initialStructure)
)

if (compHasSlots) {
result.chunks.forEach((chunk) => {
if (chunk.fileType === FileType.CSS) {
chunks.push(chunk)
}
})
} else {
const chunk = chunks.find((item) => item.name === componentClone.name)
if (!chunk) {
const styleChunk = result.chunks.find(
(item: ChunkDefinition) => item.fileType === FileType.CSS
)
if (!styleChunk) {
return
}
chunks.push(styleChunk)
result.chunks.forEach((chunk) => {
if (chunk.fileType === FileType.CSS) {
chunks.push(chunk)
}
}
})

addNodeToLookup(elementType, node, compTag, nodesLookup, [compName, component.name])
addNodeToLookup(node.content.key, node, compTag, nodesLookup, [compName, component.name])
return compTag
}

Expand Down
66 changes: 38 additions & 28 deletions packages/teleport-plugin-react-jss/__tests__/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import { ChunkDefinition, FileType, ChunkType } from '@teleporthq/teleport-types'
import * as types from '@babel/types'

export const createComponentChunk = (): ChunkDefinition => ({
name: 'jsx-component',
meta: {
nodesLookup: {
container: {
openingElement: {
name: {
name: '',
},
attributes: [],
},
export const createComponentChunk = (): ChunkDefinition => {
const jsxElement: types.JSXElement = {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {
type: 'JSXIdentifier',
name: '',
},
selfClosing: false,
attributes: [],
},
dynamicRefPrefix: {
prop: 'props',
children: [],
}

return {
name: 'jsx-component',
meta: {
nodesLookup: {
container: jsxElement as unknown as Record<string, unknown>,
},
dynamicRefPrefix: {
prop: 'props',
},
},
},
type: ChunkType.AST,
fileType: FileType.JS,
linkAfter: ['import-local'],
content: {
declarations: [
{
init: {
params: [],
body: {
body: [],
type: ChunkType.AST,
fileType: FileType.JS,
linkAfter: ['import-local'],
content: {
declarations: [
{
init: {
params: [],
body: {
body: [],
},
},
},
},
],
},
})
],
},
}
}

0 comments on commit 5bf5d9e

Please sign in to comment.