2024-03-21 16:43:05 +08:00
///
/// Copyright (c) 2019 Of Him Code Technology Studio
/// Jpom is licensed under Mulan PSL v2.
/// You can use this software according to the terms and conditions of the Mulan PSL v2.
/// You may obtain a copy of Mulan PSL v2 at:
/// http://license.coscl.org.cn/MulanPSL2
/// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
/// See the Mulan PSL v2 for more details.
///
2023-04-08 13:43:03 +08:00
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'
2024-01-16 22:18:34 +08:00
import { visualizer } from 'rollup-plugin-visualizer'
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'
2024-03-07 15:25:08 +08:00
import postcss from 'postcss'
2023-04-09 17:44:47 +08:00
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-16 16:56:59 +08:00
outDir : '../modules/server/src/main/resources/dist' ,
rollupOptions : {
output : {
// 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
entryFileNames : 'assets/js/[name].[hash].js' , // 用于命名代码拆分时创建的共享块的输出命名
chunkFileNames : 'assets/js/[name].[hash].js' , // 用于输出静态资源的命名,[ext]表示文件扩展名
assetFileNames : 'assets/[ext]/[name].[hash].[ext]'
}
2024-01-16 22:18:34 +08:00
} ,
//打包前清空文件, 默认true
emptyOutDir : true ,
module Preload : { polyfill : true } ,
polyfillModulePreload : true ,
manifest : false
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 ( {
//安装两行后你会发现在组件中不用再导入ref, reactive等
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' ,
2024-03-04 13:18:22 +08:00
eslintrc : {
enabled : true ,
filepath : '.eslintrc-auto-import.json' ,
globalsPropValue : true
} ,
2023-04-09 17:44:47 +08:00
//ant-design-vue
resolvers : [ AntDesignVueResolver ( ) ]
} ) ,
2023-04-12 17:02:53 +08:00
AutoImport ( {
dirs : [ 'src/d.ts/global' ] ,
2024-03-04 13:39:32 +08:00
dts : 'src/d.ts/auto-global-import.d.ts' ,
eslintrc : {
enabled : true ,
filepath : '.eslintrc-global-import.json' ,
globalsPropValue : true
}
2023-04-12 17:02:53 +08:00
} ) ,
2023-04-09 17:44:47 +08:00
Components ( {
dts : 'src/d.ts/components.d.ts' ,
2024-01-16 17:18:26 +08:00
//ant-design-vue
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 ,
2024-01-09 21:46:06 +08:00
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
}
}
2024-01-16 22:18:34 +08:00
} ) ,
visualizer ( {
emitFile : false ,
// file: 'states.html',
open : true
2024-03-07 15:25:08 +08:00
} ) ,
{
name : 'vite-plugin-skip-empty-css' ,
async transform ( code , id ) {
if ( /(\.css|\.scss|\.less)$/ . test ( id ) ) {
2024-03-07 15:33:41 +08:00
if ( code === '' ) return ''
2024-03-07 15:25:08 +08:00
const { root } = await postcss ( [
{
postcssPlugin : 'check-empty-or-comments-only'
}
] ) . process ( code , { from : id } )
2024-03-07 15:33:41 +08:00
if (
root . nodes . length === 0 ||
root . nodes . every ( ( node ) = > {
return node . type === 'comment'
} )
) {
2024-03-07 15:25:08 +08:00
return ''
}
return code
}
return code
}
}
2023-04-09 17:44:47 +08:00
]
2023-04-09 12:33:58 +08:00
}
2023-04-08 13:43:03 +08:00
} )