123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import { fileURLToPath, URL } from "node:url";
- import { defineConfig, loadEnv } from "vite";
- import vue from "@vitejs/plugin-vue";
- import vueJsx from "@vitejs/plugin-vue-jsx";
- import Components from "unplugin-vue-components/vite";
- import {ElementPlusResolver, VantResolver} from "unplugin-vue-components/resolvers";
- import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
- import AutoImport from 'unplugin-auto-import/vite';
- import path from "path";
- import mockDevServerPlugin from "vite-plugin-mock-dev-server";
- import vueSetupExtend from "vite-plugin-vue-setup-extend";
- import viteCompression from "vite-plugin-compression";
- import { createHtmlPlugin } from "vite-plugin-html";
- import { enableCDN } from "./build/cdn";
- import { ViteImageOptimizer } from 'vite-plugin-image-optimizer';
- import legacy from '@vitejs/plugin-legacy';
- // 当前工作目录路径
- const root: string = process.cwd();
- // https://vitejs.dev/config/
- export default defineConfig(({ mode }) => {
- // 环境变量
- const env = loadEnv(mode, root, "");
- return {
- base: env.VITE_PUBLIC_PATH || "/",
- plugins: [
- vue(),
- vueJsx(),
- mockDevServerPlugin(),
- // vant 组件自动按需引入
- Components({
- dts: "src/typings/components.d.ts",
- resolvers: [VantResolver()]
- }),
- // svg icon
- createSvgIconsPlugin({
- // 指定图标文件夹
- iconDirs: [path.resolve(root, "src/icons/svg")],
- // 指定 symbolId 格式
- symbolId: "icon-[dir]-[name]"
- }),
- // 允许 setup 语法糖上添加组件名属性
- vueSetupExtend(),
- // 生产环境 gzip 压缩资源
- viteCompression(),
- // 注入模板数据
- createHtmlPlugin({
- inject: {
- data: {
- ENABLE_ERUDA: env.VITE_ENABLE_ERUDA || "false"
- }
- }
- }),
- // 自动引入组件
- AutoImport({
- // 自动导入 Vue 相关函数
- imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
- dts: './auto-imports.d.ts',
- eslintrc: {
- enabled: false,
- filepath: './.eslintrc-auto-import.json',
- globalsPropValue: true
- },
- resolvers: [
- ElementPlusResolver()
- ],
- vueTemplate: true, // 是否在 vue 模板中自动导入
- }),
- legacy({
- targets: ['defaults', 'Chrome >= 51'],
- additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
- modernPolyfills: ['es.string.replace-all'],
- renderLegacyChunks: true,
- polyfills: [
- 'es.array.flat-map',
- 'es.array.filter',
- 'es.promise',
- 'es.promise.finally',
- 'es.object.assign',
- 'es.array.for-each',
- 'es.object.define-properties',
- 'es.object.define-property',
- 'es.object.get-own-property-descriptor',
- 'es.object.get-own-property-descriptors',
- 'es.object.keys',
- 'es.object.to-string',
- 'web.dom-collections.for-each',
- 'esnext.global-this',
- 'esnext.string.match-all',
- 'es.array.iterator',
- 'es.string.includes',
- 'es.string.starts-with',
- 'es.object.values'
- ]
- }),
- // 生产环境默认不启用 CDN 加速
- enableCDN(env.VITE_CDN_DEPS),
- ViteImageOptimizer()
- ],
- resolve: {
- alias: {
- "@": fileURLToPath(new URL("./src", import.meta.url))
- }
- },
- server: {
- host: true,
- port: Number(env.VITE_APP_PORT),
- open: true
- // 仅在 proxy 中配置的代理前缀, mock-dev-server 才会拦截并 mock
- // doc: https://github.com/pengzhanbo/vite-plugin-mock-dev-server
- // proxy: {
- // "^/dev-api": {
- // target: ""
- // }
- // }
- },
- css: {
- preprocessorOptions: {
- scss: { api: 'modern-compiler' },
- }
- },
- build: {
- rollupOptions: {
- output: {
- chunkFileNames: "static/js/[name]-[hash].js",
- entryFileNames: "static/js/[name]-[hash].js",
- assetFileNames: "static/[ext]/[name]-[hash].[ext]"
- }
- }
- }
- };
- });
|