optimize.ts
1.22 KB
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
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootPath = path.resolve(__dirname, "../");
function optimizeAntdComponents(moduleName: string): string[] {
const moduleESPath = `${moduleName}/es`;
const nodeModulePath = `./node_modules/${moduleESPath}`;
const includes: string[] = [ moduleESPath ];
const folders = fs.readdirSync(path.resolve(rootPath, nodeModulePath));
folders.forEach((name) => {
const folderName = path.resolve(rootPath, nodeModulePath, name);
const stat = fs.lstatSync(folderName);
if (stat.isDirectory()) {
const styleFolder = path.resolve(folderName, "style");
if (fs.existsSync(styleFolder)) {
const _stat = fs.lstatSync(styleFolder);
if (_stat.isDirectory()) {
includes.push(`${moduleESPath}/${name}/style`);
}
}
}
});
return includes;
}
export function optimizeDeps() {
return {
name: "optimizeDeps",
configResolved: async (config) => {
const components = [ ...optimizeAntdComponents("ant-design-vue") ];
const concat = config.optimizeDeps.include.concat(components);
config.optimizeDeps.include = Array.from(new Set(concat));
},
};
}