@embeddable.com/sdk-react 4.0.10 → 4.1.0-next.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.esm.js
CHANGED
|
@@ -13,7 +13,7 @@ import 'node:child_process';
|
|
|
13
13
|
import * as crypto from 'node:crypto';
|
|
14
14
|
import { z } from 'zod';
|
|
15
15
|
import { parse } from '@babel/parser';
|
|
16
|
-
import
|
|
16
|
+
import traverseModule from '@babel/traverse';
|
|
17
17
|
import * as fs$1 from 'fs';
|
|
18
18
|
import { existsSync } from 'node:fs';
|
|
19
19
|
import dts from 'vite-plugin-dts';
|
|
@@ -464,6 +464,7 @@ const getComponentLibraryConfig = (componentLibrary) => {
|
|
|
464
464
|
};
|
|
465
465
|
const EXTERNAL_LIBRARY_META_FILE_NAME = "embeddable-components.json";
|
|
466
466
|
const getComponentLibraryMeta = async (ctx, componentLibrary) => {
|
|
467
|
+
var _a;
|
|
467
468
|
const { libraryName, include, exclude } = getComponentLibraryConfig(componentLibrary);
|
|
468
469
|
const libraryMeta = (await loadJson(getLibraryPath(ctx, libraryName, EXTERNAL_LIBRARY_META_FILE_NAME)));
|
|
469
470
|
const filterItems = (items) => {
|
|
@@ -477,6 +478,7 @@ const getComponentLibraryMeta = async (ctx, componentLibrary) => {
|
|
|
477
478
|
...libraryMeta,
|
|
478
479
|
components: filterItems(libraryMeta.components),
|
|
479
480
|
editors: filterItems(libraryMeta.editors),
|
|
481
|
+
previews: filterItems((_a = libraryMeta.previews) !== null && _a !== void 0 ? _a : []),
|
|
480
482
|
};
|
|
481
483
|
};
|
|
482
484
|
const getLibraryPath = (ctx, libraryName, fileName) => path.resolve(ctx.client.rootDir, "node_modules", libraryName, "dist", fileName);
|
|
@@ -836,7 +838,7 @@ const componentMetaSchema = z
|
|
|
836
838
|
});
|
|
837
839
|
|
|
838
840
|
// @ts-ignore
|
|
839
|
-
const babelTraverse =
|
|
841
|
+
const babelTraverse = traverseModule.default;
|
|
840
842
|
const parseAndTraverse = (code, visitor) => {
|
|
841
843
|
const ast = parse(code || "", {
|
|
842
844
|
sourceType: "module",
|
|
@@ -1301,17 +1303,59 @@ const validateComponentDuplicates = (filesList, additionalComponents, ora) => {
|
|
|
1301
1303
|
return errors;
|
|
1302
1304
|
};
|
|
1303
1305
|
|
|
1306
|
+
const traverse =
|
|
1307
|
+
// @ts-ignore
|
|
1308
|
+
typeof traverseModule === "function" ? traverseModule : traverseModule.default;
|
|
1309
|
+
function findFilesWithPreviewExport(files) {
|
|
1310
|
+
const result = [];
|
|
1311
|
+
for (const [name, filePath] of files) {
|
|
1312
|
+
let ast;
|
|
1313
|
+
try {
|
|
1314
|
+
const code = fs$1.readFileSync(filePath, "utf-8");
|
|
1315
|
+
ast = parse(code, {
|
|
1316
|
+
sourceType: "module",
|
|
1317
|
+
plugins: ["typescript", "jsx"],
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
catch (err) {
|
|
1321
|
+
console.warn(`⚠️ Failed to parse ${filePath}: ${err.message}`);
|
|
1322
|
+
continue;
|
|
1323
|
+
}
|
|
1324
|
+
let hasPreviewExport = false;
|
|
1325
|
+
traverse(ast, {
|
|
1326
|
+
ExportNamedDeclaration(path) {
|
|
1327
|
+
var _a, _b;
|
|
1328
|
+
const { node } = path;
|
|
1329
|
+
if (((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.type) === "VariableDeclaration") {
|
|
1330
|
+
if (node.declaration.declarations.some((d) => d.id.type === "Identifier" && d.id.name === "preview")) {
|
|
1331
|
+
hasPreviewExport = true;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
if ((_b = node.specifiers) === null || _b === void 0 ? void 0 : _b.some((s) => s.exported.type === "Identifier" && s.exported.name === "preview")) {
|
|
1335
|
+
hasPreviewExport = true;
|
|
1336
|
+
}
|
|
1337
|
+
},
|
|
1338
|
+
});
|
|
1339
|
+
if (hasPreviewExport) {
|
|
1340
|
+
result.push([name, filePath]);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
return result;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1304
1346
|
const EMB_FILE_REGEX = /^(.*)(?<!\.type|\.options)\.emb\.[jt]s$/;
|
|
1305
1347
|
var generate = async (ctx) => {
|
|
1306
1348
|
var _a;
|
|
1307
1349
|
await usageValidator(ctx.client.srcDir);
|
|
1308
1350
|
const filesList = await findFiles(ctx.client.srcDir, EMB_FILE_REGEX);
|
|
1309
|
-
const additionalComponents = await getAdditionalComponents$1(ctx);
|
|
1351
|
+
const { additionalComponents, additionalPreviews } = await getAdditionalComponents$1(ctx);
|
|
1352
|
+
const previewComponents = findFilesWithPreviewExport(filesList);
|
|
1310
1353
|
const errors = validateComponentDuplicates(filesList, additionalComponents, ora);
|
|
1311
1354
|
if (errors.length) {
|
|
1312
1355
|
process.exit(1);
|
|
1313
1356
|
}
|
|
1314
|
-
|
|
1357
|
+
ctx.client.componentsWithPreview = Object.values(additionalPreviews).flat().concat(previewComponents.map((c) => c[0]));
|
|
1358
|
+
await prepareEntrypoint(ctx, filesList, additionalComponents, previewComponents, additionalPreviews);
|
|
1315
1359
|
let result;
|
|
1316
1360
|
if ((_a = ctx.dev) === null || _a === void 0 ? void 0 : _a.watch) {
|
|
1317
1361
|
result = await runViteWatch(ctx);
|
|
@@ -1461,12 +1505,21 @@ function getRelativeFileNameForImport(ctx, fileName) {
|
|
|
1461
1505
|
// but for imports it must be '.src/something/something'
|
|
1462
1506
|
.replaceAll("\\", "/")}`;
|
|
1463
1507
|
}
|
|
1464
|
-
async function prepareEntrypoint(ctx, filesList, additionalComponents) {
|
|
1465
|
-
const additionalComponentsImports = getAdditionalEntryPointImports(additionalComponents);
|
|
1508
|
+
async function prepareEntrypoint(ctx, filesList, additionalComponents, previewComponents, additionalPreviews) {
|
|
1509
|
+
const additionalComponentsImports = getAdditionalEntryPointImports(additionalComponents, additionalPreviews);
|
|
1466
1510
|
const repositoryImports = filesList
|
|
1467
1511
|
.map(([fileName, filePath]) => `\t${fileName}: React.lazy(() => import('${getRelativeFileNameForImport(ctx, filePath)}'))`)
|
|
1468
1512
|
.join(",\n");
|
|
1469
|
-
const
|
|
1513
|
+
const repositoryPreviewImports = previewComponents
|
|
1514
|
+
.map(([fileName, filePath]) => `\t${fileName}_preview: React.lazy(() => import('${getRelativeFileNameForImport(ctx, filePath)}').then(module => ({
|
|
1515
|
+
default: module.preview
|
|
1516
|
+
})))`)
|
|
1517
|
+
.join(",\n");
|
|
1518
|
+
const imports = [
|
|
1519
|
+
additionalComponentsImports,
|
|
1520
|
+
repositoryImports,
|
|
1521
|
+
repositoryPreviewImports,
|
|
1522
|
+
]
|
|
1470
1523
|
.filter(Boolean)
|
|
1471
1524
|
.join(",\n");
|
|
1472
1525
|
let errorBoundaryImport = "";
|
|
@@ -1531,8 +1584,9 @@ const findExternalComponents = async (ctx) => {
|
|
|
1531
1584
|
const getAdditionalComponents$1 = async (ctx) => {
|
|
1532
1585
|
const componentLibraries = ctx.client.componentLibraries || [];
|
|
1533
1586
|
let additionalComponents = {};
|
|
1587
|
+
let additionalPreviews = {};
|
|
1534
1588
|
for (const componentLibrary of componentLibraries) {
|
|
1535
|
-
const { libraryName} = getComponentLibraryConfig(componentLibrary);
|
|
1589
|
+
const { libraryName } = getComponentLibraryConfig(componentLibrary);
|
|
1536
1590
|
try {
|
|
1537
1591
|
const libMeta = await getComponentLibraryMeta(ctx, componentLibrary);
|
|
1538
1592
|
if (libMeta.plugin !== "react") {
|
|
@@ -1542,23 +1596,30 @@ const getAdditionalComponents$1 = async (ctx) => {
|
|
|
1542
1596
|
...libMeta.components,
|
|
1543
1597
|
...libMeta.editors,
|
|
1544
1598
|
];
|
|
1599
|
+
additionalPreviews[libraryName] = libMeta.previews || [];
|
|
1545
1600
|
}
|
|
1546
1601
|
catch (e) {
|
|
1547
1602
|
console.error(`Can't load component library: ${libraryName}`, e);
|
|
1548
1603
|
throw e;
|
|
1549
1604
|
}
|
|
1550
1605
|
}
|
|
1551
|
-
return additionalComponents;
|
|
1606
|
+
return { additionalComponents, additionalPreviews };
|
|
1552
1607
|
};
|
|
1553
|
-
const getAdditionalEntryPointImports = (additionalComponents) => {
|
|
1554
|
-
|
|
1555
|
-
.map(([library, components]) =>
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1608
|
+
const getAdditionalEntryPointImports = (additionalComponents, additionalPreviews) => {
|
|
1609
|
+
const componentImports = Object.entries(additionalComponents)
|
|
1610
|
+
.map(([library, components]) => components
|
|
1611
|
+
.map((component) => `\t${component}: React.lazy(() => import('${library}/dist/${component}'))`)
|
|
1612
|
+
.join(",\n"))
|
|
1613
|
+
.join(",\n");
|
|
1614
|
+
const previewImports = Object.entries(additionalPreviews)
|
|
1615
|
+
.map(([library, previews]) => previews
|
|
1616
|
+
.map((preview) => `\t${preview}_preview: React.lazy(() => import('${library}/dist/${preview}').then(module => ({
|
|
1617
|
+
default: module.preview
|
|
1618
|
+
})))`)
|
|
1619
|
+
.join(",\n"))
|
|
1620
|
+
.join(",\n");
|
|
1621
|
+
return [componentImports, previewImports]
|
|
1622
|
+
.filter(Boolean)
|
|
1562
1623
|
.join(",\n");
|
|
1563
1624
|
};
|
|
1564
1625
|
|
|
@@ -1585,6 +1646,7 @@ async function buildPackage$1(ctx) {
|
|
|
1585
1646
|
: undefined;
|
|
1586
1647
|
const componentFiles = {};
|
|
1587
1648
|
const editorFiles = {};
|
|
1649
|
+
const previewComponents = [];
|
|
1588
1650
|
if (indexFiles.length > 0) {
|
|
1589
1651
|
inputEntries["index"] = indexFiles[0];
|
|
1590
1652
|
}
|
|
@@ -1619,6 +1681,7 @@ async function buildPackage$1(ctx) {
|
|
|
1619
1681
|
const libMeta = {
|
|
1620
1682
|
components: Object.keys(componentFiles).concat(additionalComponents.components),
|
|
1621
1683
|
editors: Object.keys(editorFiles).concat(additionalComponents.editors),
|
|
1684
|
+
previews: previewComponents.concat(additionalComponents.previews),
|
|
1622
1685
|
plugin: "react",
|
|
1623
1686
|
};
|
|
1624
1687
|
const jsonContent = JSON.stringify(libMeta, null, 2);
|
|
@@ -1629,7 +1692,7 @@ async function buildPackage$1(ctx) {
|
|
|
1629
1692
|
});
|
|
1630
1693
|
},
|
|
1631
1694
|
},
|
|
1632
|
-
groupComponents(componentFiles, editorFiles),
|
|
1695
|
+
groupComponents(componentFiles, editorFiles, previewComponents),
|
|
1633
1696
|
],
|
|
1634
1697
|
build: {
|
|
1635
1698
|
sourcemap: true,
|
|
@@ -1650,7 +1713,7 @@ async function buildPackage$1(ctx) {
|
|
|
1650
1713
|
});
|
|
1651
1714
|
progress.succeed("Building React library completed");
|
|
1652
1715
|
}
|
|
1653
|
-
const groupComponents = (components, editors) => ({
|
|
1716
|
+
const groupComponents = (components, editors, previewComponents) => ({
|
|
1654
1717
|
name: "group-components",
|
|
1655
1718
|
moduleParsed(moduleInfo) {
|
|
1656
1719
|
if (!moduleInfo ||
|
|
@@ -1660,13 +1723,14 @@ const groupComponents = (components, editors) => ({
|
|
|
1660
1723
|
return;
|
|
1661
1724
|
try {
|
|
1662
1725
|
moduleInfo.ast.body.forEach((node) => {
|
|
1726
|
+
var _a, _b;
|
|
1727
|
+
const isTs = moduleInfo.id.endsWith(".ts");
|
|
1728
|
+
const basename = path.basename(moduleInfo.id, `.emb.${isTs ? "ts" : "js"}`);
|
|
1663
1729
|
if (node.type === "ExportDefaultDeclaration") {
|
|
1664
1730
|
const calleeName = node.declaration.type === "CallExpression" &&
|
|
1665
1731
|
node.declaration.callee.type === "Identifier"
|
|
1666
1732
|
? node.declaration.callee.name
|
|
1667
1733
|
: null;
|
|
1668
|
-
const isTs = moduleInfo.id.endsWith(".ts");
|
|
1669
|
-
const basename = path.basename(moduleInfo.id, `.emb.${isTs ? "ts" : "js"}`);
|
|
1670
1734
|
if (calleeName === "defineComponent") {
|
|
1671
1735
|
components[basename] = moduleInfo.id;
|
|
1672
1736
|
}
|
|
@@ -1674,6 +1738,17 @@ const groupComponents = (components, editors) => ({
|
|
|
1674
1738
|
editors[basename] = moduleInfo.id;
|
|
1675
1739
|
}
|
|
1676
1740
|
}
|
|
1741
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
1742
|
+
if (((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.type) === "VariableDeclaration") {
|
|
1743
|
+
if (node.declaration.declarations.some((d) => d.id.type === "Identifier" && d.id.name === "preview")) {
|
|
1744
|
+
previewComponents.push(basename);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
if ((_b = node.specifiers) === null || _b === void 0 ? void 0 : _b.some((s) => s.exported.type === "Identifier" &&
|
|
1748
|
+
s.exported.name === "preview")) {
|
|
1749
|
+
previewComponents.push(basename);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1677
1752
|
});
|
|
1678
1753
|
}
|
|
1679
1754
|
catch (error) {
|
|
@@ -1718,6 +1793,7 @@ const getAdditionalComponents = async (ctx) => {
|
|
|
1718
1793
|
const allAdditionalComponents = {};
|
|
1719
1794
|
const components = [];
|
|
1720
1795
|
const editors = [];
|
|
1796
|
+
const previews = [];
|
|
1721
1797
|
for (const componentLibrary of componentLibraries) {
|
|
1722
1798
|
const { libraryName } = getComponentLibraryConfig(componentLibrary);
|
|
1723
1799
|
try {
|
|
@@ -1730,14 +1806,14 @@ const getAdditionalComponents = async (ctx) => {
|
|
|
1730
1806
|
...libMeta.editors,
|
|
1731
1807
|
];
|
|
1732
1808
|
components.push(...libMeta.components);
|
|
1733
|
-
|
|
1809
|
+
previews.push(...libMeta.previews);
|
|
1734
1810
|
}
|
|
1735
1811
|
catch (e) {
|
|
1736
1812
|
console.error(`Can't load component library: ${libraryName}`, e);
|
|
1737
1813
|
throw e;
|
|
1738
1814
|
}
|
|
1739
1815
|
}
|
|
1740
|
-
return { components, editors, allAdditionalComponents };
|
|
1816
|
+
return { components, editors, previews, allAdditionalComponents };
|
|
1741
1817
|
};
|
|
1742
1818
|
|
|
1743
1819
|
var build = async (ctx) => {
|