vite.config.build.disperse.ts 2.4 KB

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