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