Use vite's development proxy for api-override

This commit is contained in:
Julian Stirling 2026-05-10 14:27:50 +01:00
parent a58007a98c
commit 2b8d08c78c
4 changed files with 250 additions and 978 deletions

View file

@ -4,58 +4,79 @@
// Note: Comments are generated to explain relevant configuration options.
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), "");
build: {
// Output directory for production builds
outDir: "../src/openflexure_microscope_server/static",
// Clear output directory before build
emptyOutDir: true,
chunkSizeWarningLimit: 400,
rollupOptions: {
output: {
manualChunks: {
"vue-vendor": ["vue", "pinia", "pinia-plugin-persistedstate"],
openseadragon: ["openseadragon"],
uikit: ["uikit"],
utils: ["axios", "mitt", "mousetrap", "@vueuse/core"],
var target;
if (command === "serve") {
if (env.VITE_API_TARGET === "local") {
target = "http://localhost:5000";
} else if (env.VITE_API_TARGET === "microscope") {
target = env.VITE_MICROSCPOPE_HOST || "http://microscope.local:5000";
} else {
throw new Error(`Invalid VITE_API_TARGET: ${env.VITE_API_TARGET}`);
}
console.log(`Proxying /api → ${target}`);
}
return {
plugins: [vue()],
build: {
// Output directory for production builds
outDir: "../src/openflexure_microscope_server/static",
// Clear output directory before build
emptyOutDir: true,
chunkSizeWarningLimit: 400,
rollupOptions: {
output: {
manualChunks: {
"vue-vendor": ["vue", "pinia", "pinia-plugin-persistedstate"],
openseadragon: ["openseadragon"],
uikit: ["uikit"],
utils: ["axios", "mitt", "mousetrap", "@vueuse/core"],
},
},
},
},
},
resolve: {
alias: {
// Setup path alias for src directory.
// This allows importing modules using '@/path/to/module'.
"@": fileURLToPath(new URL("./src", import.meta.url)),
resolve: {
alias: {
// Setup path alias for src directory.
// This allows importing modules using '@/path/to/module'.
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
// Recognize these file extensions for module resolution.
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
},
// Recognize these file extensions for module resolution.
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
},
server: {
host: true,
// Set the development server port to 8080.
// OFM uses port 5000, so we avoid conflicts. run, and override by using the address:
// http://microscope.local:8080/?overrideOrigin=http://microscope.local:5000/api/v3#
port: 8080,
strictPort: true,
},
css: {
preprocessorOptions: {
less: {
// Always include math in Less files.
math: "always",
// Enable relative URLs in Less files.
relativeUrls: true,
// Enable JavaScript in Less files
javascriptEnabled: true,
server: {
host: true,
// Set the development server port to 8080.
port: 8080,
strictPort: true,
proxy: {
"/api": {
target,
changeOrigin: true,
},
},
},
},
css: {
preprocessorOptions: {
less: {
// Always include math in Less files.
math: "always",
// Enable relative URLs in Less files.
relativeUrls: true,
// Enable JavaScript in Less files
javascriptEnabled: true,
},
},
},
};
});