155 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			155 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import { defineConfig, loadEnv } from 'vite'
 | 
						|
import vue from '@vitejs/plugin-vue'
 | 
						|
import AutoImport from 'unplugin-auto-import/vite'
 | 
						|
// https://vitejs.dev/config/
 | 
						|
import { resolve } from 'path'
 | 
						|
import vueJsx from '@vitejs/plugin-vue-jsx'
 | 
						|
import Components from 'unplugin-vue-components/vite'
 | 
						|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
 | 
						|
import { createHtmlPlugin } from 'vite-plugin-html'
 | 
						|
 | 
						|
 | 
						|
// @ts-ignore
 | 
						|
import pjson from './package.json'
 | 
						|
 | 
						|
const now = new Date()
 | 
						|
 | 
						|
export default ({ mode }: any) => {
 | 
						|
  const envInfo = loadEnv(mode, process.cwd() + '/env')
 | 
						|
  const isProduction = mode == 'production' //正式生产环境
 | 
						|
  const isSIT = ['sit', 'dev', 'development'].includes(mode)
 | 
						|
  const isUAT = ['uat'].includes(mode)
 | 
						|
  function getTestResource(): string {
 | 
						|
    if (isSIT) {
 | 
						|
      return `https://testSit.com`
 | 
						|
    } else if (isUAT) {
 | 
						|
      return `https://testUat.com`
 | 
						|
    } else if (mode === 'production') {
 | 
						|
      return `https://production.com`
 | 
						|
    }
 | 
						|
    return ''
 | 
						|
  }
 | 
						|
 | 
						|
  return defineConfig({
 | 
						|
    base: '/mall-view/',
 | 
						|
    plugins: [
 | 
						|
      vue(),
 | 
						|
      vueJsx(),
 | 
						|
      AutoImport({
 | 
						|
        imports: ['vue', 'vue-router', 'pinia'],
 | 
						|
      }),
 | 
						|
      Components({
 | 
						|
        resolvers: [ElementPlusResolver()],
 | 
						|
      }),
 | 
						|
      createHtmlPlugin({
 | 
						|
        inject: {
 | 
						|
          data: {
 | 
						|
            injectTestResource: getTestResource()
 | 
						|
            // 动态插入js脚本
 | 
						|
          }
 | 
						|
        }
 | 
						|
      }),
 | 
						|
      // 打包文件
 | 
						|
    ],
 | 
						|
    resolve: {
 | 
						|
      // 路径别名配置
 | 
						|
      alias: {
 | 
						|
        '@': resolve(__dirname, './src'),
 | 
						|
        assets: resolve(__dirname, './src/assets'),
 | 
						|
        components: resolve(__dirname, './src/components'),
 | 
						|
        store: resolve(__dirname, './src/store'),
 | 
						|
        views: resolve(__dirname, './src/views'),
 | 
						|
        router: resolve(__dirname, './src/router'),
 | 
						|
        utils: resolve(__dirname, './src/utils'),
 | 
						|
        http: resolve(__dirname, './src/http'),
 | 
						|
      }
 | 
						|
    },
 | 
						|
    css: {
 | 
						|
      preprocessorOptions: {
 | 
						|
        scss: {
 | 
						|
          // 两种方式都可以
 | 
						|
          additionalData: "@import '@/style/scss/index.scss';",
 | 
						|
        }
 | 
						|
      }
 | 
						|
    },
 | 
						|
    server: {
 | 
						|
      host: '0.0.0.0',
 | 
						|
      port: 8102,
 | 
						|
      // port: Number(envInfo.VITE_PORT),
 | 
						|
      // open: envInfo.VITE_OPEN,
 | 
						|
      proxy: {
 | 
						|
        '/proxyApi': {
 | 
						|
          target: envInfo.VITE_proxyTarget,
 | 
						|
          secure: false,
 | 
						|
          changeOrigin: true,
 | 
						|
          rewrite: (path) => path.replace(/^\/proxyApi/, ''),
 | 
						|
          configure: (proxy, _options) => {
 | 
						|
            proxy.on('error', (err, _req, _res) => {
 | 
						|
              // console.log('proxy error', err)
 | 
						|
            })
 | 
						|
 | 
						|
            proxy.on('start', (req, res, target) => {
 | 
						|
              // console.log(
 | 
						|
              //   'Proxy Start:',
 | 
						|
              //   req.method,
 | 
						|
              //   req.url,
 | 
						|
              //   req.headers,
 | 
						|
              //   _options
 | 
						|
              // )
 | 
						|
            })
 | 
						|
            proxy.on('proxyReq', (proxyReq, req, _res) => {
 | 
						|
              // console.log(
 | 
						|
              //   'Sending Request to the Target:',
 | 
						|
              //   req.method,
 | 
						|
              //   req.url,
 | 
						|
              //   req.headers,
 | 
						|
              //   _options
 | 
						|
              // )
 | 
						|
              proxyReq.removeHeader('origin')
 | 
						|
              // 跨域解决
 | 
						|
            })
 | 
						|
            proxy.on('proxyRes', (proxyRes, req, _res) => {
 | 
						|
              // console.log(
 | 
						|
              //   'Received Response from the Target:',
 | 
						|
              //   proxyRes.statusCode,
 | 
						|
              //   proxyRes.headers,
 | 
						|
              //   req.url
 | 
						|
              // )
 | 
						|
            })
 | 
						|
          }
 | 
						|
        }
 | 
						|
      },
 | 
						|
      hmr: {
 | 
						|
        overlay: true
 | 
						|
      }
 | 
						|
    },
 | 
						|
    envDir: 'env',
 | 
						|
    build: {
 | 
						|
      outDir: 'dist/' + pjson.name + '-' + envInfo.VITE_BUILD_MODE,
 | 
						|
      emptyOutDir: true,
 | 
						|
      terserOptions: {
 | 
						|
        compress: {
 | 
						|
          drop_console: isProduction, //生产正式 去除
 | 
						|
          drop_debugger: isProduction //生产正式 去除
 | 
						|
        }
 | 
						|
      },
 | 
						|
      minify: 'terser',
 | 
						|
      /*
 | 
						|
      rollupOptions: {
 | 
						|
          output: {
 | 
						|
              manualChunks(id) {
 | 
						|
                  if (id.includes('node_modules')) {
 | 
						|
                      return id
 | 
						|
                          .toString()
 | 
						|
                          .split('node_modules/')[1]
 | 
						|
                          .split('/')[0]
 | 
						|
                          .toString()
 | 
						|
                  }
 | 
						|
              }
 | 
						|
          }
 | 
						|
      }
 | 
						|
       */
 | 
						|
    }
 | 
						|
  })
 | 
						|
}
 |