vite.config.js 3.84 KB
import { defineConfig, loadEnv } from 'vite'
import path, { resolve } from 'path'
import createVitePlugins from './vite/plugins'
import inject from '@rollup/plugin-inject'
import commonjs from '@rollup/plugin-commonjs'
// import legacy from '@vitejs/plugin-legacy'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    // 部署生产环境和开发环境下的URL。
    // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
    base: env.VITE_APP_CONTEXT_PATH,
    plugins: [
      createVitePlugins(env, command === 'build'),
      commonjs(),
      inject({
        include: ['node_modules/**'],
        'window.katex': 'katex',
        'window.Quill': ['@vueup/vue-quill', 'Quill']
      })
      // legacy({
      //   targets: ['defaults', 'not IE 11'], // 需要兼容的目标列表,可以设置多个
      //   additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
      //   renderLegacyChunks: true,
      //   polyfills: [
      //     'es.symbol',
      //     'es.array.filter',
      //     'es.promise',
      //     'es.promise.finally',
      //     'es/map',
      //     'es/set',
      //     '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'
      //   ]
      // })
    ],
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src'),
        '/@': path.resolve(__dirname, './src')
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    // vite 相关配置
    server: {
      port: 5175,
      host: true,
      open: true,
      proxy: {
        // https://cn.vitejs.dev/config/#server-proxy
        '/dev-api/ztx-train': {
          // target: 'http://123.60.96.243:1896/stage-api',
          target: 'http://192.168.1.25:8686',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api\/ztx-train/, '')
        },
        '/dev-api/ztx-match': {
          target: 'http://123.60.96.243:1892/stage-api/',
          // target: 'http://192.168.1.132:8081',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api\/ztx-match/, '')
        },
        '/dev-api/ztx-webSite': {
          // target: 'http://123.60.96.243:1897/stage-api/',
          target: 'http://192.168.1.132:8083/',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api\/ztx-webSite/, '')
        },
        '/dev-api': {
          target: 'http://192.168.1.132:8083',
          // target: 'http://localhost:8787',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, '')
        }
      }
    },
    // fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove()
                }
              }
            }
          }
        ]
      }
    },
    build: {
      // target: 'es2015',
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'index.html')
        }
      }
    }
  }
})