@rebasepro/core 0.0.1-canary.eae7889 → 0.1.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.
Files changed (37) hide show
  1. package/dist/components/BootstrapAdminBanner.d.ts +4 -0
  2. package/dist/components/LoginView/LoginView.d.ts +22 -0
  3. package/dist/components/common/useDataTableController.d.ts +3 -3
  4. package/dist/components/index.d.ts +1 -0
  5. package/dist/hooks/data/useRelationSelector.d.ts +2 -2
  6. package/dist/hooks/index.d.ts +1 -0
  7. package/dist/hooks/useCollapsedGroups.d.ts +16 -1
  8. package/dist/hooks/useResolvedComponent.d.ts +47 -0
  9. package/dist/index.es.js +333 -121
  10. package/dist/index.es.js.map +1 -1
  11. package/dist/index.umd.js +330 -118
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/vitePlugin.d.ts +28 -1
  14. package/dist/vitePlugin.js +42 -0
  15. package/package.json +7 -8
  16. package/src/components/BootstrapAdminBanner.tsx +66 -0
  17. package/src/components/LoginView/LoginView.tsx +73 -42
  18. package/src/components/UserSelectPopover.tsx +8 -34
  19. package/src/components/common/useColumnsIds.tsx +16 -16
  20. package/src/components/common/useDataTableController.tsx +30 -18
  21. package/src/components/index.tsx +1 -1
  22. package/src/core/Rebase.tsx +20 -14
  23. package/src/hooks/data/useRelationSelector.tsx +6 -6
  24. package/src/hooks/index.tsx +1 -0
  25. package/src/hooks/useCollapsedGroups.ts +48 -6
  26. package/src/hooks/useRebaseContext.tsx +11 -6
  27. package/src/hooks/useResolvedComponent.tsx +157 -0
  28. package/src/hooks/useStudioBridge.tsx +2 -1
  29. package/src/locales/de.ts +4 -0
  30. package/src/locales/en.ts +6 -0
  31. package/src/locales/es.ts +4 -0
  32. package/src/locales/fr.ts +4 -0
  33. package/src/locales/hi.ts +4 -0
  34. package/src/locales/it.ts +4 -0
  35. package/src/locales/pt.ts +4 -0
  36. package/src/util/previews.ts +16 -7
  37. package/src/vitePlugin.ts +87 -1
package/src/vitePlugin.ts CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ import path from "path";
2
3
 
3
4
  export interface RebaseCollectionsPluginOptions {
4
5
  /**
@@ -8,22 +9,70 @@ export interface RebaseCollectionsPluginOptions {
8
9
  collectionsDir: string;
9
10
  }
10
11
 
12
+ /**
13
+ * Properties on collection objects that accept `ComponentRef` values.
14
+ * When a string literal is found for any of these keys in a collection file,
15
+ * the transform plugin replaces it with a `LazyComponentRef` object so the
16
+ * component is loaded lazily and never evaluated by the backend.
17
+ */
18
+ const LAZY_COMPONENT_KEYS = ["Field", "Preview", "Builder"];
19
+
20
+ /**
21
+ * Regex that matches `Key: "relative/path"` or `Key: 'relative/path'`
22
+ * for each key listed in LAZY_COMPONENT_KEYS.
23
+ *
24
+ * It captures:
25
+ * $1 — everything before the quote (e.g. `Field: `)
26
+ * $2 — the quote character (' or ")
27
+ * $3 — the path (must start with ./ or ../)
28
+ *
29
+ * The lookbehind-free pattern avoids issues with older runtimes.
30
+ */
31
+ function buildTransformRegex(): RegExp {
32
+ const keys = LAZY_COMPONENT_KEYS.join("|");
33
+ // Match property key, colon, optional whitespace, then a string starting with a dot-path
34
+ return new RegExp(
35
+ `((?:${keys})\\s*:\\s*)(['"])(\\.\\.?\\/[^'"]+)\\2`,
36
+ "g"
37
+ );
38
+ }
39
+
11
40
  /**
12
41
  * A Vite plugin that dynamically loads and automatically wires Rebase collections.
13
- * It provides a virtual module "virtual:rebase-collections" that statically exports the resolved collections array.
42
+ *
43
+ * It provides two capabilities:
44
+ * 1. A **virtual module** `"virtual:rebase-collections"` that statically exports
45
+ * the resolved collections array.
46
+ * 2. A **transform hook** that converts string-based component references
47
+ * (e.g. `Field: "../../components/MyField"`) into `LazyComponentRef` objects
48
+ * (`{ __rebaseLazy: true, load: () => import(...) }`), enabling code-splitting
49
+ * and preventing the backend from loading React-dependent modules.
14
50
  */
15
51
  export function rebaseCollectionsPlugin(options: RebaseCollectionsPluginOptions) {
16
52
  const virtualModuleId = "virtual:rebase-collections";
17
53
  const resolvedVirtualModuleId = "\0" + virtualModuleId;
18
54
 
55
+ let resolvedCollectionsDir: string;
56
+ const transformRegex = buildTransformRegex();
57
+
19
58
  return {
20
59
  name: "rebase-collections-plugin",
60
+
61
+ configResolved(config: { root: string }) {
62
+ // Resolve the collections directory to an absolute path
63
+ // so the `transform` hook can match files reliably.
64
+ resolvedCollectionsDir = path.isAbsolute(options.collectionsDir)
65
+ ? options.collectionsDir
66
+ : path.resolve(config.root, options.collectionsDir);
67
+ },
68
+
21
69
  resolveId(id: string) {
22
70
  if (id === virtualModuleId) {
23
71
  return resolvedVirtualModuleId;
24
72
  }
25
73
  return null;
26
74
  },
75
+
27
76
  load(id: string) {
28
77
  if (id === resolvedVirtualModuleId) {
29
78
  // Vite evaluates `import.meta.glob` relative to the project root.
@@ -41,6 +90,43 @@ export function rebaseCollectionsPlugin(options: RebaseCollectionsPluginOptions)
41
90
  `;
42
91
  }
43
92
  return null;
93
+ },
94
+
95
+ /**
96
+ * Transform collection files to convert string component references
97
+ * into lazy-loading `LazyComponentRef` objects.
98
+ *
99
+ * Example transform:
100
+ * ```
101
+ * // Input
102
+ * Field: "../../frontend/src/components/MyField"
103
+ *
104
+ * // Output
105
+ * Field: { __rebaseLazy: true, load: () => import("../../frontend/src/components/MyField") }
106
+ * ```
107
+ */
108
+ transform(code: string, id: string) {
109
+ // Only process .ts/.tsx files inside the collections directory
110
+ if (!resolvedCollectionsDir) return null;
111
+ if (!id.startsWith(resolvedCollectionsDir)) return null;
112
+ if (!/\.tsx?$/.test(id)) return null;
113
+
114
+ // Reset the regex state (global flag means lastIndex persists)
115
+ transformRegex.lastIndex = 0;
116
+
117
+ if (!transformRegex.test(code)) return null;
118
+
119
+ // Reset again after the test consumed the regex
120
+ transformRegex.lastIndex = 0;
121
+
122
+ const transformed = code.replace(
123
+ transformRegex,
124
+ (_match, prefix: string, quote: string, importPath: string) => {
125
+ return `${prefix}{ __rebaseLazy: true, load: () => import(${quote}${importPath}${quote}) }`;
126
+ }
127
+ );
128
+
129
+ return { code: transformed, map: null };
44
130
  }
45
131
  };
46
132
  }