vite.config.js 2.9 KB
Newer Older
P
piexlmax 已提交
1 2
import legacyPlugin from '@vitejs/plugin-legacy';
// import usePluginImport from 'vite-plugin-importer';
B
bypanghu 已提交
3 4
import { viteLogo } from './src/core/config'
import Banner from 'vite-plugin-banner'
P
piexlmax 已提交
5
import * as path from 'path';
6 7
import * as dotenv from 'dotenv';
import * as fs from 'fs';
P
piexlmax 已提交
8 9 10
import vuePlugin from '@vitejs/plugin-vue';
// @see https://cn.vitejs.dev/config/
export default ({
B
bypanghu 已提交
11 12
    command,
    mode
P
piexlmax 已提交
13
}) => {
B
bypanghu 已提交
14 15 16 17 18 19 20 21 22 23
    let NODE_ENV = process.env.NODE_ENV || 'development'
    let envFiles = [
        `.env.${NODE_ENV}`
    ]
    for (const file of envFiles) {
        const envConfig = dotenv.parse(fs.readFileSync(file))
        for (const k in envConfig) {
            process.env[k] = envConfig[k]
        }
    }
24

B
bypanghu 已提交
25
    viteLogo(process.env)
P
piexlmax 已提交
26

B
bypanghu 已提交
27
    let timestamp = Date.parse(new Date())
P
piexlmax 已提交
28

B
bypanghu 已提交
29 30 31 32 33 34 35
    let rollupOptions = {
        output: {
            entryFileNames: `gva/gin-vue-admin-[name].${timestamp}.js`,
            chunkFileNames: `js/gin-vue-admin-[name].${timestamp}.js`,
            assetFileNames: `assets/gin-vue-admin-[name].${timestamp}.[ext]`
        }
    };
P
piexlmax 已提交
36

B
bypanghu 已提交
37
    let optimizeDeps = {};
P
piexlmax 已提交
38

B
bypanghu 已提交
39 40 41 42
    let alias = {
        '@': path.resolve(__dirname, './src'),
        'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
    }
P
piexlmax 已提交
43

B
bypanghu 已提交
44
    let esbuild = {}
P
piexlmax 已提交
45

B
bypanghu 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    return {
        base: './', // index.html文件所在位置
        root: './', // js导入的资源路径,src
        resolve: {
            alias,
        },
        define: {
            'process.env': {}
        },
        server: {
            open: true,
            port: process.env.VITE_CLI_PORT,
            proxy: {
                // 把key的路径代理到target位置
                // detail: https://cli.vuejs.org/config/#devserver-proxy
                [process.env.VITE_BASE_API]: { // 需要代理的路径   例如 '/api'
                    target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
                    changeOrigin: true,
                    rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
                }
            },
        },
        build: {
            target: 'es2015',
            minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
            manifest: false, // 是否产出maifest.json
            sourcemap: false, // 是否产出soucemap.json
            outDir: 'dist', // 产出目录
            rollupOptions,
        },
        esbuild,
        optimizeDeps,
        plugins: [
            legacyPlugin({
                targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
            }), vuePlugin(), [Banner(`\n Build based on gin-vue-admin \n Time : ${timestamp}`)]
        ],
        css: {
            preprocessorOptions: {
                less: {
                    // 支持内联 JavaScript
                    javascriptEnabled: true,
                }
            }
        },
    }
P
piexlmax 已提交
92
}