@embeddable.com/sdk-react 2.2.9 → 2.2.10

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