vite.config.build.taro.vue.disperse.ts 2.6 KB

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