leaflet_svelte/vite.config.js
2026-04-22 01:27:38 +09:00

41 lines
1.3 KiB
JavaScript

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, loadEnv } from 'vite';
import { mockApiPlugin } from './mocks/vitePlugin';
/**
* Three dev modes are supported:
*
* 1. VITE_USE_MOCK_API=true
* → serve fake data from ./mocks. No backend needed.
*
* 2. VITE_API_BASE_URL starts with '/'
* → client makes same-origin requests; the dev server proxies
* VITE_API_BASE_URL to VITE_API_PROXY_TARGET (default
* http://localhost:8000). This matches production where the web
* server routes /api to Django.
*
* 3. VITE_API_BASE_URL is an absolute URL
* → client talks to it directly. CORS must be enabled on that host.
* No proxy is registered.
*/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const useMock = env.VITE_USE_MOCK_API === 'true';
const base = env.VITE_API_BASE_URL ?? '/api';
const proxyTarget = env.VITE_API_PROXY_TARGET ?? 'http://localhost:8000';
const shouldProxy = !useMock && base.startsWith('/');
return {
plugins: [sveltekit(), ...(useMock ? [mockApiPlugin()] : [])],
server: {
proxy: shouldProxy
? {
[base]: {
target: proxyTarget,
changeOrigin: true,
},
}
: undefined,
},
};
});