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

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