加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vite.config.ts 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
Joeshu 提交于 2024-01-04 14:26 . refactor: defineConfig
import { fileURLToPath, URL } from 'node:url';
import type { UserConfig, ConfigEnv } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import pkg from './package.json';
import dayjs from 'dayjs';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import AutoComponents from 'unplugin-vue-components/vite';
import { createHtmlPlugin } from 'vite-plugin-html';
/**
* 当前执行node命令时文件夹的地址(工作目录)
*/
const root: string = process.cwd();
// 打包后静态资源的存放路径
const assetsDir = 'assets';
// __APP_INFO__
const { dependencies, devDependencies, name, version } = pkg;
const appVersion = dayjs().format('YYYYMMDDHHmm');
const lastBuildTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
const __APP_INFO__ = {
pkg: { dependencies, devDependencies, name, version },
version: appVersion,
lastBuildTime,
};
/**
* Configuring Vite
*
* @see https://vitejs.dev/config/
*/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
const env = loadEnv(mode, root);
return {
base: './',
plugins: [
vue(),
vueJsx(),
AutoComponents({
dirs: ['./src/components'],
extensions: ['vue', 'jsx', 'tsx'],
dts: './src/auto-components.d.ts',
}),
createHtmlPlugin({
inject: {
data: {
title: env.VITE_APP_TITLE,
},
},
minify: true,
}),
env.VITE_LEGACY === 'true' ? legacy() : null,
],
server: {
host: true,
port: Number(env.VITE_PORT),
proxy: {
'/dev-api': {
target: `https://${env.VITE_APP_API_HOST}/${env.VITE_APP_SUB_DOMAIN}`, // https://api.it120.cc/xiaochengxu
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^\/dev-api/, ''),
},
},
},
build: {
assetsDir: assetsDir,
sourcemap: false,
chunkSizeWarningLimit: 1500,
rollupOptions: {
output: {
entryFileNames: `${assetsDir}/[name].${appVersion}.js`,
chunkFileNames: `${assetsDir}/[name].${appVersion}.js`,
assetFileNames: `${assetsDir}/[ext]/[name].${appVersion}.[ext]`,
},
},
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
define: {
__APP_INFO__: JSON.stringify(__APP_INFO__),
},
css: {
modules: {
localsConvention: 'camelCaseOnly',
},
preprocessorOptions: {
less: {
modifyVars: {
hack: `true; @import './src/styles/variable.less';`,
},
javascriptEnabled: true,
},
},
},
};
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化