@base44/vite-plugin 1.0.35 → 1.0.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44/vite-plugin",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "description": "The Vite plugin for base44 based applications",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,103 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ /**
4
+ * Serves `base44:runtime` to full-stack server routes, so a route reads secrets
5
+ * and schedules background work with the SAME import a backend function uses:
6
+ *
7
+ * import { secrets, waitUntil } from "base44:runtime";
8
+ *
9
+ * WHY A BUILD-TIME VIRTUAL MODULE. The Workers runtime cannot resolve a custom
10
+ * scheme: the legacy module registry special-cases only `node:` / `cloudflare:` /
11
+ * `workerd:`, and the new registry (`new_module_registry`) explicitly rejects a
12
+ * bundle module whose name carries a non-`file:` scheme. Import maps are not
13
+ * supported either. So `base44:runtime` has to be resolved before upload — there
14
+ * is no runtime path to it, and no amount of wrapping the deployed entry creates
15
+ * one.
16
+ *
17
+ * WHY NO ASYNCLOCALSTORAGE / NO WRAPPER ENTRY. Cloudflare now ships both
18
+ * primitives ambiently from `cloudflare:workers` (`env` since 2025-03-17,
19
+ * `waitUntil` since 2025-08-08), backed by workerd's own AsyncContextFrame and
20
+ * IoContext. The backend-function bundler predates that and threads a context
21
+ * through `globalThis.Base44` because it owns the generated entry; here the
22
+ * framework owns the entry, and borrowing the platform's ambient accessors means
23
+ * we never have to.
24
+ */
25
+
26
+ const SPECIFIER = "base44:runtime";
27
+ // Rollup convention: a resolved virtual id starts with NUL so no other plugin
28
+ // (and no filesystem lookup) tries to claim it.
29
+ const RESOLVED = "\0base44:runtime";
30
+
31
+ // Reserved so a route importing it fails with an explanation instead of a bare
32
+ // "failed to resolve import". Actors are never imported — they live in the app's
33
+ // actors directory, in server routes exactly as in backend functions.
34
+ const ACTORS_REASON =
35
+ "Actors are defined in the app's actors directory, not imported into a route.";
36
+
37
+ function unsupportedReason(id: string): string | null {
38
+ if (id === "base44:runtime/actors") return ACTORS_REASON;
39
+ return null;
40
+ }
41
+
42
+ /**
43
+ * `cloudflare:workers` stays external — workerd provides it, and Nitro's
44
+ * cloudflare preset already treats `cloudflare:*` that way.
45
+ *
46
+ * There is deliberately no separate dev body. Nitro's `cloudflareDev` preset
47
+ * (aliased for `cloudflare-module`, which is what these apps build with) sets
48
+ * `devServer.runner: "miniflare"`, so dev runs in workerd too and the same
49
+ * import resolves there. A `process.env` fallback would buy nothing and cost a
50
+ * dev/prod behaviour split — the shape that makes background work silently
51
+ * vanish locally while working in production. If someone overrides the runner
52
+ * away from workerd, this fails loudly at resolution rather than quietly
53
+ * serving a different value.
54
+ *
55
+ * `waitUntil` is WRAPPED rather than re-exported: ours returns the promise so it
56
+ * composes (`infra/base44-userapp-bundler/src/runtime/index.ts` in apper),
57
+ * Cloudflare's returns void. A bare re-export would make `const p = waitUntil(x)`
58
+ * work in a backend function and silently yield `undefined` here — one specifier
59
+ * with two behaviours is the exact failure this module exists to prevent.
60
+ */
61
+ const RUNTIME_MODULE = `
62
+ import { env, waitUntil as cfWaitUntil } from "cloudflare:workers";
63
+
64
+ export function waitUntil(promise) {
65
+ cfWaitUntil(promise);
66
+ return promise;
67
+ }
68
+
69
+ export const secrets = {
70
+ get(name) {
71
+ const value = env[name];
72
+ return typeof value === "string" ? value : undefined;
73
+ },
74
+ };
75
+ `;
76
+
77
+ export function base44RuntimePlugin(): Plugin {
78
+ return {
79
+ name: "base44-runtime",
80
+ // Ahead of Vite's own resolution so the bare `base44:` specifier is never
81
+ // mistaken for a URL protocol.
82
+ enforce: "pre",
83
+
84
+ resolveId(id) {
85
+ const reason = unsupportedReason(id);
86
+ if (reason) {
87
+ this.error(`"${id}" is not available in server routes. ${reason}`);
88
+ }
89
+ if (id !== SPECIFIER) return null;
90
+
91
+ // `cloudflare:workers` has no browser equivalent, so a client-side import
92
+ // would bundle a module that cannot run. Fail with the reason instead.
93
+ if (this.environment?.name === "client") {
94
+ this.error(`"${SPECIFIER}" is server-only. Move this import into a server route.`);
95
+ }
96
+ return RESOLVED;
97
+ },
98
+
99
+ load(id) {
100
+ return id === RESOLVED ? RUNTIME_MODULE : null;
101
+ },
102
+ };
103
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ import { visualEditPlugin } from "./visual-edit-plugin.js";
6
6
  import { filterPackagesInProject } from "./utils.js";
7
7
  import { htmlInjectionsPlugin } from "./html-injections-plugin.js";
8
8
  import { buildStatusPlugin } from "./build-status-plugin.js";
9
+ import { base44RuntimePlugin } from "./base44-runtime-plugin.js";
9
10
 
10
11
  const isRunningInSandbox = !!process.env.MODAL_SANDBOX_ID;
11
12
 
@@ -288,5 +289,10 @@ export default function vitePlugin(
288
289
  ...(process.env.BASE44_BUILD_STATUS_ENABLED === "1"
289
290
  ? [buildStatusPlugin()]
290
291
  : []),
292
+ // Serves `base44:runtime` to full-stack server routes. Unconditional: it
293
+ // only claims the `base44:` specifiers, so an app with no server dir never
294
+ // notices it. Ordering comes from its own `enforce: "pre"`, not from its
295
+ // position here — several tests index this array from the front.
296
+ base44RuntimePlugin(),
291
297
  ];
292
298
  }
