Jpom/web-vue/vite.config.ts

109 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-04-08 13:43:03 +08:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import path from 'node:path'
2023-04-12 13:24:50 +08:00
import { ConfigEnv, defineConfig, loadEnv } from 'vite'
2023-04-08 13:43:03 +08:00
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
2023-04-09 15:49:02 +08:00
import { createHtmlPlugin } from 'vite-plugin-html'
2023-04-08 13:43:03 +08:00
2023-04-09 17:44:47 +08:00
//自动导入vue中hook reactive ref等
import AutoImport from 'unplugin-auto-import/vite'
//自动导入ui-组件 比如说ant-design-vue element-plus等
import Components from 'unplugin-vue-components/vite'
//ant-design-vue
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
2023-04-08 13:43:03 +08:00
// https://vitejs.dev/config/
2023-04-12 13:24:50 +08:00
export default defineConfig(({ mode }: ConfigEnv) => {
2023-04-09 12:33:58 +08:00
// 加载环境配置
2023-04-12 13:24:50 +08:00
const env: Record<string, string> = loadEnv(mode, __dirname, 'JPOM')
const { JPOM_PROXY_HOST: HOST, JPOM_BASE_URL, JPOM_PORT }: Record<string, string> = env
2023-04-09 12:33:58 +08:00
console.log(env, `当前为${mode}环境`)
2023-04-08 13:43:03 +08:00
2023-04-09 12:33:58 +08:00
return {
base: JPOM_BASE_URL, // 公共基础路径如当值为jpom时网站访问路径为https://jpom.top/jpom
envPrefix: 'JPOM_', // 可在项目中通过import.meta.env.JPOM_xxx获取环境变量
resolve: {
alias: {
2023-04-09 17:44:47 +08:00
'@/': `${path.resolve(__dirname, 'src')}/`
2023-04-09 12:33:58 +08:00
},
2024-01-09 22:09:12 +08:00
2023-04-09 12:33:58 +08:00
// 忽略后缀名的配置选项
2023-04-09 17:44:47 +08:00
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
2023-04-09 12:33:58 +08:00
},
build: {
sourcemap: mode !== 'production', // 非生产环境都生成sourcemap
2024-01-08 18:42:21 +08:00
outDir: '../modules/server/src/main/resources/dist'
2023-04-09 12:33:58 +08:00
},
server: {
2023-04-12 13:24:50 +08:00
port: Number(JPOM_PORT),
2024-01-06 23:38:14 +08:00
host: '0.0.0.0',
2023-04-09 12:33:58 +08:00
proxy: {
// http
'/api': {
target: `http://${HOST}`,
changeOrigin: true,
2024-01-06 23:38:14 +08:00
ws: true,
2023-04-09 12:33:58 +08:00
rewrite: (path) => path.replace(/^\/api/, ''),
2023-04-09 17:44:47 +08:00
timeout: 10 * 60 * 1000
}
}
2023-04-09 12:33:58 +08:00
},
2023-04-09 15:49:02 +08:00
plugins: [
vue(),
vueJsx(),
2023-04-09 17:44:47 +08:00
AutoImport({
//安装两行后你会发现在组件中不用再导入refreactive等
2023-04-12 17:02:53 +08:00
imports: ['vue', 'vue-router', 'pinia'],
2023-04-09 17:44:47 +08:00
dts: 'src/d.ts/auto-import.d.ts',
//ant-design-vue
resolvers: [AntDesignVueResolver()]
}),
2023-04-12 17:02:53 +08:00
AutoImport({
dirs: ['src/d.ts/global'],
dts: 'src/d.ts/auto-global-import.d.ts'
}),
2023-04-09 17:44:47 +08:00
Components({
dts: 'src/d.ts/components.d.ts',
//ant-design-vue importStyle = false 样式就没了
2024-01-06 23:38:14 +08:00
resolvers: [AntDesignVueResolver({ importStyle: false, resolveIcons: true })]
2023-04-09 17:44:47 +08:00
}),
2023-04-09 15:49:02 +08:00
createHtmlPlugin({
minify: true,
viteNext: true,
2023-04-09 15:49:02 +08:00
inject: {
data: {
title: env.JPOM_APP_TITLE,
base_url: env.JPOM_BASE_URL,
build: new Date().getTime(),
env: process.env.NODE_ENV,
2023-04-09 17:44:47 +08:00
buildVersion: process.env.npm_package_version
}
}
})
]
2023-04-09 12:33:58 +08:00
}
2023-04-08 13:43:03 +08:00
})