| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { defineConfig, loadEnv } from 'vite'
- import vue from '@vitejs/plugin-vue'
- import vueJsx from '@vitejs/plugin-vue-jsx'
- import path from 'path'
- import viteCompression from 'vite-plugin-compression'
- import Components from 'unplugin-vue-components/vite'
- import AutoImport from 'unplugin-auto-import/vite'
- import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
- import { fileURLToPath } from 'url'
- // import vueDevTools from 'vite-plugin-vue-devtools'
- // import { visualizer } from 'rollup-plugin-visualizer'
- import tailwindcss from '@tailwindcss/vite'
- import legacy from '@vitejs/plugin-legacy'
- export default ({ mode }: { mode: string }) => {
- const root = process.cwd()
- const env = loadEnv(mode, root)
- const { VITE_VERSION, VITE_PORT } = env
- return defineConfig({
- define: {
- __APP_VERSION__: JSON.stringify(VITE_VERSION)
- },
- base: './',
- server: {
- port: Number(VITE_PORT),
- proxy: {
- '/api': {
- target: 'https://haijunit.icu/course/api',
- changeOrigin: true,
- secure: false,
- rewrite: (path) => path.replace(/^\/api/, '')
- },
- '/course/api': {
- target: 'https://haijunit.icu',
- changeOrigin: true,
- secure: false
- }
- },
- host: true
- },
- // 路径别名
- resolve: {
- alias: {
- '@': fileURLToPath(new URL('./src', import.meta.url)),
- '@views': resolvePath('src/views'),
- '@imgs': resolvePath('src/assets/img'),
- '@icons': resolvePath('src/assets/icons'),
- '@utils': resolvePath('src/utils'),
- '@stores': resolvePath('src/store'),
- '@plugins': resolvePath('src/plugins'),
- '@styles': resolvePath('src/assets/styles')
- }
- },
- build: {
- target: 'es2015',
- outDir: 'dist/course',
- chunkSizeWarningLimit: 2000,
- minify: 'terser',
- assetsDir: 'assets',
- rollupOptions: {
- output: {
- chunkFileNames: 'assets/[name]-[hash].js',
- entryFileNames: 'assets/[name]-[hash].js'
- }
- },
- terserOptions: {
- compress: {
- // 生产环境去除 console
- drop_console: true,
- // 生产环境去除 debugger
- drop_debugger: true
- }
- },
- dynamicImportVarsOptions: {
- warnOnError: true,
- exclude: [],
- include: ['src/views/**/*.vue']
- }
- },
- plugins: [
- vue(),
- vueJsx(),
- tailwindcss(),
- // 自动导入 components 目录下的组件
- Components({
- deep: true,
- extensions: ['vue'],
- dirs: ['src/components/core', 'src/components/business', 'src/components/icon'],
- resolvers: [ElementPlusResolver({ importStyle: false })],
- dts: 'src/types/components.d.ts'
- }),
- // 自动导入组件 Api
- AutoImport({
- imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
- resolvers: [ElementPlusResolver()],
- dts: 'src/types/auto-imports.d.ts',
- // ESLint 配置
- eslintrc: {
- // 首次运行时设置为 true 生成配置文件,之后改为 false
- enabled: true,
- // ESLint 配置文件路径
- filepath: './.auto-import.json',
- // 允许全局使用自动导入的 API
- globalsPropValue: true
- }
- }),
- // 压缩
- viteCompression({
- verbose: false, // 是否在控制台输出压缩结果
- disable: false, // 是否禁用
- algorithm: 'gzip', // 压缩算法
- ext: '.gz', // 压缩后的文件名后缀
- threshold: 10240, // 只有大小大于该值的资源会被处理 10240B = 10KB
- deleteOriginFile: false // 压缩后是否删除原文件
- }),
- // vueDevTools()
- // 打包分析
- // visualizer({
- // open: true,
- // gzipSize: true,
- // brotliSize: true,
- // filename: 'dist/stats.html' // 分析图生成的文件名及路径
- // }),
- legacy({ targets: ['defaults', 'not IE 11'] })
- ],
- css: {
- preprocessorOptions: {
- // sass variable and mixin
- scss: {
- api: 'modern-compiler',
- additionalData: `
- @use "@styles/variables.scss" as *;
- @use "@styles/mixin.scss" as *;
- `
- }
- },
- postcss: {
- plugins: [
- {
- postcssPlugin: 'internal:charset-removal',
- AtRule: {
- charset: (atRule) => {
- if (atRule.name === 'charset') {
- atRule.remove()
- }
- }
- }
- }
- ]
- }
- }
- })
- }
- function resolvePath(paths: string) {
- return path.resolve(__dirname, paths)
- }
|