@@ -1,6 +1,6 @@
1
1
  /** Controller that encapsulates layer-dropdown integration logic */
2
2
 
3
- import { getElementSelectorId } from "../utils.js";
3
+ import { readVisualEditTagName, getElementSelectorId } from "../utils.js";
4
4
  import { buildLayerChain } from "./utils.js";
5
5
  import {
6
6
  enhanceLabelWithChevron,
@@ -62,7 +62,7 @@ export function createLayerController(config: LayerControllerConfig): LayerContr
62
62
  } else {
63
63
  dropdownSourceLayer = {
64
64
  element,
65
- tagName: element.tagName.toLowerCase(),
65
+ tagName: readVisualEditTagName(element).toLowerCase(),
66
66
  selectorId: currentId,
67
67
  };
68
68
  config.onDeselect();
@@ -1,6 +1,6 @@
1
1
  /** DOM utilities for the layer-dropdown module */
2
2
 
3
- import { isInstrumentedElement, getElementSelectorId } from "../utils.js";
3
+ import { readVisualEditTagName, isInstrumentedElement, getElementSelectorId } from "../utils.js";
4
4
  import { MAX_PARENT_DEPTH, MAX_CHILD_DEPTH } from "./consts.js";
5
5
 
6
6
  import type { LayerInfo } from "./types.js";
@@ -23,7 +23,7 @@ export function getLayerDisplayName(layer: LayerInfo): string {
23
23
  function toLayerInfo(element: Element, depth?: number): LayerInfo {
24
24
  const info: LayerInfo = {
25
25
  element,
26
- tagName: element.tagName.toLowerCase(),
26
+ tagName: readVisualEditTagName(element).toLowerCase(),
27
27
  selectorId: getElementSelectorId(element),
28
28
  };
29
29
  if (depth !== undefined) info.depth = depth;
@@ -50,7 +50,7 @@ export function getInstrumentedDescendants(
50
50
  if (isInstrumentedElement(child)) {
51
51
  const info: LayerInfo = {
52
52
  element: child,
53
- tagName: child.tagName.toLowerCase(),
53
+ tagName: readVisualEditTagName(child).toLowerCase(),
54
54
  selectorId: getElementSelectorId(child),
55
55
  };
56
56
  if (startDepth !== undefined) {
@@ -53,6 +53,14 @@ export function getElementSelectorId(element: Element): string | null {
53
53
  );
54
54
  }
55
55
 
56
+ export function isImageComponent(element: Element): boolean {
57
+ return element.hasAttribute("data-base44-image");
58
+ }
59
+
60
+ export function readVisualEditTagName(element: Element): string {
61
+ return isImageComponent(element) ? "IMG" : element.tagName;
62
+ }
63
+
56
64
  export const ALLOWED_ATTRIBUTES: string[] = ["src"];
57
65
 
58
66
  export const PLUGIN_ELEMENT_ATTR = "data-vite-plugin-element";
@@ -93,7 +101,11 @@ export function updateElementAttribute(elements: Element[], attribute: string, v
93
101
  targetArrIndex === null ||
94
102
  (element as HTMLElement).dataset.arrIndex === targetArrIndex
95
103
  ) {
96
- element.setAttribute(attribute, value);
104
+ if (attribute === "src" && isImageComponent(element)) {
105
+ element.dispatchEvent(new CustomEvent("base44:image-replace", { detail: { src: value } }));
106
+ } else {
107
+ element.setAttribute(attribute, value);
108
+ }
97
109
  }
98
110
  });
99
111
  }
@@ -111,7 +123,9 @@ export function readElementClasses(element: Element): string {
111
123
  export function collectAllowedAttributes(element: Element, allowedAttributes: string[]): Record<string, string> {
112
124
  const attributes: Record<string, string> = {};
113
125
  for (const attr of allowedAttributes) {
114
- const val = element.getAttribute(attr);
126
+ const val = attr === "src" && isImageComponent(element)
127
+ ? element.getAttribute("data-base44-image-src")
128
+ : element.getAttribute(attr);
115
129
  if (val !== null) {
116
130
  attributes[attr] = val;
117
131
  }
@@ -1,4 +1,4 @@
1
- import { findElementsById, updateElementClasses, updateElementAttribute, collectAllowedAttributes, readElementClasses, ALLOWED_ATTRIBUTES, getElementSelectorId, stopAnimations, resumeAnimations, findInstrumentedElement, resolveHoverTarget, positionLabel, injectFontFaceCss } from "./utils.js";
1
+ import { readVisualEditTagName, findElementsById, updateElementClasses, updateElementAttribute, collectAllowedAttributes, readElementClasses, ALLOWED_ATTRIBUTES, getElementSelectorId, stopAnimations, resumeAnimations, findInstrumentedElement, resolveHoverTarget, positionLabel, injectFontFaceCss } from "./utils.js";
2
2
  import { createLayerController } from "./layer-dropdown/controller.js";
3
3
  import { LAYER_DROPDOWN_ATTR } from "./layer-dropdown/consts.js";
4
4
  import { createInlineEditController } from "../capabilities/inline-edit/index.js";
@@ -65,7 +65,7 @@ export function setupVisualEditAgent() {
65
65
 
66
66
  if (!label) {
67
67
  label = document.createElement("div");
68
- label.textContent = element.tagName.toLowerCase();
68
+ label.textContent = readVisualEditTagName(element).toLowerCase();
69
69
  label.style.position = "absolute";
70
70
  label.style.left = "-2px";
71
71
  label.style.padding = "2px 8px";
@@ -137,7 +137,7 @@ export function setupVisualEditAgent() {
137
137
  const notifyElementSelected = (element: Element) => {
138
138
  const htmlElement = element as HTMLElement;
139
139
  const rect = element.getBoundingClientRect();
140
- const isTextElement = TEXT_TAGS.includes(element.tagName?.toLowerCase());
140
+ const isTextElement = TEXT_TAGS.includes(readVisualEditTagName(element).toLowerCase());
141
141
 
142
142
  const arrEl = htmlElement.closest("[data-arr-variable-name]") as HTMLElement | null;
143
143
  const staticArrayName = arrEl?.dataset?.arrVariableName || null;
@@ -151,7 +151,7 @@ export function setupVisualEditAgent() {
151
151
 
152
152
  window.parent.postMessage({
153
153
  type: "element-selected",
154
- tagName: element.tagName,
154
+ tagName: readVisualEditTagName(element),
155
155
  classes: readElementClasses(element),
156
156
  visualSelectorId: getElementSelectorId(element),
157
157
  content: isTextElement ? htmlElement.innerText : undefined,
@@ -181,7 +181,7 @@ export function setupVisualEditAgent() {
181
181
  };
182
182
 
183
183
  // Select an element: create overlays, update state, notify parent
184
- const selectElement = (element: Element): HTMLDivElement | undefined => {
184
+ const selectElement = (element: Element, notifyParent = true): HTMLDivElement | undefined => {
185
185
  const visualSelectorId = getElementSelectorId(element);
186
186
 
187
187
  clearSelectedOverlays();
@@ -197,7 +197,7 @@ export function setupVisualEditAgent() {
197
197
  selectedElementId = visualSelectorId || null;
198
198
  selectedElement = element;
199
199
  clearHoverOverlays();
200
- notifyElementSelected(element);
200
+ if (notifyParent) notifyElementSelected(element);
201
201
 
202
202
  return selectedOverlays[0];
203
203
  };
@@ -381,9 +381,20 @@ export function setupVisualEditAgent() {
381
381
  // Reposition overlays after attribute change (e.g. image src swap can affect layout)
382
382
  setTimeout(() => {
383
383
  if (selectedElementId === visualSelectorId) {
384
+ const currentElements = findElementsById(visualSelectorId);
385
+ if (selectedElement && !selectedElement.isConnected) {
386
+ const selectedIndex = (selectedElement as HTMLElement).dataset.arrIndex;
387
+ const replacement = currentElements.find(
388
+ (element) => (element as HTMLElement).dataset.arrIndex === selectedIndex
389
+ );
390
+ if (replacement) {
391
+ const overlay = selectElement(replacement, false);
392
+ layerController.attachToOverlay(overlay, replacement);
393
+ }
394
+ }
384
395
  selectedOverlays.forEach((overlay, index) => {
385
- if (index < elements.length) {
386
- positionOverlay(overlay, elements[index]!);
396
+ if (index < currentElements.length) {
397
+ positionOverlay(overlay, currentElements[index]!);
387
398
  }
388
399
  });
389
400
  }
@@ -356,3 +356,18 @@ The `transformIndexHtml` hook injects the Tailwind CSS CDN, enabling visual edit
356
356
  | `src/injections/sandbox-mount-observer.ts` | Detects when instrumented elements mount in DOM |
357
357
  | `src/injections/utils.ts` | `findElementsById()`, `updateElementClasses()` utilities |
358
358
  | `tests/visual-edit-agent.test.ts` | Unit tests for element finding and class updates |
359
+
360
+ ### Seeded Image components
361
+
362
+ A `data-base44-image` wrapper is one logical image edit target. Its classes and
363
+ source location refer to the app's `<Image>` call site; report its tag as `IMG`
364
+ for the image toolbar and layer labels. Read `src` from `data-base44-image-src`,
365
+ which preserves the original prop rather than the inner image's transformed CDN
366
+ URL. The template keeps all instrumentation on the wrapper and suppresses source
367
+ locations on the placeholder and main image.
368
+
369
+ For `src` edits, dispatch `base44:image-replace` with `detail: { src }` on the
370
+ wrapper, retaining the selected-array-index filter. The template updates React
371
+ state so the replacement survives responsive resizes. Native images continue to
372
+ use direct attribute updates. Publish this support before seeding a template
373
+ that uses the wrapper contract.