Files
lingji-work-fe/config/utils.ts
rd 529b9f28d9 refactor(env): 重构环境变量配置并简化项目设置
-移除了大部分冗余环境变量,简化了配置结构
- 更新了 API 地址配置方式,使用 VITE_API_URL 替代原有变量
- 删除了未使用的代码文件,包括 apiCodes.ts、axiosHandler.ts 和 types.d.ts
- 调整了 vite 配置,使用环境变量中的 API地址
- 更新了 gitignore 文件,添加了 dist 目录的忽略项
2025-09-19 11:44:09 +08:00

74 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { sep } from 'node:path';
import { fileURLToPath, URL } from 'node:url';
import { accessSync, readdirSync } from 'node:fs';
export const resolve = (src: string) => fileURLToPath(new URL(`../${src}`, import.meta.url));
export const getSep = (src: string) => resolve(src).split(sep).join('/');
interface AccessType {
name: string;
dir: string;
extensions: string[];
}
const access = ({ name, dir, extensions }: AccessType) => {
const paths = ['/Index', '/index', `/${name}`].reduce(
(t: string[], o: string) => [...t, ...extensions.map((r) => `${o}.${r}`)],
[],
);
for (const path of paths) {
try {
accessSync(`${getSep('src')}/${dir}/${name}${path}`);
return path;
} catch {}
}
return undefined;
};
// 解决 nodejs 大小写不敏感不能正确读取路径vite 的自动导入组件不能热更新
interface PathParams {
folder: string;
}
export const getPath = (parentPath: string, { folder }: PathParams) => {
try {
const files = readdirSync(parentPath);
for (const file of files) {
if (file.toLowerCase() === folder) {
return `${parentPath}/${file}`;
}
}
} catch {}
throw new Error(`${parentPath}/${folder} is exist?`);
};
export const setResolve = (name: string, { prefix = 'Eo', dir = 'components' } = {}) => {
const partialName = name.slice(prefix.length);
const path = access({ name: partialName, dir, extensions: ['vue', 'js'] }) || '.vue';
return `${getSep('src')}/${dir}/${partialName}${path}`;
};
// resolver
export const layoutsResolver = () => (name: string) => {
if (name.startsWith('Layout')) {
return setResolve(name, { prefix: 'Layout', dir: 'layouts' });
}
};
// 本地服务器配置
export const setServerConfig =
({ env }: { env: Record<string, string> }) =>
(opts = {}): Record<string, any> => {
return {
port: Number(env.APP_PORT),
proxy: {
'/api': {
target: env.VITE_API_URL || 'http://lingjiapi.lvfunai.com/api',
changeOrigin: true,
rewrite: (path: string) => path.replace(/^\/api/, ''),
},
},
...opts,
};
};