BigScreenUI/vite.config.ts

155 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2023-11-30 13:40:30 +08:00
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
2023-11-30 10:48:06 +08:00
// https://vitejs.dev/config/
2023-11-30 13:40:30 +08:00
import { resolve } from 'path'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createHtmlPlugin } from 'vite-plugin-html'
// @ts-ignore
import pjson from './package.json'
const now = new Date()
export default ({ mode }: any) => {
const envInfo = loadEnv(mode, process.cwd() + '/env')
const isProduction = mode == 'production' //正式生产环境
const isSIT = ['sit', 'dev', 'development'].includes(mode)
const isUAT = ['uat'].includes(mode)
function getTestResource(): string {
if (isSIT) {
return `https://testSit.com`
} else if (isUAT) {
return `https://testUat.com`
} else if (mode === 'production') {
return `https://production.com`
}
return ''
}
2023-11-30 10:48:06 +08:00
2023-11-30 13:40:30 +08:00
return defineConfig({
base: './',
plugins: [
vue(),
vueJsx(),
AutoImport({
imports: ['vue', 'vue-router', 'pinia'],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
createHtmlPlugin({
inject: {
data: {
injectTestResource: getTestResource()
// 动态插入js脚本
}
}
}),
// 打包文件
],
2023-11-30 10:48:06 +08:00
resolve: {
2023-11-30 13:40:30 +08:00
// 路径别名配置
2023-11-30 10:48:06 +08:00
alias: {
2023-11-30 13:40:30 +08:00
'@': resolve(__dirname, './src'),
assets: resolve(__dirname, './src/assets'),
components: resolve(__dirname, './src/components'),
store: resolve(__dirname, './src/store'),
views: resolve(__dirname, './src/views'),
router: resolve(__dirname, './src/router'),
utils: resolve(__dirname, './src/utils'),
http: resolve(__dirname, './src/http')
}
},
css: {
preprocessorOptions: {
scss: {
// 两种方式都可以
additionalData:
2023-11-30 18:37:22 +08:00
"@import '@/style/scss/index.scss';"
2023-11-30 13:40:30 +08:00
}
}
2023-11-30 10:48:06 +08:00
},
server: {
2023-11-30 13:40:30 +08:00
host: '0.0.0.0',
// port: Number(envInfo.VITE_PORT),
// open: envInfo.VITE_OPEN,
2023-11-30 10:48:06 +08:00
proxy: {
2023-11-30 13:40:30 +08:00
'/proxyApi': {
2023-12-01 15:05:38 +08:00
target: envInfo.VITE_proxyTarget,
2023-11-30 13:40:30 +08:00
secure: false,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/proxyApi/, ''),
configure: (proxy, _options) => {
proxy.on('error', (err, _req, _res) => {
2023-12-02 17:14:56 +08:00
// console.log('proxy error', err)
2023-11-30 13:40:30 +08:00
})
proxy.on('start', (req, res, target) => {
2023-12-02 17:14:56 +08:00
// console.log(
// 'Proxy Start:',
// req.method,
// req.url,
// req.headers,
// _options
// )
2023-11-30 13:40:30 +08:00
})
proxy.on('proxyReq', (proxyReq, req, _res) => {
2023-12-02 17:14:56 +08:00
// console.log(
// 'Sending Request to the Target:',
// req.method,
// req.url,
// req.headers,
// _options
// )
2023-11-30 13:40:30 +08:00
proxyReq.removeHeader('origin')
// 跨域解决
})
proxy.on('proxyRes', (proxyRes, req, _res) => {
2023-12-02 17:14:56 +08:00
// console.log(
// 'Received Response from the Target:',
// proxyRes.statusCode,
// proxyRes.headers,
// req.url
// )
2023-11-30 13:40:30 +08:00
})
}
}
2023-11-30 10:48:06 +08:00
},
2023-11-30 13:40:30 +08:00
hmr: {
overlay: true
}
2023-11-30 10:48:06 +08:00
},
2023-11-30 13:40:30 +08:00
envDir: 'env',
2023-11-30 10:48:06 +08:00
build: {
2023-12-01 15:05:38 +08:00
outDir: 'dist/' + pjson.name +'-'+envInfo.VITE_BUILD_MODE,
2023-11-30 13:40:30 +08:00
emptyOutDir: true,
terserOptions: {
compress: {
drop_console: isProduction, //生产正式 去除
drop_debugger: isProduction //生产正式 去除
}
}
/*
2023-11-30 10:48:06 +08:00
rollupOptions: {
2023-11-30 13:40:30 +08:00
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
return id
.toString()
.split('node_modules/')[1]
.split('/')[0]
.toString()
}
}
}
}
*/
}
})
}