106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { defineConfig } from "vite";
|
||
import react from "@vitejs/plugin-react";
|
||
import { templatePreviewCss } from "./template-preview-css.mjs";
|
||
|
||
const adminCsp = [
|
||
"default-src 'self'",
|
||
"img-src 'self' data: blob:",
|
||
"media-src 'self' data: blob:",
|
||
"style-src 'self' 'unsafe-inline'",
|
||
"font-src 'self' data:",
|
||
"connect-src 'self' ws: wss:",
|
||
"script-src 'self'",
|
||
"object-src 'none'",
|
||
"base-uri 'none'",
|
||
"frame-ancestors 'self'",
|
||
"form-action 'self'",
|
||
].join("; ");
|
||
const adminDevelopmentCsp = adminCsp.replace(
|
||
"script-src 'self'",
|
||
"script-src 'self' 'unsafe-inline'",
|
||
);
|
||
|
||
export default defineConfig({
|
||
plugins: [
|
||
react(),
|
||
{
|
||
name: "admin-production-csp",
|
||
// Vite dev preamble 是 inline script;生产构建保留 meta CSP,dev/preview 用响应头控制。
|
||
transformIndexHtml(html, context) {
|
||
const isDevelopmentServe =
|
||
context.server?.config.command === "serve" && !context.server?.config.isProduction;
|
||
return isDevelopmentServe
|
||
? html.replace(/\s*<meta\s+http-equiv="Content-Security-Policy"[^>]*>/, "")
|
||
: html;
|
||
},
|
||
},
|
||
],
|
||
css: { postcss: { plugins: [templatePreviewCss()] } },
|
||
resolve: {
|
||
dedupe: ["react", "react-dom"],
|
||
alias: {
|
||
"@tarojs/components": `${process.cwd()}/src/features/brand/taro-web-components.jsx`,
|
||
"@tarojs/taro": `${process.cwd()}/src/features/brand/taro-web-runtime.jsx`,
|
||
},
|
||
},
|
||
define: {
|
||
"process.env": "{}",
|
||
"process.env.TARO_PLATFORM": JSON.stringify("web"),
|
||
"process.env.TARO_ENV": JSON.stringify("h5"),
|
||
PREVIEW_ALL_TEMPLATES: JSON.stringify(true),
|
||
TEMPLATE_MANIFEST: JSON.stringify({ navigation: [] }),
|
||
},
|
||
server: {
|
||
host: "0.0.0.0",
|
||
port: Number(process.env.ADMIN_PORT ?? 3000),
|
||
// Vite dev preamble 需要 inline script;preview/生产保留严格 CSP。
|
||
headers: {
|
||
"Content-Security-Policy": adminDevelopmentCsp,
|
||
"Referrer-Policy": "no-referrer",
|
||
"X-Content-Type-Options": "nosniff",
|
||
},
|
||
base: process.env.VITE_APP_BASE_PATH ?? "/",
|
||
proxy: {
|
||
"/api": {
|
||
target: process.env.VITE_DEV_API_TARGET ?? "http://localhost:3001",
|
||
rewrite: (path) => path.replace(/^\/api/, ""),
|
||
},
|
||
"/media": {
|
||
target: process.env.VITE_MEDIA_API_TARGET ?? "http://localhost:3001",
|
||
changeOrigin: true,
|
||
},
|
||
...(process.env.VITE_MEDIA_PROXY_TARGET
|
||
? {
|
||
[`/${(process.env.OBJECT_STORAGE_BUCKET ?? "personal-brand-media").replace(/^\/+/, "")}`]:
|
||
{
|
||
target: process.env.VITE_MEDIA_PROXY_TARGET,
|
||
changeOrigin: true,
|
||
},
|
||
}
|
||
: {}),
|
||
},
|
||
},
|
||
preview: {
|
||
// 响应头在 dev/preview 均生效;meta CSP 是静态托管环境的基础防线。
|
||
headers: {
|
||
"Content-Security-Policy": adminCsp,
|
||
"Referrer-Policy": "no-referrer",
|
||
"X-Content-Type-Options": "nosniff",
|
||
},
|
||
base: process.env.VITE_APP_BASE_PATH ?? "/",
|
||
},
|
||
build: {
|
||
outDir: "dist",
|
||
rollupOptions: {
|
||
output: {
|
||
manualChunks(id) {
|
||
if (id.includes("/node_modules/.pnpm/react-dom@")) return "react-runtime";
|
||
if (id.includes("/node_modules/.pnpm/react@")) return "react-runtime";
|
||
if (id.includes("/node_modules/.pnpm/scheduler@")) return "react-runtime";
|
||
return undefined;
|
||
},
|
||
},
|
||
},
|
||
},
|
||
});
|