Files
inventory/inventory/vite.config.ts

94 lines
2.6 KiB
TypeScript

import path from "path"
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { loadEnv } from "vite"
import fs from 'fs-extra'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "")
const isDev = mode === 'development'
return {
plugins: [
react(),
{
name: 'copy-build',
closeBundle: async () => {
if (!isDev) {
const sourcePath = path.resolve(__dirname, 'build');
const targetPath = path.resolve(__dirname, '../inventory-server/frontend/build');
try {
await fs.ensureDir(path.dirname(targetPath));
await fs.remove(targetPath);
await fs.copy(sourcePath, targetPath);
console.log('Build files copied successfully to server directory!');
} catch (error) {
console.error('Error copying build files:', error);
process.exit(1);
}
}
}
}
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
server: {
host: "0.0.0.0",
port: 5173,
proxy: isDev ? {
"/api": {
target: "https://inventory.kent.pw",
changeOrigin: true,
secure: false,
ws: true,
xfwd: true,
cookieDomainRewrite: "",
withCredentials: true,
rewrite: (path) => path.replace(/^\/api/, "/api"),
configure: (proxy, _options) => {
proxy.on("error", (err, req, res) => {
console.log("API proxy error:", err)
res.writeHead(500, {
"Content-Type": "application/json",
})
res.end(
JSON.stringify({ error: "Proxy Error", message: err.message })
)
})
proxy.on("proxyReq", (proxyReq, req, _res) => {
console.log("Outgoing request:", {
method: req.method,
url: req.url,
headers: proxyReq.getHeaders(),
})
})
proxy.on("proxyRes", (proxyRes, req, _res) => {
console.log("Proxy response:", {
statusCode: proxyRes.statusCode,
url: req.url,
headers: proxyRes.headers,
})
})
},
},
} : {},
},
build: {
outDir: "build",
sourcemap: true,
rollupOptions: {
output: {
manualChunks: {
vendor: ["react", "react-dom", "react-router-dom"],
},
},
},
},
}
})