vehicle-monitor/build/utils.ts

153 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-02-26 16:31:59 +08:00
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
2025-03-14 15:25:47 +08:00
import fs from 'fs';
import path from 'path';
2025-02-26 16:31:59 +08:00
import { visualizer } from 'rollup-plugin-visualizer';
import { loadEnv } from 'vite';
import viteCompression from 'vite-plugin-compression';
2025-02-24 18:23:33 +08:00
2025-03-14 15:25:47 +08:00
import { buildEnv } from './buildEnv';
2025-02-24 18:23:33 +08:00
export const root: string = process.cwd();
/**
* @description
* @param dir `build`
* @param metaUrl `url``build``import.meta.url`
*/
2025-03-14 15:25:47 +08:00
// @ts-ignore
2025-02-26 16:31:59 +08:00
export const pathResolve = (dir = '.', metaUrl = import.meta.url) => {
2025-02-25 23:14:50 +08:00
// 当前文件目录的绝对路径
const currentFileDir = dirname(fileURLToPath(metaUrl));
// build 目录的绝对路径
2025-02-26 16:31:59 +08:00
const buildDir = resolve(currentFileDir, 'build');
2025-02-25 23:14:50 +08:00
// 解析的绝对路径
const resolvedPath = resolve(currentFileDir, dir);
// 检查解析的绝对路径是否在 build 目录内
if (resolvedPath.startsWith(buildDir)) {
// 在 build 目录内,返回当前文件路径
return fileURLToPath(metaUrl);
}
// 不在 build 目录内,返回解析后的绝对路径
return resolvedPath;
2025-02-24 18:23:33 +08:00
};
/**
*
* @param mode
* @param prefix
* @link https://cn.vite.dev/config/#using-environment-variables-in-config
*/
2025-03-13 23:01:44 +08:00
// @ts-ignore
export const wrapperEnv = (mode: string, prefix: string = ''): ViteEnv => {
const env: any = loadEnv(mode, root, prefix);
2025-02-25 18:29:26 +08:00
2025-02-25 23:14:50 +08:00
// 将变量转换指定类型
for (const envName of Object.keys(env)) {
2025-03-13 23:01:44 +08:00
let realName: string | boolean | number = env[envName].replace(/\\n/g, '\n');
2025-02-26 16:31:59 +08:00
realName = realName === 'true' ? true : realName === 'false' ? false : realName;
2025-02-25 18:29:26 +08:00
2025-02-26 16:31:59 +08:00
if (envName === 'VITE_PORT') {
2025-02-25 23:14:50 +08:00
realName = Number(realName);
}
env[envName] = realName;
2025-03-13 23:01:44 +08:00
// @ts-ignore
2025-02-25 23:14:50 +08:00
process.env[envName] = realName;
}
return env;
2025-02-24 18:23:33 +08:00
};
2025-02-24 22:45:14 +08:00
/* 打包分析 */
export const report = () => {
2025-02-25 23:14:50 +08:00
const lifecycle = process.env.npm_lifecycle_event;
2025-02-26 16:31:59 +08:00
return lifecycle === 'report'
? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
2025-02-25 23:14:50 +08:00
: (null as any);
2025-02-24 22:45:14 +08:00
};
/* 启用gzip压缩 */
2025-03-13 23:01:44 +08:00
export const compressPack = (mode: string) => {
2025-02-25 23:14:50 +08:00
const { VITE_COMPRESSION } = wrapperEnv(mode);
2025-02-24 22:45:14 +08:00
2025-02-26 16:31:59 +08:00
return VITE_COMPRESSION == 'gzip' ? viteCompression({ threshold: 1024000 }) : null;
2025-02-24 22:45:14 +08:00
};
2025-03-14 15:25:47 +08:00
/**
*
* @returns
*/
export const logOutputSize = (): string => {
const outDir = `../${buildEnv().outDir}`;
function convertSize(size: number) {
const units: Array<string> = ['byte', 'KB', 'MB', 'GB'];
// 输入的单位是否存在
let index = 0;
while (size >= 1024) {
size /= 1024;
index++;
}
return `${size.toFixed(2)} ${units[index]}`;
}
2025-05-24 19:31:33 +08:00
/**
*
* @param folderPath
* @param currentDepth 使
* @returns
*/
function getFolderSize(folderPath: string, currentDepth: number = 0): number {
// 公共常量定义
const EXCLUDED_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg'];
const MAX_DEPTH = 10; // 最大递归深度限制
// 安全检查
if (!fs.existsSync(folderPath)) {
throw new Error(`文件夹不存在: ${folderPath}`);
}
if (currentDepth > MAX_DEPTH) {
console.warn(`达到最大递归深度 ${MAX_DEPTH},停止遍历: ${folderPath}`);
return 0;
}
2025-03-14 15:25:47 +08:00
2025-05-24 19:31:33 +08:00
let size = 0;
2025-03-14 15:25:47 +08:00
2025-05-24 19:31:33 +08:00
try {
const files = fs.readdirSync(folderPath);
for (const fileName of files) {
const filePath = path.join(folderPath, fileName);
try {
const stats = fs.statSync(filePath);
if (stats.isFile()) {
// 检查文件扩展名是否在排除列表中
const ext = path.extname(fileName).toLowerCase();
if (!EXCLUDED_FILE_EXTENSIONS.includes(ext)) {
size += stats.size;
}
} else if (stats.isDirectory()) {
size += getFolderSize(filePath, currentDepth + 1);
}
} catch (error) {
console.error(`无法访问文件: ${filePath}`, error);
}
2025-03-14 15:25:47 +08:00
}
2025-05-24 19:31:33 +08:00
} catch (error) {
console.error(`无法读取目录: ${folderPath}`, error);
}
2025-03-14 15:25:47 +08:00
return size;
}
const folderSize = getFolderSize(path.resolve(__dirname, outDir));
return convertSize(folderSize);
};