131 lines
5.2 KiB
JavaScript
131 lines
5.2 KiB
JavaScript
import { loadEnv } from "vite";
|
|
const ANALYTICS_TRACKER_SCRIPT = `
|
|
let lastPath = "";
|
|
function getPageNameFromPath(path) {
|
|
const segments = path.split("/").filter(Boolean);
|
|
return segments[0] || null;
|
|
}
|
|
function trackPageView() {
|
|
const path = window.location.pathname;
|
|
if (path === lastPath) return;
|
|
lastPath = path;
|
|
const pageName = getPageNameFromPath(path) || "home";
|
|
const appId = import.meta.env.VITE_BASE44_APP_ID;
|
|
const serverUrl = import.meta.env.VITE_BASE44_BACKEND_URL || "";
|
|
if (!appId) return;
|
|
fetch(\`\${serverUrl}/app-logs/\${appId}/log-user-in-app/\${pageName}\`, {
|
|
method: "POST",
|
|
}).catch(() => {});
|
|
}
|
|
const originalPushState = history.pushState.bind(history);
|
|
history.pushState = function (...args) {
|
|
originalPushState(...args);
|
|
trackPageView();
|
|
};
|
|
const originalReplaceState = history.replaceState.bind(history);
|
|
history.replaceState = function (...args) {
|
|
originalReplaceState(...args);
|
|
trackPageView();
|
|
};
|
|
window.addEventListener("popstate", trackPageView);
|
|
trackPageView();
|
|
`;
|
|
const INJECTIONS = {
|
|
unhandledErrors: {
|
|
src: "/node_modules/@base44/vite-plugin/dist/injections/unhandled-errors-handlers.js",
|
|
injectTo: "head",
|
|
mode: "dev",
|
|
},
|
|
sandboxMount: {
|
|
src: "/node_modules/@base44/vite-plugin/dist/injections/sandbox-mount-observer.js",
|
|
injectTo: "body",
|
|
mode: "dev",
|
|
},
|
|
hmrNotifier: {
|
|
src: "/node_modules/@base44/vite-plugin/dist/injections/sandbox-hmr-notifier.js",
|
|
injectTo: "head",
|
|
mode: "dev",
|
|
},
|
|
navigationNotifier: {
|
|
src: "/node_modules/@base44/vite-plugin/dist/injections/navigation-notifier.js",
|
|
injectTo: "head",
|
|
mode: "dev",
|
|
},
|
|
analyticsTracker: {
|
|
injectTo: "head",
|
|
mode: "production",
|
|
inlineContent: ANALYTICS_TRACKER_SCRIPT,
|
|
},
|
|
};
|
|
export function htmlInjectionsPlugin({ hmrNotifier, navigationNotifier, visualEditAgent, analyticsTracker, }) {
|
|
const enabledInjections = {
|
|
unhandledErrors: true,
|
|
sandboxMount: true,
|
|
hmrNotifier,
|
|
navigationNotifier,
|
|
analyticsTracker,
|
|
};
|
|
let resolvedEnv = {};
|
|
return {
|
|
name: "html-injections",
|
|
configResolved(config) {
|
|
// Capture env vars for use in transformIndexHtml
|
|
// loadEnv gets all env vars including VITE_ prefixed ones
|
|
resolvedEnv = loadEnv(config.mode, config.root, "");
|
|
},
|
|
transformIndexHtml: {
|
|
handler(_html, ctx) {
|
|
const currentMode = ctx.server ? "dev" : "production";
|
|
const tags = Object.entries(INJECTIONS)
|
|
.filter(([key, injection]) => enabledInjections[key] && injection.mode === currentMode)
|
|
.map(([, injection]) => {
|
|
// Production injections use inline content (src paths don't work in built output)
|
|
if (injection.inlineContent) {
|
|
// Replace import.meta.env references with actual values
|
|
// Vite doesn't replace these in inline script content
|
|
let content = injection.inlineContent;
|
|
content = content.replace(/import\.meta\.env\.(\w+)/g, (_, envKey) => JSON.stringify(resolvedEnv[envKey] ?? ""));
|
|
return {
|
|
tag: "script",
|
|
attrs: { type: "module" },
|
|
children: content,
|
|
injectTo: injection.injectTo,
|
|
};
|
|
}
|
|
// Dev injections use src path (served from node_modules)
|
|
return {
|
|
tag: "script",
|
|
attrs: {
|
|
src: injection.src,
|
|
type: "module",
|
|
},
|
|
injectTo: injection.injectTo,
|
|
};
|
|
});
|
|
// Visual edit agent — loaded via dynamic import to support
|
|
// local dev iteration with ?sandbox-bridge=local
|
|
if (currentMode === "dev" && visualEditAgent) {
|
|
const dist = "/node_modules/@base44/vite-plugin/dist";
|
|
tags.push({
|
|
tag: "script",
|
|
attrs: { type: "module" },
|
|
children: [
|
|
`if (window.self !== window.top) {`,
|
|
` const mode = new URLSearchParams(location.search).get("sandbox-bridge");`,
|
|
` const url = mode === "local"`,
|
|
` ? "https://localhost:3201/index.mjs"`,
|
|
` : "${dist}/statics/index.mjs";`,
|
|
` import(url)`,
|
|
` .then(mod => { if (typeof mod.setupVisualEditAgent === "function") mod.setupVisualEditAgent(); })`,
|
|
` .catch(e => console.error("[visual-edit-agent] Failed to load:", e));`,
|
|
`}`,
|
|
].join("\n"),
|
|
injectTo: "body",
|
|
});
|
|
}
|
|
return tags;
|
|
},
|
|
},
|
|
};
|
|
}
|
|
//# sourceMappingURL=html-injections-plugin.js.map
|