vite 区分生产环境开发环境

示例 1

vite.config.ts

import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'

export default ({ mode }) => {
  const __DEV__ = mode === 'development'

  return defineConfig({
    base: __DEV__ ? '/' : 'emoji-popover',
    root: 'example',
    server: {
      open: false,
      port: 3000
    },
    build: {
      outDir: '../docs',
      emptyOutDir: true
    }
  })
}

示例 2

vite.config.ts

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';


export default ({ mode }) => {
    process.env = {...process.env, ...loadEnv(mode, process.cwd())};

    // import.meta.env.VITE_NAME available here with: process.env.VITE_NAME
    // import.meta.env.VITE_PORT available here with: process.env.VITE_PORT

    return defineConfig({
        plugins: [vue()],

        server: {
            port: process.env.VITE_PORT,
        },
    });
}
posted @ 2021-06-23 21:15  guangzan  阅读(5786)  评论(0编辑  收藏  举报