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

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