Newer
Older
KaiFengPC / vite.config.js
@zhangdeliang zhangdeliang 28 days ago 4 KB update
import { defineConfig, loadEnv } from 'vite';
import path from 'path';
import createVitePlugins from './vite/plugins';
import postCssPxToRem from 'postcss-pxtorem';
import autoprefixer from 'autoprefixer';

const Timestamp = new Date().getTime(); //随机时间戳

// const targetUrl = 'http://192.168.20.52:7300'; //杨辉
const targetUrl = 'https://server2.wh-nf.cn:8082/prod-api'; //线上

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: '/',
    // 是否开启eslint保存检测
    lintOnSave: false,
    plugins: createVitePlugins(env, command === 'build'),
    resolve: {
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src'),
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    },
    // vite 相关配置
    server: {
      port: 4319,
      host: true,
      hot: true, //自动保存
      cors: true,
      hmr: true, //vue3 vite配置热更新不用手动刷新   http://server1.wh-nf.cn:8023
      proxy: {
        '/business': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/business/, '/business/'),
        },
        '/publicService': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/publicService/, '/publicService'),
        },
        '/system': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/system/, '/system'),
        },
        '/auth': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/auth/, '/auth'),
        },
        '/code': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/code/, '/code'),
        },
        '/scada': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: p => p.replace(/^\/scada/, '/scada'),
        },
        '/sponge': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/sponge/, '/sponge'),
        },
        '/amap': {
          target: 'https://restapi.amap.com', //geocode服务
          changeOrigin: true,
          rewrite: p => p.replace(/^\/amap/, ''),
        },
        '/geoserver': {
          target: 'http://server1.wh-nf.cn:8023/geoserver', //geoserver服务
          changeOrigin: true,
          rewrite: path => path.replace(/^\/geoserver/, ''),
        },
        '/supermap1': {
          target: 'https://supermap1.wh-nf.cn:8090', //mapBox矢量地图
          changeOrigin: true,
          rewrite: path => path.replace(/^\/supermap1/, ''),
        },
        '/previewUrl': {
          target: 'https://server1.wh-nf.cn:8115', //文件预览地址
          changeOrigin: true,
          rewrite: path => path.replace(/^\/previewUrl/, ''),
        },
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: atRule => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              },
            },
          },
          postCssPxToRem({
            // 自适应,px>rem转换
            rootValue: 192, // 根据设计图尺寸写,设计图是 1920,就写 192
            propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
            selectorBlackList: ['-nopx'], // 过滤掉-nopx结尾的class,不进行rem转换
          }),
          autoprefixer({
            // 自动添加前缀
            overrideBrowserslist: [
              'Android 4.1',
              'iOS 7.1',
              'Chrome > 31',
              'ff > 31',
              'ie >= 8',
              //'last 2 versions', // 所有主流浏览器最近2个版本
            ],
            grid: true,
          }),
        ],
      },
    },
    build: {
      sourcemap: false,
      outDir: 'KaiFengPC-dist',
      rollupOptions: {
        output: {
          chunkFileNames: `static/js/[name].[hash]${Timestamp}.js`,
          entryFileNames: `static/js/[name].[hash]${Timestamp}.js`,
          assetFileNames: `static/[ext]/[name].[hash]${Timestamp}.[ext]`,
        },
        brotliSize: true, // 不统计
        target: 'esnext',
        minify: 'esbuild', // 混淆器,terser构建后文件体积更小
      },
    },
  };
});