vite.config.build.disperse.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { defineConfig } from 'vite';
  2. import dts from 'vite-plugin-dts';
  3. import vue from '@vitejs/plugin-vue';
  4. import path from 'path';
  5. import config from './package.json';
  6. import configPkg from './src/config.json';
  7. import { terser } from 'rollup-plugin-terser';
  8. const banner = `/*!
  9. * ${config.name} v${config.version} ${new Date()}
  10. * (c) 2022 @jdf2e.
  11. * Released under the MIT License.
  12. */`;
  13. let input = {};
  14. configPkg.nav.map((item) => {
  15. item.packages.forEach((element) => {
  16. let { name, type } = element;
  17. input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index${type === 'methods' ? '.ts' : '.vue'}`;
  18. });
  19. });
  20. export default defineConfig({
  21. resolve: {
  22. alias: [{ find: '@', replacement: path.resolve(__dirname, './src') }]
  23. },
  24. plugins: [
  25. vue(),
  26. dts({
  27. insertTypesEntry: true,
  28. copyDtsFiles: false,
  29. cleanVueFileName: false,
  30. outputDir: path.resolve(__dirname, './dist/types'),
  31. include: path.resolve(__dirname, './src/packages/__VUE'),
  32. beforeWriteFile: (filePath: string, content: string) => {
  33. const fileContent = `import { App, PropType, CSSProperties } from 'vue';
  34. declare type Install<T> = T & {
  35. install(app: App): void;
  36. };
  37. `;
  38. const start = 'declare const _sfc_main:';
  39. const end = ';\nexport default _sfc_main;\n';
  40. let name = Object.keys(input).find((item: string) => item.toLowerCase() === filePath.split('/').slice(-2)[0]);
  41. name = name ? name.toLowerCase() : ' ';
  42. const remain = `
  43. declare module 'vue' {
  44. interface GlobalComponents {
  45. Nut${name[0].toUpperCase() + name.substr(1)}: typeof _sfc_main;
  46. }
  47. }
  48. `;
  49. const inputs = content.match(RegExp(`${start}([\\s\\S]*?)${end}`));
  50. const changeContent = inputs && inputs.length ? `${start} Install<${inputs[1]}>${end}${remain}` : content;
  51. return {
  52. filePath,
  53. content: fileContent + changeContent
  54. };
  55. }
  56. })
  57. ],
  58. build: {
  59. minify: false,
  60. lib: {
  61. entry: '',
  62. name: 'index',
  63. // fileName: (format) => format,
  64. formats: ['es']
  65. },
  66. rollupOptions: {
  67. // 请确保外部化那些你的库中不需要的依赖
  68. external: ['vue', 'vue-router', '@/packages/locale'],
  69. input,
  70. output: {
  71. banner,
  72. paths: {
  73. '@/packages/locale': '../locale/lang'
  74. },
  75. dir: path.resolve(__dirname, './dist/packages/_es'),
  76. entryFileNames: '[name].js',
  77. plugins: [
  78. terser({
  79. compress: {
  80. drop_console: true,
  81. drop_debugger: true
  82. }
  83. })
  84. ]
  85. }
  86. },
  87. emptyOutDir: false
  88. }
  89. });