@webstudio-is/react-sdk 0.133.0 → 0.134.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.
package/lib/index.js CHANGED
@@ -1337,7 +1337,8 @@ import { parseComponentName } from "@webstudio-is/sdk";
1337
1337
  var generateAction = ({
1338
1338
  scope,
1339
1339
  prop,
1340
- dataSources
1340
+ dataSources,
1341
+ usedDataSources
1341
1342
  }) => {
1342
1343
  const setters = /* @__PURE__ */ new Set();
1343
1344
  let args = [];
@@ -1354,6 +1355,7 @@ var generateAction = ({
1354
1355
  const depId = decodeDataSourceVariable(identifier);
1355
1356
  const dep = depId ? dataSources.get(depId) : void 0;
1356
1357
  if (dep) {
1358
+ usedDataSources.set(dep.id, dep);
1357
1359
  if (assignee) {
1358
1360
  setters.add(dep);
1359
1361
  }
@@ -1389,7 +1391,8 @@ var generateAction = ({
1389
1391
  var generatePropValue = ({
1390
1392
  scope,
1391
1393
  prop,
1392
- dataSources
1394
+ dataSources,
1395
+ usedDataSources
1393
1396
  }) => {
1394
1397
  if (prop.type === "asset" || prop.type === "page") {
1395
1398
  return;
@@ -1402,17 +1405,19 @@ var generatePropValue = ({
1402
1405
  if (dataSource === void 0) {
1403
1406
  return;
1404
1407
  }
1408
+ usedDataSources.set(dataSource.id, dataSource);
1405
1409
  return scope.getName(dataSource.id, dataSource.name);
1406
1410
  }
1407
1411
  if (prop.type === "expression") {
1408
1412
  return generateExpression({
1409
1413
  expression: prop.value,
1410
1414
  dataSources,
1415
+ usedDataSources,
1411
1416
  scope
1412
1417
  });
1413
1418
  }
1414
1419
  if (prop.type === "action") {
1415
- return generateAction({ scope, prop, dataSources });
1420
+ return generateAction({ scope, prop, dataSources, usedDataSources });
1416
1421
  }
1417
1422
  prop;
1418
1423
  };
@@ -1421,6 +1426,7 @@ var generateJsxElement = ({
1421
1426
  instance,
1422
1427
  props,
1423
1428
  dataSources,
1429
+ usedDataSources,
1424
1430
  indexesWithinAncestors,
1425
1431
  children,
1426
1432
  classesMap
@@ -1445,7 +1451,12 @@ ${indexAttribute}="${index}"`;
1445
1451
  if (prop.instanceId !== instance.id) {
1446
1452
  continue;
1447
1453
  }
1448
- const propValue = generatePropValue({ scope, prop, dataSources });
1454
+ const propValue = generatePropValue({
1455
+ scope,
1456
+ prop,
1457
+ dataSources,
1458
+ usedDataSources
1459
+ });
1449
1460
  if (prop.name === showAttribute) {
1450
1461
  if (propValue === "true") {
1451
1462
  continue;
@@ -1525,6 +1536,7 @@ var generateJsxChildren = ({
1525
1536
  instances,
1526
1537
  props,
1527
1538
  dataSources,
1539
+ usedDataSources,
1528
1540
  indexesWithinAncestors,
1529
1541
  classesMap
1530
1542
  }) => {
@@ -1540,6 +1552,7 @@ var generateJsxChildren = ({
1540
1552
  const expression = generateExpression({
1541
1553
  expression: child.value,
1542
1554
  dataSources,
1555
+ usedDataSources,
1543
1556
  scope
1544
1557
  });
1545
1558
  generatedChildren = `{${expression}}
@@ -1557,6 +1570,7 @@ var generateJsxChildren = ({
1557
1570
  instance,
1558
1571
  props,
1559
1572
  dataSources,
1573
+ usedDataSources,
1560
1574
  indexesWithinAncestors,
1561
1575
  classesMap,
1562
1576
  children: generateJsxChildren({
@@ -1566,6 +1580,7 @@ var generateJsxChildren = ({
1566
1580
  instances,
1567
1581
  props,
1568
1582
  dataSources,
1583
+ usedDataSources,
1569
1584
  indexesWithinAncestors
1570
1585
  })
1571
1586
  });
@@ -1590,17 +1605,36 @@ var generateWebstudioComponent = ({
1590
1605
  if (instance === void 0) {
1591
1606
  return "";
1592
1607
  }
1608
+ const usedDataSources = /* @__PURE__ */ new Map();
1609
+ const generatedJsx = generateJsxElement({
1610
+ scope,
1611
+ instance,
1612
+ props,
1613
+ dataSources,
1614
+ usedDataSources,
1615
+ indexesWithinAncestors,
1616
+ classesMap,
1617
+ children: generateJsxChildren({
1618
+ scope,
1619
+ children: instance.children,
1620
+ instances,
1621
+ props,
1622
+ dataSources,
1623
+ usedDataSources,
1624
+ indexesWithinAncestors,
1625
+ classesMap
1626
+ })
1627
+ });
1593
1628
  let generatedProps = "";
1594
1629
  if (parameters.length > 0) {
1595
1630
  let generatedPropsValue = "{ ";
1596
1631
  let generatedPropsType = "{ ";
1597
1632
  for (const parameter of parameters) {
1598
- const dataSource = dataSources.get(parameter.value);
1599
- if (dataSource === void 0) {
1600
- continue;
1633
+ const dataSource = usedDataSources.get(parameter.value);
1634
+ if (dataSource) {
1635
+ const valueName = scope.getName(dataSource.id, dataSource.name);
1636
+ generatedPropsValue += `${parameter.name}: ${valueName}, `;
1601
1637
  }
1602
- const valueName = scope.getName(dataSource.id, dataSource.name);
1603
- generatedPropsValue += `${parameter.name}: ${valueName}, `;
1604
1638
  generatedPropsType += `${parameter.name}: any; `;
1605
1639
  }
1606
1640
  generatedPropsValue += `}`;
@@ -1608,7 +1642,7 @@ var generateWebstudioComponent = ({
1608
1642
  generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
1609
1643
  }
1610
1644
  let generatedDataSources = "";
1611
- for (const dataSource of dataSources.values()) {
1645
+ for (const dataSource of usedDataSources.values()) {
1612
1646
  if (dataSource.type === "variable") {
1613
1647
  const valueName = scope.getName(dataSource.id, dataSource.name);
1614
1648
  const setterName = scope.getName(
@@ -1631,23 +1665,6 @@ var generateWebstudioComponent = ({
1631
1665
  `;
1632
1666
  }
1633
1667
  }
1634
- const generatedJsx = generateJsxElement({
1635
- scope,
1636
- instance,
1637
- props,
1638
- dataSources,
1639
- indexesWithinAncestors,
1640
- classesMap,
1641
- children: generateJsxChildren({
1642
- scope,
1643
- children: instance.children,
1644
- instances,
1645
- props,
1646
- dataSources,
1647
- indexesWithinAncestors,
1648
- classesMap
1649
- })
1650
- });
1651
1668
  let generatedComponent = "";
1652
1669
  generatedComponent += `const ${name} = (${generatedProps}) => {
1653
1670
  `;
@@ -1665,25 +1682,11 @@ var generateResourcesLoader = ({
1665
1682
  dataSources,
1666
1683
  resources
1667
1684
  }) => {
1668
- let generatedVariables = "";
1669
1685
  let generatedOutput = "";
1670
1686
  let generatedLoaders = "";
1671
1687
  let hasResources = false;
1688
+ const usedDataSources = /* @__PURE__ */ new Map();
1672
1689
  for (const dataSource of dataSources.values()) {
1673
- if (dataSource.type === "variable") {
1674
- const name = scope.getName(dataSource.id, dataSource.name);
1675
- const value = JSON.stringify(dataSource.value.value);
1676
- generatedVariables += `let ${name} = ${value}
1677
- `;
1678
- }
1679
- if (dataSource.type === "parameter") {
1680
- if (dataSource.id !== page.pathVariableId) {
1681
- continue;
1682
- }
1683
- const name = scope.getName(dataSource.id, dataSource.name);
1684
- generatedVariables += `const ${name} = _props.params
1685
- `;
1686
- }
1687
1690
  if (dataSource.type === "resource") {
1688
1691
  const resource = resources.get(dataSource.resourceId);
1689
1692
  if (resource === void 0) {
@@ -1702,6 +1705,7 @@ var generateResourcesLoader = ({
1702
1705
  const url = generateExpression({
1703
1706
  expression: resource.url,
1704
1707
  dataSources,
1708
+ usedDataSources,
1705
1709
  scope
1706
1710
  });
1707
1711
  generatedLoaders += `url: ${url},
@@ -1714,6 +1718,7 @@ var generateResourcesLoader = ({
1714
1718
  const value = generateExpression({
1715
1719
  expression: header.value,
1716
1720
  dataSources,
1721
+ usedDataSources,
1717
1722
  scope
1718
1723
  });
1719
1724
  generatedLoaders += `{ name: "${header.name}", value: ${value} },
@@ -1725,12 +1730,30 @@ var generateResourcesLoader = ({
1725
1730
  const body = generateExpression({
1726
1731
  expression: resource.body,
1727
1732
  dataSources,
1733
+ usedDataSources,
1728
1734
  scope
1729
1735
  });
1730
1736
  generatedLoaders += `body: ${body},
1731
1737
  `;
1732
1738
  }
1733
1739
  generatedLoaders += `}),
1740
+ `;
1741
+ }
1742
+ }
1743
+ let generatedVariables = "";
1744
+ for (const dataSource of usedDataSources.values()) {
1745
+ if (dataSource.type === "variable") {
1746
+ const name = scope.getName(dataSource.id, dataSource.name);
1747
+ const value = JSON.stringify(dataSource.value.value);
1748
+ generatedVariables += `let ${name} = ${value}
1749
+ `;
1750
+ }
1751
+ if (dataSource.type === "parameter") {
1752
+ if (dataSource.id !== page.pathParamsDataSourceId) {
1753
+ continue;
1754
+ }
1755
+ const name = scope.getName(dataSource.id, dataSource.name);
1756
+ generatedVariables += `const ${name} = _props.params
1734
1757
  `;
1735
1758
  }
1736
1759
  }
@@ -1743,8 +1766,8 @@ var generateResourcesLoader = ({
1743
1766
  `;
1744
1767
  generated += `export const loadResources = async (_props: { params: Params }) => {
1745
1768
  `;
1769
+ generated += generatedVariables;
1746
1770
  if (hasResources) {
1747
- generated += generatedVariables;
1748
1771
  generated += `const [
1749
1772
  `;
1750
1773
  generated += generatedOutput;
@@ -1862,7 +1885,7 @@ var generatePageMeta = ({
1862
1885
  continue;
1863
1886
  }
1864
1887
  if (dataSource.type === "parameter") {
1865
- if (dataSource.id === page.pathVariableId) {
1888
+ if (dataSource.id === page.pathParamsDataSourceId) {
1866
1889
  const valueName = localScope.getName(dataSource.id, dataSource.name);
1867
1890
  generated += ` let ${valueName} = params
1868
1891
  `;
@@ -1,6 +1,6 @@
1
1
  import type { Instances, Instance, Props, Scope, DataSources, Prop } from "@webstudio-is/sdk";
2
2
  import type { IndexesWithinAncestors } from "./instance-utils";
3
- export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, classesMap, }: {
3
+ export declare const generateJsxElement: ({ scope, instance, props, dataSources, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
4
4
  scope: Scope;
5
5
  instance: Instance;
6
6
  props: Map<string, {
@@ -117,11 +117,44 @@ export declare const generateJsxElement: ({ scope, instance, props, dataSources,
117
117
  resourceId: string;
118
118
  scopeInstanceId?: string | undefined;
119
119
  }>;
120
+ usedDataSources: Map<string, {
121
+ value: {
122
+ value: number;
123
+ type: "number";
124
+ } | {
125
+ value: string;
126
+ type: "string";
127
+ } | {
128
+ value: boolean;
129
+ type: "boolean";
130
+ } | {
131
+ value: string[];
132
+ type: "string[]";
133
+ } | {
134
+ type: "json";
135
+ value?: unknown;
136
+ };
137
+ type: "variable";
138
+ name: string;
139
+ id: string;
140
+ scopeInstanceId?: string | undefined;
141
+ } | {
142
+ type: "parameter";
143
+ name: string;
144
+ id: string;
145
+ scopeInstanceId?: string | undefined;
146
+ } | {
147
+ type: "resource";
148
+ name: string;
149
+ id: string;
150
+ resourceId: string;
151
+ scopeInstanceId?: string | undefined;
152
+ }>;
120
153
  indexesWithinAncestors: IndexesWithinAncestors;
121
154
  children: string;
122
155
  classesMap?: Map<string, string[]> | undefined;
123
156
  }) => string;
124
- export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
157
+ export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, usedDataSources, indexesWithinAncestors, classesMap, }: {
125
158
  scope: Scope;
126
159
  children: Instance["children"];
127
160
  instances: Map<string, {
@@ -254,6 +287,39 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
254
287
  resourceId: string;
255
288
  scopeInstanceId?: string | undefined;
256
289
  }>;
290
+ usedDataSources: Map<string, {
291
+ value: {
292
+ value: number;
293
+ type: "number";
294
+ } | {
295
+ value: string;
296
+ type: "string";
297
+ } | {
298
+ value: boolean;
299
+ type: "boolean";
300
+ } | {
301
+ value: string[];
302
+ type: "string[]";
303
+ } | {
304
+ type: "json";
305
+ value?: unknown;
306
+ };
307
+ type: "variable";
308
+ name: string;
309
+ id: string;
310
+ scopeInstanceId?: string | undefined;
311
+ } | {
312
+ type: "parameter";
313
+ name: string;
314
+ id: string;
315
+ scopeInstanceId?: string | undefined;
316
+ } | {
317
+ type: "resource";
318
+ name: string;
319
+ id: string;
320
+ resourceId: string;
321
+ scopeInstanceId?: string | undefined;
322
+ }>;
257
323
  indexesWithinAncestors: IndexesWithinAncestors;
258
324
  classesMap?: Map<string, string[]> | undefined;
259
325
  }) => string;
@@ -49,7 +49,7 @@ export declare const generateExpression: ({ expression, dataSources, usedDataSou
49
49
  resourceId: string;
50
50
  scopeInstanceId?: string | undefined;
51
51
  }>;
52
- usedDataSources?: Map<string, {
52
+ usedDataSources: Map<string, {
53
53
  value: {
54
54
  value: number;
55
55
  type: "number";
@@ -81,7 +81,7 @@ export declare const generateExpression: ({ expression, dataSources, usedDataSou
81
81
  id: string;
82
82
  resourceId: string;
83
83
  scopeInstanceId?: string | undefined;
84
- }> | undefined;
84
+ }>;
85
85
  scope: Scope;
86
86
  }) => string;
87
87
  export declare const executeExpression: (expression: undefined | string) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.133.0",
3
+ "version": "0.134.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -16,8 +16,8 @@
16
16
  "type-fest": "^4.3.1",
17
17
  "typescript": "5.2.2",
18
18
  "zod": "^3.21.4",
19
- "@webstudio-is/jest-config": "1.0.7",
20
- "@webstudio-is/tsconfig": "1.0.7"
19
+ "@webstudio-is/tsconfig": "1.0.7",
20
+ "@webstudio-is/jest-config": "1.0.7"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "@remix-run/react": "^1.19.1",
@@ -33,11 +33,11 @@
33
33
  "jsep": "^1.3.8",
34
34
  "nanoid": "^5.0.1",
35
35
  "title-case": "^4.1.0",
36
- "@webstudio-is/css-engine": "0.133.0",
37
- "@webstudio-is/fonts": "0.133.0",
38
- "@webstudio-is/icons": "^0.133.0",
39
- "@webstudio-is/image": "0.133.0",
40
- "@webstudio-is/sdk": "0.133.0"
36
+ "@webstudio-is/css-engine": "0.134.0",
37
+ "@webstudio-is/image": "0.134.0",
38
+ "@webstudio-is/sdk": "0.134.0",
39
+ "@webstudio-is/fonts": "0.134.0",
40
+ "@webstudio-is/icons": "^0.134.0"
41
41
  },
42
42
  "exports": {
43
43
  ".": {