@embeddable.com/sdk-react 3.2.0-next.9 → 3.2.1

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,10 +1,11 @@
1
1
  import { ModuleInfo } from "rollup";
2
- declare const _default: ({ globalKey, outputDir, fileName, componentFileRegex, searchEntry, }: {
2
+ declare const _default: ({ globalKey, outputDir, fileName, componentFileRegex, isDev, searchEntry, }: {
3
3
  globalKey: string;
4
4
  outputDir: string;
5
5
  fileName: string;
6
6
  componentFileRegex: RegExp;
7
- searchEntry?: string | undefined;
7
+ isDev?: boolean;
8
+ searchEntry?: string;
8
9
  }) => {
9
10
  name: string;
10
11
  moduleParsed: (moduleInfo: ModuleInfo) => Promise<void>;
package/lib/index.esm.js CHANGED
@@ -7,7 +7,9 @@ import viteReactPlugin from '@vitejs/plugin-react';
7
7
  import * as fs from 'node:fs/promises';
8
8
  import { readdir, lstat, rm } from 'node:fs/promises';
9
9
  import * as url from 'node:url';
10
+ import { GlobalRegistrator } from '@happy-dom/global-registrator';
10
11
  import 'node:child_process';
12
+ import * as crypto from 'node:crypto';
11
13
  import { z } from 'zod';
12
14
  import { parse } from '@babel/parser';
13
15
  import traverse from '@babel/traverse';
@@ -25,45 +27,16 @@ var createContext = (pluginRoot, coreCtx) => {
25
27
  };
26
28
  };
27
29
 
30
+ GlobalRegistrator.register({
31
+ url: "http://localhost:3000",
32
+ width: 1920,
33
+ height: 1080,
34
+ });
28
35
  const loadComponentMeta = async (moduleId) => {
29
36
  const module = await import(url.pathToFileURL(moduleId).href);
30
37
  return module.meta;
31
38
  };
32
39
 
33
- /**
34
- * TODO: for some reason code from @embeddable.com/extract-components-config
35
- * is being cached. Thus we use this code duplication in place. Please investigate and fix.
36
- */
37
- var extractComponentsConfigPlugin = ({ globalKey, outputDir, fileName, componentFileRegex, searchEntry = "", }) => {
38
- let configs = [];
39
- return {
40
- name: "extract-components-config",
41
- moduleParsed: async (moduleInfo) => {
42
- var _a;
43
- if (componentFileRegex.test(moduleInfo.id) &&
44
- ((_a = moduleInfo.code) === null || _a === void 0 ? void 0 : _a.includes(searchEntry))) {
45
- const meta = await loadComponentMeta(moduleInfo.id);
46
- const configJSON = JSON.stringify(meta, (_key, value) => typeof value === "object" &&
47
- value !== null &&
48
- "__embeddableType" in value
49
- ? value.toString()
50
- : value);
51
- configs.push(configJSON);
52
- }
53
- },
54
- buildEnd: async () => {
55
- const template = `
56
- globalThis.__EMBEDDABLE__ = globalThis.__EMBEDDABLE__ || {};
57
- globalThis.__EMBEDDABLE__.${globalKey} = globalThis.__EMBEDDABLE__.${globalKey} || [
58
- __PLACEHOLDER__
59
- ];
60
- `;
61
- await fs.writeFile(`${outputDir}/${fileName}`, template.replace("__PLACEHOLDER__", configs.filter(Boolean).join(",\n")));
62
- configs = [];
63
- },
64
- };
65
- };
66
-
67
40
  var findFiles = async (initialSrcDir, regex) => {
68
41
  const filesList = [];
69
42
  async function findFilesRec(srcDir) {
@@ -270,6 +243,11 @@ class ZodError extends Error {
270
243
  processError(this);
271
244
  return fieldErrors;
272
245
  }
246
+ static assert(value) {
247
+ if (!(value instanceof ZodError)) {
248
+ throw new Error(`Not a ZodError: ${value}`);
249
+ }
250
+ }
273
251
  toString() {
274
252
  return this.message;
275
253
  }
@@ -301,6 +279,11 @@ ZodError.create = (issues) => {
301
279
  const error = new ZodError(issues);
302
280
  return error;
303
281
  };
282
+
283
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
284
+ var e = new Error(message);
285
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
286
+ };
304
287
 
305
288
  var errorUtil;
306
289
  (function (errorUtil) {
@@ -376,6 +359,63 @@ const formatErrorPath = (path) => {
376
359
  return formatted;
377
360
  };
378
361
 
362
+ /**
363
+ * Get the hash of the content string. It returns the first 5 characters of the hash
364
+ * Example: getContentHash("Hello World")
365
+ * @param contentString The content string to hash
366
+ * @returns
367
+ */
368
+ const getContentHash = (contentString) => {
369
+ return crypto
370
+ .createHash("md5")
371
+ .update(contentString)
372
+ .digest("hex")
373
+ .substring(0, 5);
374
+ };
375
+
376
+ /**
377
+ * TODO: for some reason code from @embeddable.com/extract-components-config
378
+ * is being cached. Thus we use this code duplication in place. Please investigate and fix.
379
+ */
380
+ var extractComponentsConfigPlugin = ({ globalKey, outputDir, fileName, componentFileRegex, isDev = false, searchEntry = "", }) => {
381
+ let configs = [];
382
+ return {
383
+ name: "extract-components-config",
384
+ moduleParsed: async (moduleInfo) => {
385
+ var _a;
386
+ if (componentFileRegex.test(moduleInfo.id) &&
387
+ ((_a = moduleInfo.code) === null || _a === void 0 ? void 0 : _a.includes(searchEntry))) {
388
+ const meta = await loadComponentMeta(moduleInfo.id);
389
+ const configJSON = JSON.stringify(meta, (_key, value) => typeof value === "object" &&
390
+ value !== null &&
391
+ "__embeddableType" in value
392
+ ? value.toString()
393
+ : value);
394
+ configs.push(configJSON);
395
+ }
396
+ },
397
+ buildEnd: async () => {
398
+ const template = `
399
+ globalThis.__EMBEDDABLE__ = globalThis.__EMBEDDABLE__ || {};
400
+ globalThis.__EMBEDDABLE__.${globalKey} = globalThis.__EMBEDDABLE__.${globalKey} || [
401
+ __PLACEHOLDER__
402
+ ];
403
+ `;
404
+ // sort to make sure the hash is consistent
405
+ configs.sort((a, b) => a.localeCompare(b));
406
+ const contentString = configs.filter(Boolean).join(",\n");
407
+ let newFileName = fileName;
408
+ if (!isDev) {
409
+ const fileHash = getContentHash(contentString);
410
+ const fileHashExtention = `-${fileHash}.js`;
411
+ newFileName = `${fileName.replace(".js", fileHashExtention)}`;
412
+ }
413
+ await fs.writeFile(`${outputDir}/${newFileName}`, template.replace("__PLACEHOLDER__", contentString));
414
+ configs = [];
415
+ },
416
+ };
417
+ };
418
+
379
419
  /******************************************************************************
380
420
  Copyright (c) Microsoft Corporation.
381
421
 
@@ -485,6 +525,9 @@ createBuiltInType("time", {
485
525
  });
486
526
  createBuiltInType("timeRange", {
487
527
  transform: function (value) {
528
+ // Return undefined instead of a null populated object
529
+ if (!value)
530
+ return undefined;
488
531
  var _a = [value === null || value === void 0 ? void 0 : value.from, value === null || value === void 0 ? void 0 : value.to], from = _a[0], to = _a[1];
489
532
  var fromDate = new Date(from);
490
533
  var toDate = new Date(to);
@@ -1037,6 +1080,7 @@ async function runViteBuild(ctx, watch = null) {
1037
1080
  plugins: [
1038
1081
  viteReactPlugin(),
1039
1082
  extractComponentsConfigPlugin({
1083
+ isDev: !!watch,
1040
1084
  globalKey: "componentsMeta",
1041
1085
  fileName: "embeddable-components-meta.js",
1042
1086
  outputDir: ctx.client.buildDir,
@@ -1044,6 +1088,7 @@ async function runViteBuild(ctx, watch = null) {
1044
1088
  searchEntry: "defineComponent",
1045
1089
  }),
1046
1090
  extractComponentsConfigPlugin({
1091
+ isDev: !!watch,
1047
1092
  globalKey: "editorsMeta",
1048
1093
  fileName: "embeddable-editors-meta.js",
1049
1094
  outputDir: ctx.client.buildDir,