@embeddable.com/sdk-react 2.3.4 → 2.4.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.js CHANGED
@@ -7,12 +7,11 @@ var vite = require('vite');
7
7
  var viteReactPlugin = require('@vitejs/plugin-react');
8
8
  var fs = require('node:fs/promises');
9
9
  var url = require('node:url');
10
- var parser = require('@babel/parser');
11
- var generator = require('@babel/generator');
12
- var traverse = require('@babel/traverse');
10
+ var astring = require('astring');
13
11
  require('node:child_process');
14
12
  var zod = require('zod');
15
- var core = require('@embeddable.com/core');
13
+ var parser = require('@babel/parser');
14
+ var traverse = require('@babel/traverse');
16
15
  var fs$1 = require('fs');
17
16
 
18
17
  function _interopNamespaceDefault(e) {
@@ -52,32 +51,11 @@ var createContext = (pluginRoot, coreCtx) => {
52
51
  };
53
52
  };
54
53
 
55
- // @ts-ignore
56
- const babelTraverse$1 = traverse.default;
57
- // @ts-ignore
58
- const babelGenerate = generator.default;
59
- const CORE_TYPES_IMPORT_REGEX = /@embeddable\.com\/core$/;
60
- const EMB_TYPE_FILE_REGEX = /^(.*)\.type\.emb\.[jt]s$/;
61
- const loadComponentMeta = async (moduleId, code) => {
62
- const ast = parser.parse(code, {
63
- sourceType: "module",
64
- });
65
- babelTraverse$1(ast, {
66
- ImportDeclaration: (path) => {
67
- const isNativeTypesImport = CORE_TYPES_IMPORT_REGEX.test(path.node.source.value);
68
- const isCustomTypeImport = EMB_TYPE_FILE_REGEX.test(path.node.source.value);
69
- if (!(isNativeTypesImport || isCustomTypeImport)) {
70
- path.remove();
71
- }
72
- },
73
- ExportDefaultDeclaration: (path) => {
74
- path.remove();
75
- },
76
- });
54
+ const loadComponentMeta = async (moduleId, ast) => {
77
55
  const tempFilePath = moduleId
78
56
  .replace(".emb.", ".emb-temp.")
79
57
  .replace(/\.ts$/, ".js");
80
- await fs__namespace.writeFile(tempFilePath, babelGenerate(ast).code);
58
+ await fs__namespace.writeFile(tempFilePath, astring.generate(ast));
81
59
  const module = await import(url__namespace.pathToFileURL(tempFilePath).href + `?${Date.now()}`);
82
60
  await fs__namespace.rm(tempFilePath);
83
61
  return module.meta;
@@ -95,7 +73,7 @@ var extractComponentsConfigPlugin = ({ globalKey, outputDir, fileName, component
95
73
  var _a;
96
74
  if (componentFileRegex.test(moduleInfo.id) &&
97
75
  ((_a = moduleInfo.code) === null || _a === void 0 ? void 0 : _a.includes(searchEntry))) {
98
- const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.code);
76
+ const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.ast);
99
77
  const configJSON = JSON.stringify(meta, (_key, value) => typeof value === "object" &&
100
78
  value !== null &&
101
79
  "__embeddableType" in value
@@ -429,6 +407,143 @@ const formatErrorPath = (path) => {
429
407
  return formatted;
430
408
  };
431
409
 
410
+ /******************************************************************************
411
+ Copyright (c) Microsoft Corporation.
412
+
413
+ Permission to use, copy, modify, and/or distribute this software for any
414
+ purpose with or without fee is hereby granted.
415
+
416
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
417
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
418
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
419
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
420
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
421
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
422
+ PERFORMANCE OF THIS SOFTWARE.
423
+ ***************************************************************************** */
424
+ /* global Reflect, Promise, SuppressedError, Symbol */
425
+
426
+
427
+ var __assign = function() {
428
+ __assign = Object.assign || function __assign(t) {
429
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
430
+ s = arguments[i];
431
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
432
+ }
433
+ return t;
434
+ };
435
+ return __assign.apply(this, arguments);
436
+ };
437
+
438
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
439
+ var e = new Error(message);
440
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
441
+ };
442
+
443
+ var DEFAULT_LOCALE = "en-US";
444
+ var createBuiltInType = function (name, typeConfig) {
445
+ if (typeConfig === void 0) { typeConfig = {}; }
446
+ return ({
447
+ __embeddableType: "built-in",
448
+ toString: function () { return name; },
449
+ typeConfig: __assign({ label: name, optionLabel: function () { return name; } }, typeConfig),
450
+ });
451
+ };
452
+ var STRING = "string";
453
+ var NUMBER = "number";
454
+ var BOOLEAN = "boolean";
455
+ var TIME = "time";
456
+ var TIME_RANGE = "timeRange";
457
+ var GRANULARITY = "granularity";
458
+ var DATASET = "dataset";
459
+ var MEASURE = "measure";
460
+ var DIMENSION = "dimension";
461
+ var DIMENSION_OR_MEASURE = "dimensionOrMeasure";
462
+ var ALL_NATIVE_TYPES = [
463
+ STRING,
464
+ NUMBER,
465
+ BOOLEAN,
466
+ TIME,
467
+ TIME_RANGE,
468
+ GRANULARITY,
469
+ DATASET,
470
+ MEASURE,
471
+ DIMENSION,
472
+ DIMENSION_OR_MEASURE,
473
+ ];
474
+
475
+ createBuiltInType("string", {
476
+ transform: function (value) { return value; },
477
+ optionLabel: function (value) {
478
+ return Array.isArray(value)
479
+ ? "[".concat(value.map(function (v) { return "\"".concat(v, "\""); }).join(","), "]")
480
+ : "\"".concat(value, "\"");
481
+ },
482
+ });
483
+ createBuiltInType("number", {
484
+ transform: function (value) {
485
+ return Array.isArray(value) ? value : value ? Number(value) : value;
486
+ },
487
+ optionLabel: function (value) {
488
+ var _a;
489
+ return Array.isArray(value)
490
+ ? "[".concat(value.join(","), "]")
491
+ : (_a = value === null || value === void 0 ? void 0 : value.toLocaleString(DEFAULT_LOCALE)) !== null && _a !== void 0 ? _a : "";
492
+ },
493
+ });
494
+ createBuiltInType("boolean", {
495
+ transform: function (value) { return value === "true" || value === true; },
496
+ optionLabel: function (value) { return (value ? "true" : "false"); },
497
+ });
498
+ createBuiltInType("time", {
499
+ transform: function (value) {
500
+ var date = (value === null || value === void 0 ? void 0 : value.date) ? new Date(value.date) : undefined;
501
+ var isValid = date && date.toString() !== "Invalid Date";
502
+ return {
503
+ date: isValid ? date : undefined,
504
+ relativeTimeString: value === null || value === void 0 ? void 0 : value.relativeTimeString,
505
+ };
506
+ },
507
+ optionLabel: function (value) {
508
+ var _a, _b;
509
+ if (!value)
510
+ return "";
511
+ if (value === null || value === void 0 ? void 0 : value.date) {
512
+ return ((_b = (_a = value.date) === null || _a === void 0 ? void 0 : _a.toLocaleDateString(DEFAULT_LOCALE)) !== null && _b !== void 0 ? _b : value.date.toLocaleString());
513
+ }
514
+ return value.relativeTimeString;
515
+ },
516
+ });
517
+ createBuiltInType("timeRange", {
518
+ transform: function (value) {
519
+ 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];
520
+ var fromDate = new Date(from);
521
+ var toDate = new Date(to);
522
+ return {
523
+ from: fromDate.toString() !== "Invalid Date" ? fromDate : undefined,
524
+ to: toDate.toString() !== "Invalid Date" ? toDate : undefined,
525
+ relativeTimeString: value === null || value === void 0 ? void 0 : value.relativeTimeString,
526
+ };
527
+ },
528
+ optionLabel: function (value) {
529
+ var _a, _b, _c, _d, _e, _f;
530
+ if (!value)
531
+ return "";
532
+ if ((value === null || value === void 0 ? void 0 : value.from) && (value === null || value === void 0 ? void 0 : value.to)) {
533
+ return "".concat((_b = (_a = value.from) === null || _a === void 0 ? void 0 : _a.toLocaleDateString(DEFAULT_LOCALE)) !== null && _b !== void 0 ? _b : (_c = value.from) === null || _c === void 0 ? void 0 : _c.toLocaleString(), ",").concat((_e = (_d = value.to) === null || _d === void 0 ? void 0 : _d.toLocaleDateString(DEFAULT_LOCALE)) !== null && _e !== void 0 ? _e : (_f = value.to) === null || _f === void 0 ? void 0 : _f.toLocaleString());
534
+ }
535
+ return value === null || value === void 0 ? void 0 : value.relativeTimeString;
536
+ },
537
+ });
538
+ createBuiltInType("granularity", {
539
+ transform: function (value) { return value; },
540
+ optionLabel: function (value) { return value; },
541
+ });
542
+ createBuiltInType("dataset");
543
+ createBuiltInType("measure");
544
+ createBuiltInType("dimension");
545
+ createBuiltInType("dimensionOrMeasure");
546
+
432
547
  const embeddableTypeSchema = zod.z
433
548
  .object({
434
549
  typeConfig: zod.z.object({
@@ -436,13 +551,14 @@ const embeddableTypeSchema = zod.z
436
551
  toString: zod.z.function(),
437
552
  }),
438
553
  })
439
- .or(zod.z.enum(core.ALL_NATIVE_TYPES));
554
+ .or(zod.z.enum(ALL_NATIVE_TYPES));
440
555
 
441
556
  const editorMetaSchema = zod.z
442
557
  .object({
443
558
  name: zod.z.string(),
444
559
  label: zod.z.string(),
445
560
  type: embeddableTypeSchema,
561
+ config: zod.z.object({}).optional(),
446
562
  })
447
563
  .strict();
448
564
 
@@ -757,7 +873,7 @@ var validateComponentMetaPlugin = (componentFileRegex) => {
757
873
  ? getModuleType(moduleInfo)
758
874
  : null;
759
875
  if (moduleType) {
760
- const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.code);
876
+ const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.ast);
761
877
  metaConfigs.push({
762
878
  moduleId: moduleInfo.id,
763
879
  meta,
@@ -898,7 +1014,7 @@ const usageValidator = async (directory) => {
898
1014
  }
899
1015
  const typeNames = findTypeNames(directory);
900
1016
  // check for definedTypes using a native typeNames
901
- const duplicatedNativeTypeNames = typeNames.filter((item) => core.ALL_NATIVE_TYPES.includes(item.typeName));
1017
+ const duplicatedNativeTypeNames = typeNames.filter((item) => ALL_NATIVE_TYPES.includes(item.typeName));
902
1018
  if (duplicatedNativeTypeNames.length) {
903
1019
  isValid = false;
904
1020
  progress.fail("React: defineType() can't be used with the same typeName as the nativeTypes. Fix the following files:\n" +