2023-11-30 10:49:45 +08:00
|
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
|
|
|
import vue from '@vitejs/plugin-vue'
|
|
|
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
|
|
|
// https://vitejs.dev/config/
|
|
|
|
|
import { resolve } from 'path'
|
|
|
|
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
|
|
|
import Components from 'unplugin-vue-components/vite'
|
2023-12-04 10:33:27 +08:00
|
|
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
2023-11-30 10:49:45 +08:00
|
|
|
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 ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defineConfig({
|
2024-12-27 09:52:19 +08:00
|
|
|
base: '/mall-view/',
|
2023-11-30 10:49:45 +08:00
|
|
|
plugins: [
|
|
|
|
|
vue(),
|
|
|
|
|
vueJsx(),
|
|
|
|
|
AutoImport({
|
|
|
|
|
imports: ['vue', 'vue-router', 'pinia'],
|
|
|
|
|
}),
|
|
|
|
|
Components({
|
|
|
|
|
resolvers: [ElementPlusResolver()],
|
|
|
|
|
}),
|
|
|
|
|
createHtmlPlugin({
|
|
|
|
|
inject: {
|
|
|
|
|
data: {
|
|
|
|
|
injectTestResource: getTestResource()
|
|
|
|
|
// 动态插入js脚本
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
// 打包文件
|
|
|
|
|
],
|
|
|
|
|
resolve: {
|
|
|
|
|
// 路径别名配置
|
|
|
|
|
alias: {
|
|
|
|
|
'@': 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'),
|
2023-12-04 17:42:11 +08:00
|
|
|
http: resolve(__dirname, './src/http'),
|
2023-11-30 10:49:45 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
css: {
|
|
|
|
|
preprocessorOptions: {
|
|
|
|
|
scss: {
|
|
|
|
|
// 两种方式都可以
|
2024-12-17 10:57:09 +08:00
|
|
|
additionalData: "@import '@/style/scss/index.scss';",
|
2023-11-30 10:49:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
server: {
|
|
|
|
|
host: '0.0.0.0',
|
2024-12-25 14:24:34 +08:00
|
|
|
port: 8102,
|
2023-11-30 10:49:45 +08:00
|
|
|
// port: Number(envInfo.VITE_PORT),
|
|
|
|
|
// open: envInfo.VITE_OPEN,
|
|
|
|
|
proxy: {
|
|
|
|
|
'/proxyApi': {
|
2023-12-04 10:19:21 +08:00
|
|
|
target: envInfo.VITE_proxyTarget,
|
2023-11-30 10:49:45 +08:00
|
|
|
secure: false,
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
rewrite: (path) => path.replace(/^\/proxyApi/, ''),
|
|
|
|
|
configure: (proxy, _options) => {
|
|
|
|
|
proxy.on('error', (err, _req, _res) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
// console.log('proxy error', err)
|
2023-11-30 10:49:45 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
proxy.on('start', (req, res, target) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
// console.log(
|
|
|
|
|
// 'Proxy Start:',
|
|
|
|
|
// req.method,
|
|
|
|
|
// req.url,
|
|
|
|
|
// req.headers,
|
|
|
|
|
// _options
|
|
|
|
|
// )
|
2023-11-30 10:49:45 +08:00
|
|
|
})
|
|
|
|
|
proxy.on('proxyReq', (proxyReq, req, _res) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
// console.log(
|
|
|
|
|
// 'Sending Request to the Target:',
|
|
|
|
|
// req.method,
|
|
|
|
|
// req.url,
|
|
|
|
|
// req.headers,
|
|
|
|
|
// _options
|
|
|
|
|
// )
|
2023-11-30 10:49:45 +08:00
|
|
|
proxyReq.removeHeader('origin')
|
|
|
|
|
// 跨域解决
|
|
|
|
|
})
|
|
|
|
|
proxy.on('proxyRes', (proxyRes, req, _res) => {
|
2023-12-04 10:19:21 +08:00
|
|
|
// console.log(
|
|
|
|
|
// 'Received Response from the Target:',
|
|
|
|
|
// proxyRes.statusCode,
|
|
|
|
|
// proxyRes.headers,
|
|
|
|
|
// req.url
|
|
|
|
|
// )
|
2023-11-30 10:49:45 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
hmr: {
|
|
|
|
|
overlay: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
envDir: 'env',
|
|
|
|
|
build: {
|
2023-12-04 13:10:38 +08:00
|
|
|
outDir: 'dist/' + pjson.name + '-' + envInfo.VITE_BUILD_MODE,
|
2023-11-30 10:49:45 +08:00
|
|
|
emptyOutDir: true,
|
|
|
|
|
terserOptions: {
|
|
|
|
|
compress: {
|
|
|
|
|
drop_console: isProduction, //生产正式 去除
|
|
|
|
|
drop_debugger: isProduction //生产正式 去除
|
|
|
|
|
}
|
2024-11-25 18:00:42 +08:00
|
|
|
},
|
|
|
|
|
minify: 'terser',
|
2023-11-30 10:49:45 +08:00
|
|
|
/*
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
output: {
|
|
|
|
|
manualChunks(id) {
|
|
|
|
|
if (id.includes('node_modules')) {
|
|
|
|
|
return id
|
|
|
|
|
.toString()
|
|
|
|
|
.split('node_modules/')[1]
|
|
|
|
|
.split('/')[0]
|
|
|
|
|
.toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|