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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: true,
  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 } from 'vue';
  35. declare type Install<T> = T & {
  36. install(app: App): void;
  37. };
  38. `;
  39. const start = 'declare const _default:';
  40. const end = ';\nexport default _default;\n';
  41. const remain = `
  42. declare module 'vue' {
  43. interface GlobalComponents {
  44. Nut${Object.keys(input).find(
  45. (item: string) => item.toLowerCase() === filePath.split('/').slice(-2)[0]
  46. )}: typeof _default;
  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. terserOptions: {
  62. compress: {
  63. drop_console: true,
  64. drop_debugger: true
  65. }
  66. },
  67. lib: {
  68. entry: '',
  69. name: 'index',
  70. // fileName: (format) => format,
  71. formats: ['es']
  72. },
  73. rollupOptions: {
  74. // 请确保外部化那些你的库中不需要的依赖
  75. external: ['vue', 'vue-router', '@tarojs/taro'],
  76. input,
  77. output: {
  78. banner,
  79. dir: path.resolve(__dirname, './dist/packages/_es'),
  80. entryFileNames: '[name].js'
  81. }
  82. },
  83. emptyOutDir: false
  84. }
  85. });