import path from "path" import type { ConfigEnv } from "vite" import { defineConfig, loadEnv } from "vite" // 帮手函数,这样不用 jsdoc 注解也可以获取类型提示 import { codeInspectorPlugin } from "code-inspector-plugin" import vue from "@vitejs/plugin-vue" // import legacy from "@vitejs/plugin-legacy" import eslintPlugin from "vite-plugin-eslint" import { mars3dPlugin } from "vite-plugin-mars3d" import { createStyleImportPlugin, AndDesignVueResolve } from "vite-plugin-style-import" // const baseTargetUrl = '27.11.11.30/' const baseTargetUrl = 'c2.zklhjs.com/' export default ({ mode }: ConfigEnv) => { const root = process.cwd() // 获取 .env 文件里定义的环境变量 const ENV = loadEnv(mode, root) console.log(`当前环境信息:`, mode) console.log(`ENV:`, ENV) return defineConfig({ base: '/3dScreen/', server: { host: "0.0.0.0", port: 3003, proxy: { "/djiWeb": { // target: "https://ntaf.nuoqu.net/", // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, // target: 'http://192.168.3.215:6800', changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace("/djiWeb", "/djiWeb") }, "/system": { // target: "https://ntaf.nuoqu.net/", // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, // target: 'http://192.168.3.215:6800', changeOrigin: true, // system rewrite: (path) => path.replace("/system", "/system") }, "/dji": { // target: "https://ntaf.nuoqu.net/", // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, // target: 'http://192.168.3.215:6800', changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace("/dji", "/dji") }, '/3dterrain': { // target: 'https://ntaf.nuoqu.net/', // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace('/3dterrain', ''), }, // '/saas': { // target: 'https://ntaf.nuoqu.net/', // changeOrigin: true, // 允许跨域 // rewrite: (path) => path.replace('/saas', '/saas'), // }, "/3d-model":{ // target: 'https://ntaf.nuoqu.net/', // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace('/3d-model', '/3d-model'), }, "/dashboard":{ // target: 'https://ntaf.nuoqu.net/', // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace('/dashboard', '/dashboard'), }, // '/dji': { // target: 'http://192.168.3.3:6791/', // changeOrigin: true, // 允许跨域 // rewrite: (path) => path.replace('/dji', '/'), // }, '/dji/api/v1/ws': { // target: 'wss://ntaf.nuoqu.net', // target: "ws://221.226.33.58:18800/", target: `ws://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 ws: true, rewrite: (path) => path.replace('/', '/'), }, '/saas': { // target: 'https://ntaf.nuoqu.net/', // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace('/saas', '/saas'), }, '/config':{ // target: 'http://localhost:3003', // target: "http://221.226.33.58:18800/", target: `http://${baseTargetUrl}`, changeOrigin: true, // 允许跨域 rewrite: (path) => path.replace('/config/', '/public/config/'), } } }, define: { "process.env": { mode, BASE_URL: ENV.VITE_BASE_URL, API_BASE: ENV.VITE_API_URL, API_LOCAL: ENV.VITE_LOCAL_API, BASE_TARGET_URL: JSON.stringify(baseTargetUrl) }, buildTime: new Date() }, resolve: { alias: [{ find: '@mars', replacement: path.join(__dirname, "src"), },{ find: '/@', replacement: path.join(__dirname, "src"), },{ find: '@', replacement: path.join(__dirname, "src"), },{ find: 'mars3d-3.9.2', replacement: path.join(__dirname, "packages/mars3d-3.9.2"), },{ find: 'mars3d', replacement: path.join(__dirname, "packages/mars3d-3.9.2"), }], extensions: [".js", ".ts", ".jsx", ".tsx", ".json"] }, optimizeDeps: { include: ["kml-geojson", "mars3d"] }, json: { // 支持从 .json 文件中进行按名导入 namedExports: true, stringify: false }, css: { preprocessorOptions: { less: { javascriptEnabled: true, additionalData: `@import "${path.resolve(__dirname, "src/components/mars-ui/base.less")}";` } } }, build: { // 输出路径 outDir: path.join("./dist"), // 小于此阈值的导入或引用资源将内联为 base64 编码, 以避免额外的http请求, 设置为 0, 可以完全禁用此项, assetsInlineLimit: 4096, // 启动 / 禁用 CSS 代码拆分 cssCodeSplit: true, // 构建后是否生成 soutrce map 文件 sourcemap: false, // 自定义rollup-commonjs插件选项 commonjsOptions: { include: /node_modules|packages/, transformMixedEsModules: true }, // 静态资源目录 assetsDir: "assets", // 自定义底层的 Rollup 打包配置 rollupOptions: { input: { index: path.resolve(__dirname, "index.html"), demo: path.resolve(__dirname, "demo.html"), unmannedEquipmentScreen: path.resolve(__dirname, "unmannedEquipmentScreen.html"), dataGovernanceDashboard: path.resolve(__dirname, "dataGovernanceDashboard.html") }, output: { chunkFileNames: "assets/js/[name]-[hash].js", entryFileNames: "assets/js/[name]-[hash].js", assetFileNames: "assets/[ext]/[name]-[hash].[ext]" } }, // 当设置为 true, 构建后将会生成 manifest.json 文件 manifest: false, // 设置为 false 可以禁用最小化混淆,或是用来指定是应用哪种混淆器 boolean | 'terser' | 'esbuild' minify: "terser", // 传递给 Terser 的更多 minify 选项 terserOptions: { compress: { drop_console: false, // 在生产环境移除console.log drop_debugger: true // 在生产环境移除debugger } }, // 设置为false 来禁用将构建好的文件写入磁盘 write: true, // 默认情况下 若 outDir 在 root 目录下, 则 Vite 会在构建时清空该目录。 emptyOutDir: true }, plugins: [ vue(), // codeInspectorPlugin({ bundler: 'vite', showSwitch: true }), // 兼容老版本浏览器配置 // legacy({ // targets: ["> 5%", "last 2 major versions", "chrome >80", "not dead"], // 需要兼容的目标列表,可以设置多个,参考.browserslistrc等 // additionalLegacyPolyfills: ["regenerator-runtime/runtime"], // renderLegacyChunks: true, // polyfills: [ // "es.symbol", // "es.array.filter", // "es.promise", // "es.promise.finally", // "es/map", // "es/set", // "es.array.for-each", // "es.object.define-properties", // "es.object.define-property", // "es.object.get-own-property-descriptor", // "es.object.get-own-property-descriptors", // "es.object.keys", // "es.object.to-string", // "web.dom-collections.for-each", // "esnext.global-this", // "esnext.string.match-all" // ] // }), // eslintPlugin(), mars3dPlugin({ useStatic: false }), createStyleImportPlugin({ resolves: [AndDesignVueResolve()], libs: [ { libraryName: "ant-design-vue", esModule: true, resolveStyle: (name) => { if (name === "auto-complete") { return `ant-design-vue/es/${name}/index` } return `ant-design-vue/es/${name}/style/index` } } ] }) ] }) }