加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vite.base.config.ts 4.49 KB
一键复制 编辑 原始数据 按行查看 历史
lzw 提交于 2024-05-28 19:50 . feat: 初始化
import { resolve } from 'path';
import { loadEnv } from 'vite';
import type { UserConfig, ConfigEnv } from 'vite';
import Vue from '@vitejs/plugin-vue';
import VueJsx from '@vitejs/plugin-vue-jsx';
import { ViteEjsPlugin } from 'vite-plugin-ejs'; // 传统js模板兼容
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
// import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import';
import { visualizer } from 'rollup-plugin-visualizer';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver, VantResolver } from 'unplugin-vue-components/resolvers';
import CompressPlugin from 'vite-plugin-compression';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
// import legacy from '@vitejs/plugin-legacy';
import SvgLoader from 'vite-svg-loader';
// @ts-ignore
import { CreateHtmlPlugin } from 'vite-plugin-html';
// import AutoImport from 'unplugin-auto-import/vite';
// 项目目录
const appRootPath = process.cwd();
function pathResolve(dir: string) {
return resolve(appRootPath, '.', dir);
}
function rootPathResolve(dir: string) {
return resolve(__dirname, dir);
}
export default ({ mode }: ConfigEnv): UserConfig => {
// const isBuild = command === 'build';
console.log('当前环境:', mode);
const appEnv = loadEnv(mode, appRootPath);
const rootEnv = loadEnv(mode, rootPathResolve('./env'));
const env = {
...rootEnv,
...appEnv,
};
function loadEnvConfig() {
if (!appEnv) {
console.warn(`File ${appEnv} not found. Skipping...`);
return {};
}
return Object.entries(env).reduce((acc, [key, value]) => {
// @ts-ignore
acc[`import.meta.env.${key}`] = JSON.stringify(value);
return acc;
}, {});
}
const importEnv = loadEnvConfig();
return {
base: env.VITE_PUBLIC_PATH,
envDir: pathResolve('./'),
define: {
...importEnv,
},
plugins: [
Vue({
script: {
defineModel: true,
},
}),
VueJsx(),
createSvgIconsPlugin({
iconDirs: [pathResolve('src/assets/svgs')],
symbolId: 'icon-[dir]-[name]',
svgoOptions: true,
}),
Components({
resolvers: [
ElementPlusResolver(),
VantResolver(),
],
}),
// 生产环境 gzip 压缩资源
CompressPlugin({ // 1
ext: '.gz',
deleteOriginFile: false,
}),
// 允许 setup 语法糖上添加组件名属性
VueSetupExtend(),
// legacy({
// targets: ['defaults', 'not IE 11', 'chrome 52'],
// }),
ViteEjsPlugin({
...env,
}),
// AutoImport({
// imports: ['vue', 'vue-router', 'pinia'], // 自动导入 vue 和 router
// // 声明文件生成位置和文件名称
// dts: './auto-imports.d.ts',
// }),
SvgLoader(),
CreateHtmlPlugin({
inject: {
data: {
...env,
},
},
}),
],
css: {
// preprocessorOptions: {
// less: {
// additionalData: '@import "./src/styles/variables.module.less";',
// javascriptEnabled: true,
// },
// },
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
alias: [
{
find: /@\//,
replacement: `${pathResolve('src')}/`,
},
],
},
esbuild: {
pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined,
},
build: {
target: 'es2015',
outDir: env.VITE_OUTPUT_DIR || '/',
sourcemap: env.VITE_SOURCEMAP === 'true',
// brotliSize: false,
rollupOptions: {
plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
// 拆包
output: {
// manualChunks: {
// 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
// 'element-plus': ['element-plus'],
// 'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue'],
// echarts: ['echarts', 'echarts-wordcloud'],
// },
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
},
},
cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false'),
},
server: {
port: 3000,
hmr: {
overlay: false,
},
host: '0.0.0.0',
},
};
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化