Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

10.0 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import path from 'node:path';

import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
import type { PluginOption, UserConfig, ViteDevServer } from 'vite';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';

import { canvasRuntimeEntries } from '../../plugins/vite/canvasRuntime';
import { devLoadingProgress } from '../../plugins/vite/devLoadingProgress';
import {
  createSharedRolldownOutput,
  sharedModulePreload,
  sharedOptimizeDeps,
  sharedRendererAlias,
  sharedRendererDedupe,
  sharedRendererDefine,
  sharedRendererPlugins,
} from '../../plugins/vite/sharedRendererConfig';
import { spaPublicDirNames } from '../../scripts/copySpaBuildCore';
import {
  applyDesktopViteConfigExtension,
  CLOUD_ROOT_DIR,
  desktopPackageJson,
  DEV_VITE_PORT,
  isCloudDesktopBuild,
  loadDesktopEnv,
  reactDevtoolsPlugin,
  RENDERER_CHROME_TARGET,
  ROOT_DIR,
} from './vite.shared';

const RENDERER_OUT_DIR = path.resolve(__dirname, 'dist/renderer');
const RENDERER_INPUTS = {
  main: path.resolve(__dirname, 'index.html'),
  overlay: path.resolve(__dirname, 'overlay.html'),
  popup: path.resolve(__dirname, 'popup.html'),
};

/**
 * The repository public directory can contain ignored web build outputs after
 * local SPA builds. Vite copies the whole directory by default, so remove only
 * those generated web outputs from the generated desktop renderer directory.
 * The directory list is derived from the copy script's targets so a newly
 * added SPA surface cannot silently ride into the desktop bundle again.
 */
function excludeWebSpaBuildArtifactsPlugin(): PluginOption {
  return {
    async closeBundle() {
      await Promise.all(
        spaPublicDirNames.map((directory) =>
          rm(path.join(RENDERER_OUT_DIR, directory), { force: true, recursive: true }),
        ),
      );
    },
    name: 'exclude-web-spa-build-artifacts',
  };
}

/**
 * Rewrite SPA routes to their corresponding HTML entry so the Vite
 * dev server serves the right HTML when root is the monorepo root.
 *
 * - `/popup/*` β†’ `/apps/desktop/popup.html` (topic popup SPA)
 * - `/`, `/index.html`, and everything else β†’ `/apps/desktop/index.html`
 */
function electronDesktopHtmlPlugin(): PluginOption {
  return {
    configureServer(server: ViteDevServer) {
      server.middlewares.use((req, _res, next) => {
        const rawUrl = req.url ?? '';
        const pathname = rawUrl.split('?')[0];

        // Explicit document-entry requests β€” always rewrite.
        if (pathname === '/' || pathname === '/index.html') {
          req.url = '/apps/desktop/index.html';
          next();
          return;
        }
        if (pathname === '/overlay' || pathname === '/overlay.html') {
          req.url = '/apps/desktop/overlay.html';
          next();
          return;
        }
        if (pathname === '/popup.html') {
          req.url = '/apps/desktop/popup.html';
          next();
          return;
        }

        // For SPA deep links (e.g. `/popup/agent/A/T`) rewrite to the popup
        // HTML β€” but skip asset / module requests that happen to share the
        // prefix (e.g. `/popup/@vite/client` would have been generated by a
        // mis-resolved relative import).
        const lastSegment = pathname.split('/').pop() ?? '';
        const looksLikeAsset =
          lastSegment.includes('.') ||
          pathname.startsWith('/@') ||
          pathname.startsWith('/src/') ||
          pathname.startsWith('/node_modules/') ||
          pathname.startsWith('/apps/') ||
          pathname.startsWith('/packages/');

        if (!looksLikeAsset && (pathname === '/popup' || pathname.startsWith('/popup/'))) {
          req.url = '/apps/desktop/popup.html';
        }
        next();
      });
    },
    name: 'electron-desktop-html',
  };
}

