@wix/zero-config-implementation 1.19.0 → 1.21.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.
@@ -1,3 +1,6 @@
1
1
  import { ExtractedCssInfo } from '../../index';
2
2
  import { ExtractedElement } from '../react/extractors/core/tree-builder';
3
- export declare function matchCssSelectors(html: string, elements: ExtractedElement[], cssInfos: ExtractedCssInfo[]): ExtractedElement[];
3
+ export declare function matchCssSelectors(html: string, elements: ExtractedElement[], cssInfos: ExtractedCssInfo[]): {
4
+ elements: ExtractedElement[];
5
+ varUsedByTraceId: Map<string, Set<string>>;
6
+ };
@@ -46,4 +46,14 @@ export interface CSSParserAPI {
46
46
  * @returns DOM-matchable selector string or null
47
47
  */
48
48
  getDomSelector: (selector: string) => string | null;
49
+ /**
50
+ * Determines the CSS property type for a custom property based on how it is used.
51
+ * If all usages of varName are within the same CSS property, returns that property name.
52
+ * If usages differ, returns the CSS data type inferred from the initial value
53
+ * ('color', 'length', 'number', or 'string').
54
+ * Returns undefined if the variable is never used via var().
55
+ * @param varName - The CSS variable name (with or without --)
56
+ * @param defaultValue - The initial value string of the custom property
57
+ */
58
+ getVarPropertyType: (varName: string, defaultValue: string) => string | undefined;
49
59
  }
@@ -17,6 +17,7 @@ export interface ExtractedCssInfo {
17
17
  }
18
18
  export interface ComponentInfoWithCss extends CoupledComponentInfo {
19
19
  css: ExtractedCssInfo[];
20
+ varUsedByTraceId: Map<string, Set<string>>;
20
21
  }
21
22
  /** The result of processing a single component through the manifest pipeline. */
22
23
  export interface ProcessComponentResult {
@@ -0,0 +1,6 @@
1
+ import { DATA } from '@wix/zero-config-schema';
2
+ /**
3
+ * Maps @wix/public-schemas type names (PascalCase) to their DATA_TYPE keys.
4
+ * Derived from the exports of @wix/public-schemas.
5
+ */
6
+ export declare const WIX_TYPE_TO_DATA_TYPE: Record<string, keyof typeof DATA.DATA_TYPE>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "registry": "https://registry.npmjs.org/",
5
5
  "access": "public"
6
6
  },
7
- "version": "1.19.0",
7
+ "version": "1.21.0",
8
8
  "description": "Core library for extracting component manifests from JS and CSS files",
9
9
  "type": "module",
10
10
  "main": "dist/index.js",
@@ -38,6 +38,7 @@
38
38
  "typescript": "^5.0.0"
39
39
  },
40
40
  "dependencies": {
41
+ "@wix/zero-config-schema": "1.0.0",
41
42
  "lightningcss": "^1.31.1"
42
43
  },
43
44
  "devDependencies": {
@@ -75,5 +76,5 @@
75
76
  ]
76
77
  }
77
78
  },
78
- "falconPackageHash": "c023fc2f7c36ad9d9d7f5d3e24b22d292e138350cb6a48ae6964e8a0"
79
+ "falconPackageHash": "3957b9b1458a26239d8d8e3088fe1996b439e4c7aa91e8add92e1305"
79
80
  }
@@ -12,16 +12,17 @@
12
12
  * resolved type metadata is inconsistent (kind === 'array' but no elementType).
13
13
  */
14
14
 
15
+ import type { DataItem } from '@wix/zero-config-schema'
16
+ import { DATA, MEDIA } from '@wix/zero-config-schema'
15
17
  import type { Result } from 'neverthrow'
16
18
  import { err, ok } from 'neverthrow'
17
19
  import { ParseError } from '../errors'
18
20
  import type { DOMBinding, TrackingStores } from '../information-extractors/react'
19
21
  import type { PropInfo, ResolvedType } from '../information-extractors/ts/types'
20
- import type { DataItem } from '../schema'
21
- import { DATA, MEDIA } from '../schema'
22
+ import { WIX_TYPE_TO_DATA_TYPE } from '../wix-type-to-data-type'
22
23
  import { formatDisplayName } from './utils'
