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

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