105 lines
3.4 KiB
JavaScript
105 lines
3.4 KiB
JavaScript
|
|
import { defineConfig, loadEnv, normalizePath } from 'vite'
|
|||
|
|
import path from 'path'
|
|||
|
|
import createVitePlugins from './vite/plugins'
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 为独立项目生成开发与构建配置,所有源码均从当前目录解析。
|
|||
|
|
* 依据模式读取原有环境文件,保留代理、样式注入和编辑器分包规则。
|
|||
|
|
* @param {import('vite').ConfigEnv} options Vite 当前环境模式与执行命令。
|
|||
|
|
* @returns {import('vite').UserConfig} 当前项目的 Vite 配置,不访问同级源码目录。
|
|||
|
|
*/
|
|||
|
|
function createViteConfig({ mode, command }) {
|
|||
|
|
const env = loadEnv(mode, process.cwd())
|
|||
|
|
const { VITE_APP_ENV, VITE_API_URL, VITE_PORT } = env
|
|||
|
|
const variablePath = normalizePath(path.resolve('./src/style/variable.scss'))
|
|||
|
|
return {
|
|||
|
|
base: VITE_APP_ENV === 'production' ? '/' : '/',
|
|||
|
|
plugins: createVitePlugins(env, command === 'build'),
|
|||
|
|
optimizeDeps: {
|
|||
|
|
include: [
|
|||
|
|
'monaco-editor/esm/vs/editor/editor.all.js',
|
|||
|
|
'monaco-editor/esm/vs/editor/editor.api.js',
|
|||
|
|
'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution.js',
|
|||
|
|
'monaco-editor/esm/vs/language/json/monaco.contribution.js',
|
|||
|
|
'monaco-editor/esm/vs/language/typescript/monaco.contribution'
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
resolve: {
|
|||
|
|
// https://cn.vitejs.dev/config/#resolve-alias
|
|||
|
|
alias: {
|
|||
|
|
// 设置路径
|
|||
|
|
'~': path.resolve(__dirname, './'),
|
|||
|
|
// 设置别名
|
|||
|
|
'@': path.resolve(__dirname, './src')
|
|||
|
|
},
|
|||
|
|
dedupe: ['vue', 'vue-router', 'pinia', 'element-plus', '@element-plus/icons-vue'],
|
|||
|
|
// https://cn.vitejs.dev/config/#resolve-extensions
|
|||
|
|
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
|
|||
|
|
},
|
|||
|
|
// 打包配置
|
|||
|
|
build: {
|
|||
|
|
// https://vite.dev/config/build-options.html
|
|||
|
|
sourcemap: command === 'build' ? false : 'inline',
|
|||
|
|
outDir: 'dist',
|
|||
|
|
assetsDir: 'assets',
|
|||
|
|
chunkSizeWarningLimit: 2000,
|
|||
|
|
rollupOptions: {
|
|||
|
|
output: {
|
|||
|
|
manualChunks(id) {
|
|||
|
|
if (id.includes('/node_modules/monaco-editor/')) return 'monaco'
|
|||
|
|
},
|
|||
|
|
chunkFileNames: 'static/js/[name]-[hash].js',
|
|||
|
|
entryFileNames: 'static/js/[name]-[hash].js',
|
|||
|
|
assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
// vite 相关配置
|
|||
|
|
server: {
|
|||
|
|
port: Number(VITE_PORT || 80),
|
|||
|
|
host: true,
|
|||
|
|
open: true,
|
|||
|
|
proxy: {
|
|||
|
|
// https://cn.vitejs.dev/config/#server-proxy
|
|||
|
|
'/dev-api': {
|
|||
|
|
target: VITE_API_URL || 'http://localhost:13080',
|
|||
|
|
changeOrigin: true,
|
|||
|
|
rewrite: (p) => p.replace(/^\/dev-api/, '')
|
|||
|
|
},
|
|||
|
|
// springdoc proxy
|
|||
|
|
'^/v3/api-docs/(.*)': {
|
|||
|
|
target: VITE_API_URL || 'http://localhost:13080',
|
|||
|
|
changeOrigin: true,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
css: {
|
|||
|
|
postcss: {
|
|||
|
|
plugins: [
|
|||
|
|
{
|
|||
|
|
postcssPlugin: 'internal:charset-removal',
|
|||
|
|
AtRule: {
|
|||
|
|
charset: (atRule) => {
|
|||
|
|
if (atRule.name === 'charset') {
|
|||
|
|
atRule.remove()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
preprocessorOptions: {
|
|||
|
|
scss: {
|
|||
|
|
additionalData: `@use "${variablePath}" as *;`
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
define: {
|
|||
|
|
// 将环境变量注入到全局常量中
|
|||
|
|
'__PROJECT_NAME__': JSON.stringify(env.PROJECT_NAME || 'changan'),
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default defineConfig(createViteConfig)
|