const CLOUD_DESKTOP_BUSINESS_FEATURES_FLAG = '__LOBECLOUD_DESKTOP_BUSINESS_FEATURES__';
const BUSINESS_CONST_MODULE_ID = '@lobechat/business-const';
const CLOUD_BUSINESS_CONST_MODULE_ID = '@cloud/business-const';
const DYNAMIC_BUSINESS_CONST_QUERY = '?lobe-cloud-desktop-business-const';

const createBusinessFeaturesBootstrapScript = () =>
  `globalThis[${JSON.stringify(CLOUD_DESKTOP_BUSINESS_FEATURES_FLAG)}] = true;`;

const replaceBusinessFlagExport = (code: string, name: string, initializer: string) => {
  const pattern = new RegExp(`export\\s+(?:const|let|var)\\s+${name}\\s*=\\s*[\\s\\S]*?;`);

  return {
    code: code.replace(pattern, `export let ${name} = ${initializer};`),
    replaced: pattern.test(code),
  };
};

const injectDynamicBusinessFeatureFlag = (code: string) => {
  const businessFlag = replaceBusinessFlagExport(
    code,
    'ENABLE_BUSINESS_FEATURES',
    `Boolean(globalThis['${CLOUD_DESKTOP_BUSINESS_FEATURES_FLAG}'])`,
  );
  const topicLinkFlag = replaceBusinessFlagExport(
    businessFlag.code,
    'ENABLE_TOPIC_LINK_SHARE',
    'ENABLE_BUSINESS_FEATURES',
  );

  if (!businessFlag.replaced) {
    throw new Error('Cannot find ENABLE_BUSINESS_FEATURES export in @cloud/business-const');
  }

  const topicLinkAssignment = topicLinkFlag.replaced
    ? '\n  ENABLE_TOPIC_LINK_SHARE = enabled;'
    : '';

  return `${topicLinkFlag.code}

const __lobeCloudDesktopBusinessFeaturesFlagKey = '${CLOUD_DESKTOP_BUSINESS_FEATURES_FLAG}';
const __lobeCloudDesktopApplyBusinessFeaturesFlag = (value) => {
  const enabled = Boolean(value);
  ENABLE_BUSINESS_FEATURES = enabled;${topicLinkAssignment}
  return enabled;
};

const __lobeCloudDesktopExistingDescriptor = Object.getOwnPropertyDescriptor(
  globalThis,
  __lobeCloudDesktopBusinessFeaturesFlagKey,
);
const __lobeCloudDesktopInitialValue = __lobeCloudDesktopExistingDescriptor?.get
  ? __lobeCloudDesktopExistingDescriptor.get.call(globalThis)
  : globalThis[__lobeCloudDesktopBusinessFeaturesFlagKey];

Object.defineProperty(globalThis, __lobeCloudDesktopBusinessFeaturesFlagKey, {
  configurable: true,
  get() {
    return ENABLE_BUSINESS_FEATURES;
  },
  set(value) {
    __lobeCloudDesktopApplyBusinessFeaturesFlag(value);
  },
});

__lobeCloudDesktopApplyBusinessFeaturesFlag(__lobeCloudDesktopInitialValue);
`;
};

function cloudDesktopBusinessConstPlugin(): PluginOption {
  return {
    enforce: 'pre',
    async resolveId(id, importer) {
      if (id !== BUSINESS_CONST_MODULE_ID) return;

      const resolved = await this.resolve(CLOUD_BUSINESS_CONST_MODULE_ID, importer, {
        skipSelf: true,
      });
      if (!resolved) throw new Error(`Cannot resolve ${CLOUD_BUSINESS_CONST_MODULE_ID}`);

      return `${resolved.id}${DYNAMIC_BUSINESS_CONST_QUERY}`;
    },
    load(id) {
      if (!id.endsWith(DYNAMIC_BUSINESS_CONST_QUERY)) return;

      const sourcePath = id.slice(0, -DYNAMIC_BUSINESS_CONST_QUERY.length);
      return injectDynamicBusinessFeatureFlag(readFileSync(sourcePath, 'utf8'));
    },
    name: 'lobe-cloud-desktop-business-const',
    transformIndexHtml() {
      return [
        {
          children: createBusinessFeaturesBootstrapScript(),
          injectTo: 'head-prepend',
          tag: 'script',
        },
      ];
    },
  };
}

