@embeddable.com/sdk-react 3.11.6-next.0 → 3.11.6

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
@@ -1063,6 +1063,49 @@ var validateComponentMetaPlugin = (componentFileRegex) => {
1063
1063
  };
1064
1064
  };
1065
1065
 
1066
+ /**
1067
+ * Checks if a given package exists in dependencies or devDependencies of a package.json object.
1068
+ * @param packageJson The parsed package.json object.
1069
+ * @param packageName The name of the package to check.
1070
+ * @returns True if the package exists, false otherwise.
1071
+ */
1072
+ function hasPackage(packageJson, packageName) {
1073
+ var _a, _b, _c;
1074
+ return !!((_b = (_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[packageName]) !== null && _b !== void 0 ? _b : (_c = packageJson.devDependencies) === null || _c === void 0 ? void 0 : _c[packageName]);
1075
+ }
1076
+
1077
+ /**
1078
+ * Modifies entry point only if Emotion is installed in the client's package.json.
1079
+ * Wraps the app with CacheProvider and creates a custom cache to inject Emotion styles
1080
+ * into the Shadow DOM or desired container.
1081
+ */
1082
+ const emotionReactEntrypointModifier = {
1083
+ getContentBegin() {
1084
+ return `<EmbeddableEmotionCacheProvider value={createCache({ key: 'css', container: rootEl.getRootNode() })}>`;
1085
+ },
1086
+ getContentEnd() {
1087
+ return `</EmbeddableEmotionCacheProvider>`;
1088
+ },
1089
+ getImports() {
1090
+ return `
1091
+ import { CacheProvider as EmbeddableEmotionCacheProvider } from '@emotion/react';
1092
+ import createCache from '@emotion/cache';
1093
+ `;
1094
+ },
1095
+ async needToModify(ctx) {
1096
+ const packageJsonFilePath = path.resolve(ctx.client.rootDir, "package.json");
1097
+ try {
1098
+ const packageJson = JSON.parse(await fs.readFile(packageJsonFilePath, "utf-8"));
1099
+ return !!(hasPackage(packageJson, "@emotion/react") ||
1100
+ hasPackage(packageJson, "react-select"));
1101
+ }
1102
+ catch (error) {
1103
+ console.error("emotionReactEntrypointModifier: Error reading package.json", error);
1104
+ return false;
1105
+ }
1106
+ },
1107
+ };
1108
+
1066
1109
  /**
1067
1110
  * Modifies entry point only if there is 'styled-components' in the client's package.json.
1068
1111
  * Imports StyleSheetManager from styled-components and adds shadow root as "target".
@@ -1080,12 +1123,10 @@ const styledComponentsEntrypointModifier = {
1080
1123
  return "import {StyleSheetManager} from 'styled-components'";
1081
1124
  },
1082
1125
  async needToModify(ctx) {
1083
- var _a, _b;
1084
1126
  const packageJsonFilePath = path.resolve(ctx.client.rootDir, "package.json");
1085
1127
  try {
1086
1128
  const packageJson = JSON.parse(await fs.readFile(packageJsonFilePath, "utf-8"));
1087
- return !!(((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a["styled-components"]) ||
1088
- ((_b = packageJson.devDependencies) === null || _b === void 0 ? void 0 : _b["styled-components"]));
1129
+ return !!(hasPackage(packageJson, "styled-components"));
1089
1130
  }
1090
1131
  catch (error) {
1091
1132
  console.error("styledComponentsEntrypointModifier: Error reading package.json", error);
@@ -1096,6 +1137,7 @@ const styledComponentsEntrypointModifier = {
1096
1137
 
1097
1138
  const entrypointModifiers = [
1098
1139
  styledComponentsEntrypointModifier,
1140
+ emotionReactEntrypointModifier
1099
1141
  ];
1100
1142
 
1101
1143
  function findFilesWithWrongUsage(directory, pattern, mustEndWith = []) {