optimize.ts 1.22 KB
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));
		},
	};
}