vite.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { defineConfig, loadEnv } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. import path from "path";
  4. import Components from "unplugin-vue-components/vite";
  5. import { VantResolver } from "unplugin-vue-components/resolvers";
  6. import VueSetupExtend from 'vite-plugin-vue-setup-extend'
  7. import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
  8. import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
  9. import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
  10. import rollupNodePolyFill from 'rollup-plugin-node-polyfills'
  11. // https://vitejs.dev/config/
  12. export default ({ mode }) =>{
  13. // eta webComponent组件打包配置
  14. const etaWebCompBuildConfig={
  15. outDir:'lib_dist',
  16. copyPublicDir:false,
  17. // minify:false,
  18. lib: {
  19. entry: path.resolve(__dirname,'./src/CustomElement/index.js'),
  20. formats: ["es"],
  21. name: "eta",
  22. fileName:'eta_comp'
  23. },
  24. rollupOptions:{
  25. external: ['vue'],
  26. output: {
  27. // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
  28. globals: {
  29. vue: 'Vue',
  30. },
  31. },
  32. }
  33. }
  34. return defineConfig({
  35. base: loadEnv(mode, process.cwd()).VITE_APP_BASE_URL, // 若服务器不是将该项目放在根目录的则 需要此设置 和服务器上同名
  36. plugins: [
  37. vue({
  38. template:{
  39. compilerOptions:{
  40. isCustomElement:tag=>tag.startsWith('eta-')
  41. }
  42. }
  43. }),
  44. Components({
  45. resolvers: [VantResolver()],
  46. }),
  47. VueSetupExtend(),
  48. createSvgIconsPlugin({
  49. // 指定需要缓存的图标文件夹
  50. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  51. // 指定symbolId格式
  52. symbolId: '[name]',
  53. /**
  54. * 自定义插入位置
  55. * @default: body-last
  56. */
  57. // inject?: 'body-last' | 'body-first'
  58. /**
  59. * custom dom id
  60. * @default: __svg__icons__dom__
  61. */
  62. // customDomId: '__svg__icons__dom__'
  63. })
  64. ],
  65. css: {
  66. // css预处理器
  67. preprocessorOptions: {
  68. scss: {
  69. // 定义全局的scss变量
  70. // 给导入的路径最后加上 ;
  71. additionalData: `@import '@/assets/styles/var.scss';`,
  72. },
  73. },
  74. },
  75. resolve: {
  76. alias: {
  77. "@": path.resolve(__dirname, "./src"),
  78. events: 'rollup-plugin-node-polyfills/polyfills/events',
  79. stream: 'rollup-plugin-node-polyfills/polyfills/stream',
  80. buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6',
  81. process: 'rollup-plugin-node-polyfills/polyfills/process-es6'
  82. }
  83. },
  84. optimizeDeps: {
  85. esbuildOptions: {
  86. // Enable esbuild polyfill plugins
  87. plugins: [
  88. NodeModulesPolyfillPlugin()
  89. ]
  90. }
  91. },
  92. build: ['lib','libgn'].includes(mode)?etaWebCompBuildConfig:{
  93. outDir: loadEnv(mode, process.cwd()).VITE_APP_OUTDIR,
  94. rollupOptions: {
  95. plugins: [
  96. // Enable rollup polyfills plugin used during production bundling
  97. rollupNodePolyFill()
  98. ]
  99. }
  100. },
  101. server:{
  102. host:true
  103. }
  104. });
  105. }