@teleporthq/teleport-project-generator-html 0.22.2 → 0.22.4

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,170 +0,0 @@
1
- import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
2
- import PathResolver from 'path-browserify'
3
- import {
4
- FileType,
5
- GeneratedFile,
6
- ProjectPlugin,
7
- ProjectPluginStructure,
8
- UIDLElement,
9
- UIDLPropDefinition,
10
- UIDLStateDefinition,
11
- UIDLStyleDefinitions,
12
- } from '@teleporthq/teleport-types'
13
- const { parse, join, relative, isAbsolute } = PathResolver
14
-
15
- class ProjectPluginImageResolver implements ProjectPlugin {
16
- private relativePath: string
17
-
18
- async runBefore(structure: ProjectPluginStructure) {
19
- const { uidl, strategy } = structure
20
-
21
- const assetsPath = join(strategy.id, strategy.static.path.join('/'))
22
- const pagesPath = join(strategy.id, strategy.pages.path.join('/'))
23
- this.relativePath = relative(pagesPath, assetsPath)
24
-
25
- UIDLUtils.traverseElements(uidl.root.node, this.imageResolver)
26
- Object.values(uidl?.root?.styleSetDefinitions || {}).forEach((styleSet) => {
27
- this.resolveFromStyles(styleSet.content)
28
- })
29
-
30
- Object.values(uidl.components || {}).forEach((component) => {
31
- UIDLUtils.traverseElements(component.node, this.imageResolver)
32
- Object.values(component?.styleSetDefinitions || {}).forEach((styleSet) => {
33
- this.resolveFromStyles(styleSet.content)
34
- })
35
- this.resolvePropsAndStates(component.stateDefinitions)
36
- this.resolvePropsAndStates(component.propDefinitions)
37
- })
38
-
39
- return structure
40
- }
41
-
42
- async runAfter(structure: ProjectPluginStructure) {
43
- const { uidl, files } = structure
44
- const { stateDefinitions } = uidl.root
45
-
46
- if (stateDefinitions?.route) {
47
- const { defaultValue } = stateDefinitions.route
48
- const routes = UIDLUtils.extractRoutes(uidl.root)
49
- const defaultRoute = routes.find((route) => route.content?.value === defaultValue)
50
- if (!defaultRoute) {
51
- return structure
52
- }
53
- const sanitizedName = StringUtils.removeIllegalCharacters(
54
- defaultRoute.content.value as string
55
- )
56
- const pageName = StringUtils.camelCaseToDashCase(sanitizedName)
57
-
58
- if (pageName === 'index') {
59
- return structure
60
- }
61
-
62
- const component = StringUtils.dashCaseToUpperCamelCase(sanitizedName)
63
- const homeFile = files.get(component)
64
- if (!homeFile) {
65
- return structure
66
- }
67
-
68
- const htmlFile = homeFile.files.find(
69
- ({ name, fileType }: GeneratedFile) => name === pageName && fileType === FileType.HTML
70
- )
71
- if (!htmlFile) {
72
- return structure
73
- }
74
-
75
- files.set('index', {
76
- path: homeFile.path,
77
- files: [
78
- ...homeFile.files.filter(
79
- ({ name, fileType }) => name === pageName && fileType !== FileType.HTML
80
- ),
81
- {
82
- ...htmlFile,
83
- name: 'index',
84
- },
85
- ],
86
- })
87
- files.delete(component)
88
- }
89
-
90
- return structure
91
- }
92
-
93
- private resolvePropsAndStates = (
94
- defs: Record<string, UIDLPropDefinition> | Record<string, UIDLStateDefinition>
95
- ) => {
96
- Object.keys(defs || {}).forEach((propKey) => {
97
- const propValue = defs[propKey]
98
-
99
- if (
100
- propValue.type === 'string' &&
101
- typeof propValue?.defaultValue === 'string' &&
102
- parse(propValue?.defaultValue as string).dir.startsWith('/')
103
- ) {
104
- defs[propKey] = {
105
- ...defs[propKey],
106
- defaultValue: join(this.relativePath, propValue.defaultValue),
107
- }
108
- }
109
- })
110
- }
111
-
112
- private resolveFromStyles = (style: UIDLStyleDefinitions) => {
113
- if (
114
- style?.backgroundImage?.type === 'static' &&
115
- typeof style?.backgroundImage?.content === 'string'
116
- ) {
117
- const bgImage = style.backgroundImage.content
118
- if (bgImage.includes('http')) {
119
- return
120
- }
121
-
122
- const regex = /url(?:\(['"]?)(.*?)(?:['"]?\))/
123
- const matches = regex.exec(bgImage)
124
- if (matches && matches?.length > 0 && isAbsolute(matches[1])) {
125
- style.backgroundImage.content = bgImage.replace(
126
- matches[1],
127
- `${join(this.relativePath, matches[1])}`
128
- )
129
- }
130
- }
131
- }
132
-
133
- private imageResolver = (element: UIDLElement) => {
134
- this.resolveFromStyles(element?.style || {})
135
-
136
- Object.values(element?.referencedStyles || {}).forEach((styleRef) => {
137
- if (styleRef.content.mapType === 'inlined') {
138
- this.resolveFromStyles(styleRef.content.styles)
139
- }
140
- })
141
-
142
- if (
143
- element?.elementType === 'image' &&
144
- element.attrs?.src?.type === 'static' &&
145
- typeof element.attrs.src.content === 'string' &&
146
- isAbsolute(element.attrs.src.content) &&
147
- !element.attrs.src.content.startsWith('http')
148
- ) {
149
- element.attrs.src.content = join(this.relativePath, element.attrs.src.content)
150
- }
151
-
152
- if (element.elementType === 'component') {
153
- Object.keys(element?.attrs || {}).forEach((attrKey) => {
154
- const attrValue = element?.attrs[attrKey]
155
- if (
156
- attrValue.type === 'static' &&
157
- typeof attrValue.content === 'string' &&
158
- !attrValue.content.startsWith('http')
159
- ) {
160
- const resolvedPath = parse(attrValue.content)
161
- if (resolvedPath.dir.startsWith('/')) {
162
- element.attrs[attrKey].content = join(this.relativePath, attrValue.content)
163
- }
164
- }
165
- })
166
- }
167
- }
168
- }
169
-
170
- export const pluginImageResolver = new ProjectPluginImageResolver()