const cloudTsconfigPathsPlugin = () =>
  ({
    ...tsconfigPaths({ projects: [path.resolve(CLOUD_ROOT_DIR, 'tsconfig.json')] }),
    name: 'lobe-cloud-desktop-tsconfig-paths',
  }) satisfies PluginOption;

export default defineConfig(async (env) => {
  const { mode } = env;
  loadDesktopEnv(mode);

  const isCloudDesktop = isCloudDesktopBuild();

  const config = {
    // Absolute base: relative asset URLs break in the popup window because its
    // SPA URL (`/popup/agent/:aid/:tid`) is deep enough that relative resolution
    // lands at `/popup/assets/...` instead of the actual `/assets/...`. Our
    // `app://` protocol handler resolves absolute `/assets/...` correctly
    // regardless of URL depth.
    base: '/',
    build: {
      minify: true,
      modulePreload: { ...sharedModulePreload, polyfill: false },
      outDir: RENDERER_OUT_DIR,
      reportCompressedSize: false,
      rolldownOptions: {
        input: RENDERER_INPUTS,
        output: createSharedRolldownOutput({
          initialEntries: { ...RENDERER_INPUTS, ...canvasRuntimeEntries },
          strictExecutionOrder: true,
        }),
        preserveEntrySignatures: 'allow-extension',
      },
      sourcemap: false,
      target: RENDERER_CHROME_TARGET,
    },
    define: {
      ...sharedRendererDefine({ isElectron: true, isMobile: false }),
      __MAIN_VERSION__: JSON.stringify(desktopPackageJson.version),
    },
    envDir: __dirname,
    envPrefix: ['RENDERER_VITE_', 'VITE_'],
    optimizeDeps: sharedOptimizeDeps,
    plugins: [
      isCloudDesktop && cloudTsconfigPathsPlugin(),
      isCloudDesktop && cloudDesktopBusinessConstPlugin(),
      electronDesktopHtmlPlugin(),
      devLoadingProgress(),
      reactDevtoolsPlugin(),
      excludeWebSpaBuildArtifactsPlugin(),
      vanillaExtractPlugin(),
      ...(sharedRendererPlugins({
        platform: 'desktop',
        stylexOverlaySrcDirs: isCloudDesktop ? [path.resolve(CLOUD_ROOT_DIR, 'src')] : undefined,
      }) as PluginOption[]),
    ],
    resolve: {
      alias: sharedRendererAlias(),
      dedupe: sharedRendererDedupe,
      tsconfigPaths: !isCloudDesktop,
    },
    root: ROOT_DIR,
    // In dev the BrowserWindow loads `app://renderer/` and the Electron main process
    // proxies non-backend requests to this Vite dev server via `net.fetch`. The HMR
    // WebSocket still connects directly (browser β†’ ws://localhost:<port>) β€” so the
    // port MUST be deterministic. `strictPort` fails fast on conflict instead of
    // silently sliding, and `clientPort` baked into the HMR injection has to match.
    server: {
      hmr: {
        clientPort: DEV_VITE_PORT,
        host: '127.0.0.1',
        protocol: 'ws',
      },
      // Force IPv4 so main-process `fetch` skips happy-eyeballs dual-stack
      // attempts that surface as ETIMEDOUT under cold-start request bursts.
      host: '127.0.0.1',
      port: DEV_VITE_PORT,
      strictPort: true,
    },
  } satisfies UserConfig;

  return applyDesktopViteConfigExtension('renderer', config, env);
});