@embeddable.com/sdk-react 2.2.9 → 2.2.11

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
@@ -474,6 +474,7 @@ const componentMetaSchema = zod.z
474
474
  .object({
475
475
  name: zod.z.string(),
476
476
  type: embeddableTypeSchema,
477
+ array: zod.z.boolean().optional(),
477
478
  label: zod.z.string().optional(),
478
479
  })
479
480
  .array()
@@ -486,6 +487,7 @@ const componentMetaSchema = zod.z
486
487
  .object({
487
488
  name: zod.z.string(),
488
489
  type: embeddableTypeSchema,
490
+ array: zod.z.boolean().optional(),
489
491
  defaultValue: zod.z.any().optional(),
490
492
  inputs: zod.z.array(zod.z.string()).optional(),
491
493
  events: zod.z
@@ -636,13 +638,41 @@ var validateComponentMetaPlugin = (componentFileRegex) => {
636
638
  };
637
639
  };
638
640
 
641
+ /**
642
+ * Modifies entry point only if there is 'styled-components' in the client's package.json.
643
+ * Imports StyleSheetManager from styled-components and adds shadow root as "target".
644
+ * That will force styled-components to add classes to the customers web-component
645
+ * see https://styled-components.com/docs/api#stylesheetmanager
646
+ */
647
+ const styledComponentsEntrypointModifier = {
648
+ getContentBegin() {
649
+ return "<StyleSheetManager target={rootEl.getRootNode()}>";
650
+ },
651
+ getContentEnd() {
652
+ return "</StyleSheetManager>";
653
+ },
654
+ getImports() {
655
+ return "import {StyleSheetManager} from 'styled-components'";
656
+ },
657
+ needToModify(ctx) {
658
+ var _a;
659
+ const packageJsonFilePath = path__namespace.resolve(ctx.client.rootDir, "package.json");
660
+ const packageJson = require(packageJsonFilePath);
661
+ return !!((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a["styled-components"]);
662
+ },
663
+ };
664
+
665
+ const entrypointModifiers = [
666
+ styledComponentsEntrypointModifier,
667
+ ];
668
+
639
669
  const oraP = import('ora');
640
670
  const EMB_FILE_REGEX = /^(.*)(?<!\.type|\.options)\.emb\.[jt]s$/;
641
671
  var generate = async (ctx) => {
642
672
  const ora = (await oraP).default;
643
673
  const progress = ora("React: building components...").start();
644
674
  const filesList = await findFiles(ctx.client.srcDir, EMB_FILE_REGEX);
645
- await injectImports(ctx, filesList);
675
+ await prepareEntrypoint(ctx, filesList);
646
676
  await runViteBuild(ctx);
647
677
  progress.succeed("React: components built completed");
648
678
  };
@@ -684,7 +714,10 @@ const IMPORT_REPLACE_TOKEN = "{{LAZY_IMPORTS}}";
684
714
  const ERROR_FALLBACK_COMPONENT_IMPORT_TOKEN = "{{ERROR_FALLBACK_COMPONENT_IMPORT}}";
685
715
  const ERROR_FALLBACK_COMPONENT_TOKEN = "{{ERROR_FALLBACK_COMPONENT}}";
686
716
  const ERROR_FALLBACK_COMPONENT_DEFAULT_NAME = "ErrorFallbackComponent";
687
- async function injectImports(ctx, filesList) {
717
+ const ADDITIONAL_CONTENT_IMPORT_TOKEN = "{{ADDITIONAL_CONTENT_IMPORT}}";
718
+ const ADDITIONAL_CONTENT_BEGIN_TOKEN = "{{ADDITIONAL_CONTENT_BEGIN}}";
719
+ const ADDITIONAL_CONTENT_END_TOKEN = "{{ADDITIONAL_CONTENT_END}}";
720
+ async function prepareEntrypoint(ctx, filesList) {
688
721
  const imports = filesList
689
722
  .map(([fileName, filePath]) => `\t${fileName}: React.lazy(() => import('./${path__namespace.relative(ctx.client.rootDir, filePath)}'))`)
690
723
  .join(",\n");
@@ -694,11 +727,24 @@ async function injectImports(ctx, filesList) {
694
727
  errorBoundaryImport = `import ${ERROR_FALLBACK_COMPONENT_DEFAULT_NAME} from './${path__namespace.relative(ctx.client.rootDir, ctx.client.errorFallbackComponent)}';`;
695
728
  errorFallbackComponent = ERROR_FALLBACK_COMPONENT_DEFAULT_NAME;
696
729
  }
730
+ const additionalContentImport = [];
731
+ let additionalContentBegin = [];
732
+ let additionalContentEnd = [];
733
+ for (const entrypointModifier of entrypointModifiers) {
734
+ if (entrypointModifier.needToModify(ctx)) {
735
+ additionalContentImport.push(entrypointModifier.getImports(ctx));
736
+ additionalContentBegin.push(entrypointModifier.getContentBegin(ctx));
737
+ additionalContentEnd.unshift(entrypointModifier.getContentEnd(ctx));
738
+ }
739
+ }
697
740
  const content = await fs__namespace$1.readFile(path__namespace.resolve(ctx["sdk-react"].templatesDir, `${ctx["sdk-react"].outputOptions.componentsEntryPointFilename}.template`), "utf8");
698
741
  await fs__namespace$1.writeFile(path__namespace.resolve(ctx.client.rootDir, ctx["sdk-react"].outputOptions.componentsEntryPointFilename), content
699
742
  .replace(IMPORT_REPLACE_TOKEN, imports)
700
743
  .replace(ERROR_FALLBACK_COMPONENT_IMPORT_TOKEN, errorBoundaryImport)
701
- .replace(ERROR_FALLBACK_COMPONENT_TOKEN, errorFallbackComponent));
744
+ .replace(ERROR_FALLBACK_COMPONENT_TOKEN, errorFallbackComponent)
745
+ .replace(ADDITIONAL_CONTENT_IMPORT_TOKEN, additionalContentImport.join("\n"))
746
+ .replace(ADDITIONAL_CONTENT_BEGIN_TOKEN, additionalContentBegin.join("\n"))
747
+ .replace(ADDITIONAL_CONTENT_END_TOKEN, additionalContentEnd.join("\n")));
702
748
  }
703
749
 
704
750
  var build = async (ctx) => {