Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

3.6 KB
Raw Blame
import path from 'node:path'
import react from '@vitejs/plugin-react'
import { defineConfig, loadEnv } from 'vite'
// Self-import of the shell's own Vite integration — the same composition
// hosts get from `@spree/dashboard/vite`. It wraps the TanStack Router
// generator, so the shell's committed routeTree.gen.ts is produced by the
// exact machinery hosts use (with VITE_EXAMPLE_PLUGIN=true, including the
// example plugin's file routes).
import { spreeDashboardPlugin } from './src/vite/index.ts'

export default defineConfig(({ mode }) => {
  // loadEnv, not process.env: Vite doesn't load .env files before evaluating
  // this config — this way both shell env (E2E) and .env.local work.
  const env = loadEnv(mode, process.cwd(), 'VITE_')
  const proxyTarget = env.VITE_API_PROXY_TARGET || 'http://localhost:3000'

  return {
    // Sub-path mounting for the single-node topology: the official Docker image
    // builds with VITE_BASE_PATH=/dashboard/ so asset URLs resolve under the
    // Rails-served mount. Unset (dev, CDN root deploys) Vite defaults to '/'.
    base: process.env.VITE_BASE_PATH,
    // The plugin bundles `@tailwindcss/vite`, so we don't register it
    // separately. `cssEntry` defaults to `./src/styles.css`, which matches our
    // entry — pass it here only as a hint for readers of this config.
    //
    // Plugin selection: hosts normally omit `plugins` so auto-discovery picks
    // up every dep with the `spree.dashboard.plugin` marker. This monorepo
    // copy defaults to an explicit empty whitelist because our only marked dep
    // is the reference plugin (@spree/dashboard-plugin-example, a
    // devDependency) — opt in with VITE_EXAMPLE_PLUGIN=true to exercise the
    // real discovery + activation path end-to-end.
    plugins: [
      spreeDashboardPlugin({
        cssEntry: './src/styles.css',
        plugins: process.env.VITE_EXAMPLE_PLUGIN === 'true' ? undefined : [],
      }),
      react(),
    ],
    resolve: {
      alias: {
        '@': path.resolve(import.meta.dirname, './src'),
      },
    },
    // Proxy /api to the Rails server so the SPA is same-origin with the API in
    // dev. Same-origin keeps the refresh-token cookie working under
    // SameSite=Lax without needing HTTPS (production cross-origin uses
    // SameSite=None; Secure).
    //
    // `VITE_API_PROXY_TARGET` is the proxy *target* — deliberately distinct
    // from `VITE_SPREE_API_URL` (the SDK's build-time switch to absolute URLs,
    // for deploys on a different origin): setting that in dev would bypass
    // this proxy and break on CORS and the SameSite=Lax cookie. The E2E suite
    // uses the proxy target to point at the test Rails port.
    server: {
      proxy: {
        '/api': { target: proxyTarget, changeOrigin: true },
        // Active Storage's Disk service issues presigned URLs against
        // `/rails/active_storage/...` on the Rails origin. Without proxying,
        // the SPA's PUT from :5173 hits :3000 cross-origin and the browser
        // blocks it ("Failed to fetch") because ActiveStorage::DiskController
        // doesn't speak CORS. Proxying keeps it same-origin.
        '/rails': { target: proxyTarget, changeOrigin: true },
      },
    },
    // `vite preview` inherits `server.proxy` by default, and the E2E suite
    // depends on that when it serves the built SPA on CI — pin the proxy
    // explicitly so a future `preview` block can't silently drop it.
    preview: {
      proxy: {
        '/api': { target: proxyTarget, changeOrigin: true },
        '/rails': { target: proxyTarget, changeOrigin: true },
      },
    },
  }
})