vite.config.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { defineConfig } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import VitePluginStyleInject from 'vite-plugin-style-inject';
  4. // 获取环境变量中的组件名称
  5. const componentName = process.env.name;
  6. console.log('当前组件名称:', componentName)
  7. export default defineConfig({
  8. plugins: [
  9. vue(), // 添加 Vue 插件
  10. VitePluginStyleInject() // 添加样式注入插件
  11. ],
  12. build: {
  13. lib: {
  14. entry: 'index.js', // 入口文件
  15. name: 'MyComponentLibrary', // 全局变量名
  16. fileName: (format) => `index.${format}.js`, // 输出文件名
  17. },
  18. rollupOptions: {
  19. external: ['vue'], // 外部依赖
  20. output: {
  21. globals: {
  22. vue: 'Vue'
  23. },
  24. },
  25. input: componentName
  26. ? {
  27. [componentName]: `./${componentName}/index.js`,
  28. }
  29. : {},
  30. },
  31. outDir: `./${componentName}/dist`,
  32. },
  33. });