08335 / hivui-platform-template
hivui平台项目模板
Newer
Older
hivui-platform-template / vite.config.js

// vite.config.js  利害浏览器自带的 es 动态导入,使项目冷启动
const { resolve } = require('path');
import { defineConfig, loadEnv } from 'vite'
import { createVuePlugin } from "vite-plugin-vue2";
import vueJsx from '@vitejs/plugin-vue-jsx'
import pageData from "vite-plugin-pagedata"
import vitefunc from './vite-plugin/vite-plugin-func'
import { injectHtml } from 'vite-plugin-html';

//影响到eap.umd.min.js 里的require ELEMENT 全局
// import requireTransform from 'vite-plugin-require-transform';


const pkg = require("./package.json");

// let dependencies = [];
// for(let key in pkg.dependencies){
//     if(['font-awesome'].indexOf(key)==-1)
//     dependencies.push(key)
// }
// console.log('------dependencies------',dependencies)


// https://vitejs.dev/config/
export default ({ mode }) => {
    process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
    let aliases = {}
    let build = {
        sourcemap: false,
        //编译
        //指定 多个 .html 文件作为入口点
        rollupOptions: {
            // 请确保外部化那些你的库中不需要的依赖
            // external: ['vue'],
            input: {
                main: resolve(__dirname, 'index.html'),
                nested: resolve(__dirname, 'nested/index.html')
            },
            output: {
                format: 'umd',
            }
        }
    }
    if (mode == 'development') {
        aliases = {
            '@project': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}`),
            '@main': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}/hivuiMain`),
            '@birt': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}/hivuiBirt`),
            'pinyin': 'js-pinyin',
            //开启下面注释,scp5.0本地调式指定
            //'@':resolve(__dirname, './hi-ui/src'),
        }
        // console.log('aliases@main:', aliases['@main']);
    } else {

    }
    const postDataName = "scpRequestData";
    return defineConfig({
        //预览时,预编译这些包,以防vite 启动后,浏览器通过自动模块加载导致刷新页面
        optimizeDeps:{
            include: [
                // 'vue','vue-router','vuex','@vue/babel-helper-vue-jsx-merge-props','vue-i18n',
                // 'pinyin','lodash','js-cookie','axios','js-md5','classnames','js-pinyin','enquire.js','screenfull','sortablejs',
                // 'lodash/findIndex','lodash/cloneDeep','lodash/isPlainObject','lodash/defaultsDeep','lodash/bind',
                // 'umy-ui','xlsx', 'echarts', 'echarts-liquidfill/src/liquidFill.js',
                // 'v-clipboard','vue-jstree', 'vue-gemini-scrollbar','vue-draggable-resizable',
                // 'element-ui','element-ui/lib/locale', 'element-ui/lib/locale/lang/en', 'element-ui/lib/locale/lang/zh-CN', 'element-ui/lib/locale/lang/es',
                // 'hi-ui',
                // 'hi-taskcenter',
                // "zhc-flowchart",
                // "zhc-birt-core",
                // 'zhc-birt-cmps',
                'axios',
                'babel-polyfill',
                'canvas',
                'classnames',
                'deep-lu-trans',
                'echarts',
                'echarts-liquidfill',
                'element-ui',
                'enquire.js',
                'hi-taskcenter',
                'hi-ui',
                'html2canvas',
                'jquery',
                'js-cookie',
                'js-md5',
                'js-pinyin',
                'jspdf',
                'lodash','lodash/findIndex','lodash/cloneDeep','lodash/isPlainObject','lodash/defaultsDeep','lodash/bind',
                'qrcodejs2',
                'screenfull',
                'sortablejs',
                'v-clipboard',
                'vue',
                'vue-draggable-resizable',
                'vue-gemini-scrollbar',
                'vue-i18n',
                'vue-jstree',
                'vue-qr',
                'vue-router',
                'vuex',
                'xlsx',
                'zhc-birt-cmps',
                'zhc-birt-core',
                'zhc-flowchart'
            ]
        },
        build: build,
        define: {
            'process.env': {
                //配合 webpack 环境,在vite 下定义一个空的
            }
        },
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true,
                }
            },
        },
        resolve: {
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.dtvevt', '.dtv','.dvue','.es6'],
            alias: aliases,
        },
        plugins: [
            // PkgConfig(),
            // OptimizationPersist(),
            vitefunc({
                varName: postDataName,
                global: {
                    env: 'dev'
                },
                htmlWebpackPlugin: {
                    vite: true,
                    project: pkg.name,
                    mode: mode
                }
            }),
            injectHtml({
                // 向预览时注入变量,这个变量跟打包时 html-webpack-plugin 注入的一致,用户判断是否是编译状态
                injectData: {
                    htmlWebpackPlugin: {
                        vite: true,
                        project: pkg.name,
                        mode: mode
                    }
                }
            }),
            pageData({
                varName: postDataName,
                global: {
                    env: 'dev'
                },
            }),
            createVuePlugin({
                include:/\.(dvue|vue)$/
            }),
            vueJsx({
                // options are passed on to @vue/babel-plugin-jsx
            }),
            //影响到eap.umd.min.js 里的require ELEMENT 全局
            // requireTransform({
            //     fileRegex:/.ts$|.tsx$|.vue$|.js$|.jsx$/
            // })
        ],
      
        // devServer: {
        //     port: 227,
        //     proxy: 'http://192.168.1.183:5001/'
        // },
        server: {
            hmr:{
                overlay:false,//不遮罩输出错误信息,但输出控制台
            }
            // watch: {
            //     ignored: ['!**/node_modules/vue/**']
            // }
        },
        // // 被监听的包必须被排除在优化之外,
        // // 以便它能出现在依赖关系图中并触发热更新。
        // optimizeDeps: {
        //     exclude: ['vue']
        // }
    })

};