import { compile } from "vue"; import { loadEnv } from 'vite' import { H3Event, type EventHandlerRequest } from 'h3' import type { PrerenderRoute } from 'nitropack' const env = loadEnv(process.env.NODE_ENV, process.cwd(), '') // https://nuxt.com/docs/api/configuration/nuxt-config export default ({ app: { head: { title: "KEP图形处理分析软件Web版", charset: 'utf-8', htmlAttrs: { lang: 'zh-CN' }, link: [ { rel: 'icon', type: 'image/png', href: '/img/KEP001.png' } ] }, buildAssetsDir: '/_nuxt/', // 默认值 }, // ssr: true, debug: false, runtimeConfig: { public: { wsUrl: env.VITE_WS_URL || '配置未成功读取,请检查配置文件', // WebSocket URL // 图片加载路径 imgUrl: env.VITE_IMG_URL || '配置未成功读取,请检查配置文件', // 数据缓存目录 dataDir: env.VITE_DATA_DIR || '配置未成功读取,请检查配置文件', // 请求后端API地址 apiBaseUrl: env.VITE_API_URL || '配置未成功读取,请检查配置文件', apiTimeout: env.VITE_API_TIMEOUT ? parseInt(env.VITE_API_TIMEOUT) : 10000, // API请求超时时间 signalRUrl: env.VITE_SIGNALR_URL || 'SIGNALR访问路径未加载成功', baseURL: env.NUXT_PUBLIC_API_BASE || '配置未成功读取,请检查配置文件', cookieName: 'auth_data', } }, compatibilityDate: '2025-05-15', devtools: { enabled: false }, typescript: { strict: true, typeCheck: true, shim: false, tsConfig: { compilerOptions: { // 允许更灵活的类型赋值 strictNullChecks: false } } }, css: ['~/assets/css/main.css', '~/assets/css/reset.css', 'element-plus/dist/index.css', '~/assets/css/element-overrides.scss', '~/assets/css/transitions.css'], modules: ["@pinia/nuxt", '@element-plus/nuxt', 'nuxt-proxy', '@nuxtjs/device'], elementPlus: { importStyle: 'css', importLocale: 'zh-CN', }, // 确保正确处理环境变量 // sourcemap: env.NODE_ENV !== 'production', build: { // 添加图片加载器 loaders: { imgUrl: { limit: 8192 } // 小于8KB转base64 }, transpile: ['element-plus', 'vue-toastification'] }, image: { presets: { custom: { modifiers: { format: 'webp' } } } }, icon: { customCollections: [ { prefix: 'c-icon', dir: './assets/icons' } ] }, nitro: { preset: 'node-server', // 缩小捆绑包 minify: true, // 关闭源映射生成 sourceMap: false, experimental: { websocket: true // 启用 WebSocket 支持 }, devProxy: { // '/event': { // target: env.VITE_WS_URL || '配置未成功读取,请检查配置文件', // changeOrigin: true, // ws: true, // 启用 WebSocket 代理 // }, // '/drawHub': { // target: env.VITE_SIGNALR_URL || '配置未成功读取,请检查配置文件', // changeOrigin: true, // ws: true // }, // '/api/': { // target: env.VITE_API_URL, // changeOrigin: true, // rewrite: (path: string) => path.replace(/^\/api/, ''), // configure: (proxy: any) => { // proxy.on('proxyReq', (proxyReq: any) => { // // 确保代理传递所有方法 // // proxyReq.setHeader('Access-Control-Allow-Origin', 'http://localhost:3001'); // proxyReq.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); // proxyReq.setHeader('Access-Control-Allow-Headers', 'Content-Type,Authorization'); // proxyReq.setHeader('Access-Control-Allow-Credentials', 'true'); // }); // } // }, // server: { // cors: { // origin: [ // 'http://localhost:3001' // ], // credentials: true // } // } }, // routeRules: { // '/api/**': { // cors: true, // headers: { // 'Access-Control-Allow-Credentials': 'true', // 'Access-Control-Allow-Origin': process.env.ALLOWED_ORIGIN || '*' // } // } // } // '/drawHub': { // target: env.VITE_SIGNALR_URL || '配置未成功读取,请检查配置文件', // changeOrigin: true, // ws: true // }, // server: { // cors: { // origin: [ // 'http://localhost:3000' // ], // credentials: true // } // }, // '/api': { // target: 'http://localhost:10001', // changeOrigin: true, // headers: { // 'X-Proxy': 'Nuxt-Dev-Server' // }, // 添加这些CORS相关头,确保代理请求正确处理 // onResponse: (event, { response, options }) => { // // 设置CORS头,确保前端可以正确接收响应 // response.headers.set('Access-Control-Allow-Origin', 'http://localhost:3000'); // response.headers.set('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS'); // response.headers.set('Access-Control-Allow-Headers', 'Content-Type,Authorization'); // response.headers.set('Access-Control-Allow-Credentials', 'true'); // } // } }, // server: { // host: '0.0.0.0', // port: 3001, // }, pinia: { // 指定store目录 storeDir: "./stores", autoImports: [ 'defineStore', 'acceptHMRUpdate', ['defineStore', 'definePiniaStore'] ] }, imports: { dirs: ['stores', 'composables', 'services'] }, plugins: [ //'~/plugins/drawerRuler.js '~/plugins/element-plus.client.ts', '~/plugins/progressBar.client.ts', '~/plugins/echarts.client.ts', '~/plugins/cookie.client.ts', '~/plugins/cookie.server.ts', '~/plugins/auth-init.client.ts', ], vite: { css: { preprocessorOptions: { scss: { additionalData: ` @use "@/assets/element-variables.scss" as *; ` } } }, // plugins: [ // { // name: 'log-env-vars', // configResolved(config:any) { // console.log('=== Vite 加载的环境变量 ==='); // console.log(config.env); // 检查是否包含 VITE_XXX 变量 // } // } // ] }, hooks: { // 在服务端构建完成后打印环境变量 // 'build:done': () => { // console.log('=== 环境变量加载结果 ==='); // console.log(process.env); // 检查是否包含目标变量 // }, // 'vue.setup'(app: any) { // // // if (typeof window !== 'undefined') { // require('resize-observer-polyfill') // } // }, // 'pages.extend'(pages) { // pages.forEach((page) => { // if (page.path.startsWith('/')) { // page.meta = page.meta || {}; // page.meta.requiresAuth = true; // } // }); // } // hooks: { // 'prerender:routes'(routes: PrerenderRoute[]) { // console.log('Prerendering routes:', routes) // } // } }, // routeRules: { // '/**': { // meta: { requiresAuth: true } // 自动为匹配路径添加 meta // } // } router: { middleware: ['auth'] } })