import { defineConfig, loadEnv } from "vite"; import vue from "@vitejs/plugin-vue"; import path from "path"; import Components from "unplugin-vue-components/vite"; import { VantResolver } from "unplugin-vue-components/resolvers"; import VueSetupExtend from 'vite-plugin-vue-setup-extend' import {createSvgIconsPlugin} from 'vite-plugin-svg-icons' import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill' import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill' import rollupNodePolyFill from 'rollup-plugin-node-polyfills' // https://vitejs.dev/config/ export default ({ mode }) =>{ // eta webComponent组件打包配置 const etaWebCompBuildConfig={ outDir:'lib_dist', copyPublicDir:false, // minify:false, lib: { entry: path.resolve(__dirname,'./src/CustomElement/index.js'), formats: ["es"], name: "eta", fileName:'eta_comp' }, rollupOptions:{ external: ['vue'], output: { // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量 globals: { vue: 'Vue', }, }, } } return defineConfig({ base: loadEnv(mode, process.cwd()).VITE_APP_BASE_URL, // 若服务器不是将该项目放在根目录的则 需要此设置 和服务器上同名 plugins: [ vue({ template:{ compilerOptions:{ isCustomElement:tag=>tag.startsWith('eta-') } } }), Components({ resolvers: [VantResolver()], }), VueSetupExtend(), createSvgIconsPlugin({ // 指定需要缓存的图标文件夹 iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')], // 指定symbolId格式 symbolId: '[name]', /** * 自定义插入位置 * @default: body-last */ // inject?: 'body-last' | 'body-first' /** * custom dom id * @default: __svg__icons__dom__ */ // customDomId: '__svg__icons__dom__' }) ], css: { // css预处理器 preprocessorOptions: { scss: { // 定义全局的scss变量 // 给导入的路径最后加上 ; additionalData: `@import '@/assets/styles/var.scss';`, }, }, }, resolve: { alias: { "@": path.resolve(__dirname, "./src"), events: 'rollup-plugin-node-polyfills/polyfills/events', stream: 'rollup-plugin-node-polyfills/polyfills/stream', buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6', process: 'rollup-plugin-node-polyfills/polyfills/process-es6' } }, optimizeDeps: { esbuildOptions: { // Enable esbuild polyfill plugins plugins: [ NodeModulesPolyfillPlugin() ] } }, build: mode=='lib'?etaWebCompBuildConfig:{ outDir: loadEnv(mode, process.cwd()).VITE_APP_OUTDIR, rollupOptions: { plugins: [ // Enable rollup polyfills plugin used during production bundling rollupNodePolyFill() ] } }, server:{ host:true } }); }