23
24
 
24
- const { DATA_TYPE, WIX_TYPE_TO_DATA_TYPE } = DATA
25
+ const { DATA_TYPE } = DATA
25
26
 
26
27
  type ParseErrorInstance = InstanceType<typeof ParseError>
27
28
 
@@ -376,7 +377,7 @@ function handleSemanticType(dataItem: DataItem, resolvedType: ResolvedType): voi
376
377
  dataItem.text = {}
377
378
  }
378
379
 
379
- const EVENT_HANDLER_ATTR_TO_DATA_TYPE: Record<string, string> = {
380
+ const EVENT_HANDLER_ATTR_TO_DATA_TYPE: Record<string, (typeof DATA_TYPE)[keyof typeof DATA_TYPE]> = {
380
381
  onclick: DATA_TYPE.onClick,
381
382
  onchange: DATA_TYPE.onChange,
382
383
  onkeypress: DATA_TYPE.onKeyPress,
@@ -1,3 +1,12 @@
1
+ import type {
2
+ CssCustomPropertyItem,
3
+ CssPropertyItem,
4
+ DataItem,
5
+ EditorElement,
6
+ EditorReactComponent,
7
+ ElementItem,
8
+ } from '@wix/zero-config-schema'
9
+ import { ELEMENTS } from '@wix/zero-config-schema'
1
10
  import type { ComponentInfoWithCss } from '../index'
2
11
  import type { MatchedCssData } from '../information-extractors/css/types'
3
12
  import type {
@@ -7,15 +16,6 @@ import type {
7
16
  ExtractedElement,
8
17
  TrackingStores,
9
18
  } from '../information-extractors/react'
10
- import type {
11
- CssCustomPropertyItem,
12
- CssPropertyItem,
13
- DataItem,
14
- EditorElement,
15
- EditorReactComponent,
16
- ElementItem,
17
- } from '../schema'
18
- import { ELEMENTS } from '../schema'
19
19
  import { findPreferredSemanticClass } from '../utils/css-class'
20
20
  import { buildDataItem } from './data-item-builder'
21
21
  import { formatDisplayName } from './utils'
@@ -24,22 +24,32 @@ import { formatDisplayName } from './utils'
24
24
  const EXCLUDED_PROPS = new Set(['id', 'className', 'elementProps', 'wix'])
25
25
 
26
26
  export function toEditorReactComponent(component: ComponentInfoWithCss): EditorReactComponent {
27
+ const nearestCommonAncestorCustomProps = buildNearestCommonAncestorCustomProps(component)
27
28
  return {
28
- editorElement: buildEditorElement(component),
29
+ editorElement: buildEditorElement(component, nearestCommonAncestorCustomProps),
29
30
  }
30
31
  }
31
32
 
32
- function buildEditorElement(component: ComponentInfoWithCss): EditorElement {
33
+ function buildEditorElement(
34
+ component: ComponentInfoWithCss,
35
+ nearestCommonAncestorCustomProps: Map<string, Record<string, CssCustomPropertyItem>>,
36
+ ): EditorElement {
33
37
  const rootElement = component.elements[0]
34
38
  const childElements = rootElement?.children ?? []
39
+ const rootCustomProps = rootElement ? (nearestCommonAncestorCustomProps.get(rootElement.traceId) ?? {}) : {}
35
40
 
36
41
  return {
37
42
  selector: buildSelector(rootElement),
38
43
  displayName: formatDisplayName(component.componentName),
39
44
  data: buildData(component.props, component.propUsages),
40
- elements: buildElements(childElements, component.innerElementProps, component.propUsages),
45
+ elements: buildElements(
46
+ childElements,
47
+ nearestCommonAncestorCustomProps,
48
+ component.innerElementProps,
49
+ component.propUsages,
50
+ ),
41
51
  cssProperties: buildCssProperties(rootElement),
42
- cssCustomProperties: buildCssCustomPropertiesForElement(rootElement),
52
+ cssCustomProperties: rootCustomProps,
43
53
  }
44
54
  }
45
55
 
@@ -75,6 +85,7 @@ function buildData(
75
85
 
76
86
  function buildElements(
77
87
  elements: ExtractedElement[],
88
+ nearestCommonAncestorCustomProps: Map<string, Record<string, CssCustomPropertyItem>>,
78
89
  innerElementProps?: CoupledComponentInfo['innerElementProps'],
79
90
  propUsages?: TrackingStores['propUsages'],
80
91
  ): Record<string, ElementItem> {
@@ -84,7 +95,7 @@ function buildElements(
84
95
  const elementData = innerElementProps?.get(element.traceId)
85
96
  const data = elementData && propUsages ? buildData(elementData, propUsages) : undefined
86
97
  const cssProps = buildCssProperties(element)
87
- const cssCustomProps = buildCssCustomPropertiesForElement(element)
98
+ const cssCustomProps = nearestCommonAncestorCustomProps.get(element.traceId) ?? {}
88
99
 
89
100
  result[element.name] = {
90
101
  elementType: ELEMENTS.ELEMENT_TYPE.inlineElement,
@@ -95,11 +106,13 @@ function buildElements(
95
106
  ...(data && Object.keys(data).length > 0 && { data }),
96
107
  // CSS properties from heuristic + matched CSS files
97
108
  ...(Object.keys(cssProps).length > 0 && { cssProperties: cssProps }),
98
- // CSS custom properties from matched rules for this element
109
+ // CSS custom properties placed on the Nearest Common Ancestor of all elements that use each variable
99
110
  ...(Object.keys(cssCustomProps).length > 0 && { cssCustomProperties: cssCustomProps }),
100
111
  // Recursively build nested elements
101
112
  elements:
102
- element.children.length > 0 ? buildElements(element.children, innerElementProps, propUsages) : undefined,
113
+ element.children.length > 0
114
+ ? buildElements(element.children, nearestCommonAncestorCustomProps, innerElementProps, propUsages)
115
+ : undefined,
103
116
  },
104
117
  }
105
118
  }
@@ -107,6 +120,174 @@ function buildElements(
107
120
  return result
108
121
  }
109
122
 
123
+ // ─────────────────────────────────────────────────────────────────────────────
124
+ // CSS Custom Properties — Nearest Common Ancestor computation
125
+ // ─────────────────────────────────────────────────────────────────────────────
126
+
127
+ /**
128
+ * Pre-computes a map of traceId → custom property items to assign to each element.
129
+ * Each custom property is placed on the nearest common ancestor (Nearest Common Ancestor) of all elements
130
+ * that use it via var(). Falls back to the element that defines it if unused.
131
+ */
132
+ function buildNearestCommonAncestorCustomProps(
133
+ component: ComponentInfoWithCss,
134
+ ): Map<string, Record<string, CssCustomPropertyItem>> {
135
+ const parentMap = buildParentMap(component.elements)
136
+
137
+ // Collect default values from matched elements only — this excludes values from theme/modifier
138
+ // selectors that were not applied during mock rendering (e.g. .component.dark when variant='light').
139
+ const allCustomPropertyValues = collectMatchedCustomPropertyValues(component.elements)
140
+
141
+ // Fall back to the raw CSS scan for any vars not captured by element matching
142
+ // (e.g. vars defined in a selector that matched no element in the rendered output).
143
+ for (const cssInfo of component.css) {
144
+ for (const [varName, defaultValue] of cssInfo.customProperties) {
145
+ if (!allCustomPropertyValues.has(varName)) {
146
+ allCustomPropertyValues.set(varName, defaultValue)
147
+ }
148
+ }
149
+ }
150
+
151
+ const nearestCommonAncestorCustomProps = new Map<string, Record<string, CssCustomPropertyItem>>()
152
+
153
+ for (const [varName, defaultValue] of allCustomPropertyValues) {
154
+ if (varName === '--display') continue
155
+
156
+ const usedByTraceIds = component.varUsedByTraceId.get(varName) ?? new Set<string>()
157
+
158
+ let nearestCommonAncestorTraceId: string | undefined
159
+ if (usedByTraceIds.size > 0) {
160
+ nearestCommonAncestorTraceId = findNearestCommonAncestor(usedByTraceIds, parentMap)
161
+ } else {
162
+ // Fallback: place on the element whose CSS selector defines the variable
163
+ nearestCommonAncestorTraceId = findDefiningElement(component.elements, varName)
164
+ }
165
+
166
+ if (!nearestCommonAncestorTraceId) continue
167
+
168
+ const cssPropertyType = getVarPropertyTypeFromCssInfos(varName, defaultValue, component.css)
169
+ const cleanVarName = varName.startsWith('--') ? varName.slice(2) : varName
170
+
171
+ const existingProps = nearestCommonAncestorCustomProps.get(nearestCommonAncestorTraceId) ?? {}
172
+ existingProps[cleanVarName] = {
173
+ defaultValue,
174
+ ...(cssPropertyType !== undefined && { cssPropertyType }),
175
+ }
176
+ nearestCommonAncestorCustomProps.set(nearestCommonAncestorTraceId, existingProps)
177
+ }
178
+
179
+ return nearestCommonAncestorCustomProps
180
+ }
181
+
182
+ /**
183
+ * Queries each CSS parser API for the property type of a variable and returns
184
+ * the first defined result.
185
+ */
186
+ function getVarPropertyTypeFromCssInfos(
187
+ varName: string,
188
+ defaultValue: string,
189
+ cssInfos: ComponentInfoWithCss['css'],
190
+ ): CssCustomPropertyItem['cssPropertyType'] {
191
+ for (const cssInfo of cssInfos) {
192
+ const propertyType = cssInfo.api.getVarPropertyType(varName, defaultValue)
193
+ if (propertyType !== undefined) return propertyType as CssCustomPropertyItem['cssPropertyType']
194
+ }
195
+ return undefined
196
+ }
197
+
198
+ /**
199
+ * Walks the element tree and collects custom property default values from the CSS selector
200
+ * matches recorded on each element. Only values from selectors that matched during mock rendering
201
+ * are included, so theme/modifier variant values (e.g. .component.dark) are excluded when the
202
+ * corresponding class was not present on the rendered element.
203
+ */
204
+ function collectMatchedCustomPropertyValues(elements: ExtractedElement[]): Map<string, string> {
205
+ const values = new Map<string, string>()
206
+ for (const element of elements) {
207
+ const matcherData = element.extractorData.get('css-matcher') as MatchedCssData | undefined
208
+ if (matcherData) {
209
+ for (const [varName, varValue] of Object.entries(matcherData.customProperties)) {
210
+ if (!values.has(varName)) values.set(varName, varValue)
211
+ }
212
+ }
213
+ for (const [varName, varValue] of collectMatchedCustomPropertyValues(element.children)) {
214
+ if (!values.has(varName)) values.set(varName, varValue)
215
+ }
216
+ }
217
+ return values
218
+ }
219
+
220
+ /**
221
+ * Builds a map of traceId → parent traceId by recursively walking the element tree.
222
+ */
223
+ function buildParentMap(elements: ExtractedElement[], parentTraceId?: string): Map<string, string> {
224
+ const parentMap = new Map<string, string>()
225
+ for (const element of elements) {
226
+ if (parentTraceId !== undefined) {
227
+ parentMap.set(element.traceId, parentTraceId)
228
+ }
229
+ for (const [childTraceId, childParentTraceId] of buildParentMap(element.children, element.traceId)) {
230
+ parentMap.set(childTraceId, childParentTraceId)
231
+ }
232
+ }
233
+ return parentMap
234
+ }
235
+
236
+ /**
237
+ * Returns the path from the root element down to the given traceId (inclusive).
238
+ */
239
+ function pathFromRoot(traceId: string, parentMap: Map<string, string>): string[] {
240
+ const path = [traceId]
241
+ let currentTraceId = traceId
242
+ while (parentMap.has(currentTraceId)) {
243
+ currentTraceId = parentMap.get(currentTraceId) as string
244
+ path.unshift(currentTraceId)
245
+ }
246
+ return path
247
+ }
248
+
249
+ /**
250
+ * Finds the nearest common ancestor traceId for a set of element traceIds.
251
+ * If a single traceId is given, returns it directly.
252
+ */
253
+ function findNearestCommonAncestor(traceIds: Set<string>, parentMap: Map<string, string>): string {
254
+ const traceIdList = [...traceIds]
255
+ if (traceIdList.length === 1) return traceIdList[0]
256
+
257
+ const paths = traceIdList.map((traceId) => pathFromRoot(traceId, parentMap))
258
+ const shortestPathLength = Math.min(...paths.map((path) => path.length))
259
+
260
+ let nearestCommonAncestorTraceId = paths[0][0]
261
+ for (let depth = 0; depth < shortestPathLength; depth++) {
262
+ const candidateTraceId = paths[0][depth]
263
+ if (paths.every((path) => path[depth] === candidateTraceId)) {
264
+ nearestCommonAncestorTraceId = candidateTraceId
265
+ } else {
266
+ break
267
+ }
268
+ }
269
+
270
+ return nearestCommonAncestorTraceId
271
+ }
272
+
273
+ /**
274
+ * Finds the traceId of the first element in the tree whose matched CSS rules
275
+ * define the given custom property (fallback when var() usage is not tracked).
276
+ */
277
+ function findDefiningElement(elements: ExtractedElement[], varName: string): string | undefined {
278
+ for (const element of elements) {
279
+ const matcherData = element.extractorData.get('css-matcher') as MatchedCssData | undefined
280
+ if (matcherData?.customProperties[varName] !== undefined) return element.traceId
281
+ const childResult = findDefiningElement(element.children, varName)
282
+ if (childResult !== undefined) return childResult
283
+ }
284
+ return undefined
285
+ }
286
+
287
+ // ─────────────────────────────────────────────────────────────────────────────
288
+ // CSS Properties helpers
289
+ // ─────────────────────────────────────────────────────────────────────────────
290
+
110
291
  /**
111
292
  * Returns the CSS property values matched for this specific element's selector(s).
112
293
  * Reads from css-matcher.matches which is populated by matchCssSelectors.
@@ -146,30 +327,3 @@ function buildCssProperties(element: ExtractedElement | undefined): Record<strin
146
327
 
147
328
  return result
148
329
  }
149
-
150
- /**
151
- * Build cssCustomProperties for a single element from this element's matched rules only.
152
- * Only includes --vars that were declared in a rule that matched this element's selector(s).
153
- */
154
- function buildCssCustomPropertiesForElement(
155
- element: ExtractedElement | undefined,
156
- ): Record<string, CssCustomPropertyItem> {
157
- const matcherData = element?.extractorData.get('css-matcher') as MatchedCssData | undefined
158
- const customProperties = matcherData?.customProperties
159
- if (!customProperties || Object.keys(customProperties).length === 0) {
160
- return {}
161
- }
162
-
163
- const result: Record<string, CssCustomPropertyItem> = {}
164
- for (const [name, value] of Object.entries(customProperties)) {
165
- // Skip --display (handled in regular properties)
166
- if (name === '--display') continue
167
-
168
- // Strip the -- prefix from the name
169
- const cleanName = name.startsWith('--') ? name.slice(2) : name
170
- result[cleanName] = {
171
- defaultValue: value,
172
- }
173
- }
174
- return result
175
- }
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import type { EditorReactComponent } from '@wix/zero-config-schema'
1
2
  import { Result, ResultAsync } from 'neverthrow'
2
3
  import type { ComponentType } from 'react'
3
- import type { EditorReactComponent } from './schema'
4
4
 
5
5
  import { extractAllComponentInfo, extractCssImports, extractDefaultComponentInfo } from './information-extractors/ts'
6
6
  import type { ComponentInfo } from './information-extractors/ts'
@@ -23,7 +23,7 @@ export type {
23
23
  ProcessComponentResult,
24
24
  } from './manifest-pipeline'
25
25
 
26
- export type { EditorReactComponent } from './schema'
26
+ export type { EditorReactComponent } from '@wix/zero-config-schema'
27
27
 
28
28
  // Converter
29
29
  import { toEditorReactComponent } from './converters'