@embeddable.com/sdk-react 3.11.6-next.0 → 3.11.7-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/generate.d.ts CHANGED
@@ -1,5 +1,34 @@
1
1
  import * as vite from "vite";
2
+ import { WatcherOptions } from "rollup";
2
3
  import { ResolvedEmbeddableConfig } from "@embeddable.com/sdk-core";
4
+ import { LogLevel } from "vite";
3
5
  export declare const EMB_FILE_REGEX: RegExp;
4
6
  declare const _default: (ctx: ResolvedEmbeddableConfig) => Promise<vite.Rollup.RollupOutput | vite.Rollup.RollupOutput[] | vite.Rollup.RollupWatcher>;
5
7
  export default _default;
8
+ export declare function getViteConfig(ctx: ResolvedEmbeddableConfig, watch: WatcherOptions | null, plugins: any[]): {
9
+ logLevel: LogLevel;
10
+ resolve: {
11
+ alias?: Record<string, string>;
12
+ extensions: string[];
13
+ };
14
+ plugins: any[];
15
+ build: {
16
+ sourcemap: boolean | "inline";
17
+ watch: {
18
+ include: string[];
19
+ exclude: string[];
20
+ } | undefined;
21
+ minify: boolean;
22
+ rollupOptions: vite.Rollup.RollupOptions;
23
+ lib: {
24
+ entry: string;
25
+ formats: vite.LibraryFormats[];
26
+ fileName: string;
27
+ };
28
+ outDir: string;
29
+ };
30
+ define: {
31
+ "process.env.NODE_ENV": string;
32
+ };
33
+ };
34
+ export declare function runViteBuild(ctx: ResolvedEmbeddableConfig, watch?: WatcherOptions | null): Promise<vite.Rollup.RollupOutput | vite.Rollup.RollupOutput[] | vite.Rollup.RollupWatcher>;
@@ -0,0 +1 @@
1
+ export {};
package/lib/index.esm.js CHANGED
@@ -449,12 +449,12 @@ const loadJson = async (filePath) => {
449
449
 
450
450
  const getComponentLibraryConfig = (componentLibrary) => {
451
451
  let libraryName = componentLibrary;
452
- const include = [];
452
+ let include;
453
453
  const exclude = [];
454
454
  if (typeof componentLibrary === "object" && componentLibrary !== null) {
455
455
  libraryName = componentLibrary.name;
456
456
  if (componentLibrary.include) {
457
- include.push(...componentLibrary.include);
457
+ include = [...componentLibrary.include];
458
458
  }
459
459
  if (componentLibrary.exclude) {
460
460
  exclude.push(...componentLibrary.exclude);
@@ -468,7 +468,7 @@ const getComponentLibraryMeta = async (ctx, componentLibrary) => {
468
468
  const libraryMeta = (await loadJson(getLibraryPath(ctx, libraryName, EXTERNAL_LIBRARY_META_FILE_NAME)));
469
469
  const filterItems = (items) => {
470
470
  let result = items;
471
- if (include.length > 0) {
471
+ if (include) {
472
472
  result = result.filter((item) => include.includes(item));
473
473
  }
474
474
  return result.filter((item) => !exclude.includes(item));
@@ -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 = []) {
@@ -1291,7 +1333,7 @@ function getViteConfig(ctx, watch, plugins) {
1291
1333
  },
1292
1334
  plugins,
1293
1335
  build: {
1294
- sourcemap: true,
1336
+ sourcemap: watch ? "inline" : true, // Inline sourcemaps are faster in dev
1295
1337
  watch: watch
1296
1338
  ? {
1297
1339
  include: [ctx.client.srcDir + "/**"],
@@ -1302,6 +1344,7 @@ function getViteConfig(ctx, watch, plugins) {
1302
1344
  rollupOptions: watch
1303
1345
  ? {
1304
1346
  cache: true,
1347
+ treeshake: false, // Faster builds in dev
1305
1348
  ...ctx.client.rollupOptions,
1306
1349
  output: {
1307
1350
  sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
@@ -1333,38 +1376,66 @@ async function calculateBundleHash(ctx) {
1333
1376
  ctx.client.bundleHash = getContentHash(contentString);
1334
1377
  }
1335
1378
  async function runViteBuild(ctx, watch = null) {
1379
+ // In watch mode (development), combine both builds into one to avoid duplication
1380
+ if (watch) {
1381
+ const { externalComponents, externalEditors } = await findExternalComponents(ctx);
1382
+ return await vite.build(getViteConfig(ctx, watch, [
1383
+ viteReactPlugin(),
1384
+ validateComponentMetaPlugin(EMB_FILE_REGEX),
1385
+ extractComponentsConfigPlugin({
1386
+ isDev: true,
1387
+ globalKey: "componentsMeta",
1388
+ fileName: "embeddable-components-meta.js",
1389
+ outputDir: ctx.client.buildDir,
1390
+ componentFileRegex: EMB_FILE_REGEX,
1391
+ searchEntry: "defineComponent",
1392
+ useBundleHash: false,
1393
+ ctx,
1394
+ externalComponents: externalComponents,
1395
+ }),
1396
+ extractComponentsConfigPlugin({
1397
+ isDev: true,
1398
+ globalKey: "editorsMeta",
1399
+ fileName: "embeddable-editors-meta.js",
1400
+ outputDir: ctx.client.buildDir,
1401
+ componentFileRegex: EMB_FILE_REGEX,
1402
+ searchEntry: "defineEditor",
1403
+ useBundleHash: false,
1404
+ ctx,
1405
+ externalComponents: externalEditors,
1406
+ }),
1407
+ ]));
1408
+ }
1336
1409
  // Step 1: Initial build without extractComponentsConfigPlugin
1337
1410
  await vite.build(getViteConfig(ctx, watch, [
1338
1411
  viteReactPlugin(),
1339
1412
  validateComponentMetaPlugin(EMB_FILE_REGEX),
1340
1413
  ]));
1341
1414
  // Step 2: Calculate bundleHash in production mode
1342
- if (!watch) {
1343
- await calculateBundleHash(ctx);
1344
- }
1415
+ await calculateBundleHash(ctx);
1345
1416
  const { externalComponents, externalEditors } = await findExternalComponents(ctx);
1346
1417
  // Step 3: Final build with extractComponentsConfigPlugin
1347
1418
  return await vite.build(getViteConfig(ctx, watch, [
1348
1419
  viteReactPlugin(),
1349
1420
  extractComponentsConfigPlugin({
1350
- isDev: !!watch,
1421
+ isDev: false,
1351
1422
  globalKey: "componentsMeta",
1352
1423
  fileName: "embeddable-components-meta.js",
1353
1424
  outputDir: ctx.client.buildDir,
1354
1425
  componentFileRegex: EMB_FILE_REGEX,
1355
1426
  searchEntry: "defineComponent",
1356
- useBundleHash: !watch,
1427
+ useBundleHash: true,
1357
1428
  ctx,
1358
1429
  externalComponents: externalComponents,
1359
1430
  }),
1360
1431
  extractComponentsConfigPlugin({
1361
- isDev: !!watch,
1432
+ isDev: false,
1362
1433
  globalKey: "editorsMeta",
1363
1434
  fileName: "embeddable-editors-meta.js",
1364
1435
  outputDir: ctx.client.buildDir,
1365
1436
  componentFileRegex: EMB_FILE_REGEX,
1366
1437
  searchEntry: "defineEditor",
1367
- useBundleHash: !watch,
1438
+ useBundleHash: true,
1368
1439
  ctx,
1369
1440
  externalComponents: externalEditors,
1370
1441
  }),
@@ -1429,7 +1500,7 @@ const findExternalComponents = async (ctx) => {
1429
1500
  const externalComponents = [];
1430
1501
  const externalEditors = [];
1431
1502
  for (const componentLibrary of componentLibraries) {
1432
- const { libraryName} = getComponentLibraryConfig(componentLibrary);
1503
+ const { libraryName } = getComponentLibraryConfig(componentLibrary);
1433
1504
  try {
1434
1505
  const libMeta = await getComponentLibraryMeta(ctx, componentLibrary);
1435
1506
  if (libMeta.plugin !== "react") {