vite.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { resolve } from 'node:path';
  2. import { defineConfig, type PluginOption } from 'vite';
  3. import vue from '@vitejs/plugin-vue';
  4. import vueJsx from '@vitejs/plugin-vue-jsx';
  5. import Components from 'unplugin-vue-components/vite';
  6. import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
  7. import Unocss from 'unocss/vite';
  8. const sourceDir = resolve(__dirname, '../../src');
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. publicDir: false,
  12. resolve: {
  13. alias: [
  14. {
  15. find: '@',
  16. replacement: sourceDir,
  17. },
  18. ],
  19. },
  20. esbuild: {
  21. drop: ['debugger'],
  22. pure: ['console.log'],
  23. },
  24. // 打包配置
  25. build: {
  26. target: 'esnext',
  27. lib: {
  28. entry: resolve(__dirname, './index.ts'), // 设置入口文件
  29. formats: ['es'],
  30. name: 'components', // 起个名字,安装、引入用
  31. fileName: (format) => `index.${format}.js`, // 打包后的文件名
  32. },
  33. rollupOptions: {
  34. treeshake: true,
  35. // make sure to externalize deps that shouldn't be bundled
  36. // into your library
  37. external: ['vue', 'ant-design-vue'],
  38. output: {
  39. // minifyInternalExports: false,
  40. // // Provide global variables to use in the UMD build
  41. // // for externalized deps
  42. // globals: {
  43. // vue: 'Vue',
  44. // 'ant-design-vue': 'AntDesignVue',
  45. // },
  46. // manualChunks: {
  47. // library: ['lodash-es', 'vue-i18n'],
  48. // },
  49. manualChunks(id) {
  50. if (id.includes('/src/locales/helper.ts')) {
  51. console.log('id: ', id);
  52. return 'vendor';
  53. } else if (id.includes('ant-design-vue')) {
  54. return 'vendor';
  55. }
  56. },
  57. },
  58. },
  59. },
  60. plugins: [
  61. vue(),
  62. vueJsx(),
  63. Unocss(),
  64. myPlugin(),
  65. Components({
  66. dts: false,
  67. dirs: ['../../src/components'],
  68. resolvers: [
  69. AntDesignVueResolver({
  70. importStyle: false, // css in js
  71. exclude: ['Button'],
  72. }),
  73. (componentName) => {
  74. // where `componentName` is always CapitalCase
  75. if (componentName === 'AButton') {
  76. return { name: componentName, from: resolve(sourceDir, 'components/basic/button/') };
  77. }
  78. },
  79. ],
  80. }),
  81. ],
  82. });
  83. function myPlugin(): PluginOption {
  84. const file = resolve(sourceDir, './permission');
  85. return {
  86. name: '@admin-pkg/components:transform-file',
  87. // transform(src, id) {
  88. // if (id.includes(file)) {
  89. // console.log('transform id: ', id);
  90. // return {
  91. // code: `export const hasPermission = () => true`,
  92. // map: null,
  93. // };
  94. // }
  95. // },
  96. load(id) {
  97. if (id.includes(file)) {
  98. console.log('load id: ', id);
  99. return `export const hasPermission = () => true`;
  100. }
  101. },
  102. };
  103. }