/** * getPublicUrl 选项接口 */ export interface GetPublicUrlOptions { /** 是否返回完整路径 */ isFullPath?: boolean; } /** * 获取 public 文件路径(自动拼接 hash 数据) * @param url 文件路径 * @param opt 选项 * @returns 完整的 public 文件路径 */ export function getPublicUrl(url: string | any, opt?: GetPublicUrlOptions): string | any { if (typeof url !== "string") { return url; } const { isFullPath } = opt || {}; const baseURL = (process.env.WEBPACK_PUBLIC_PATH || ".")?.replace(/^\//, "")?.replace(/\/$/, ""); if (isFullPath) { const fullPathname = (location.origin + location.pathname) .replace(/[^\/]+.html\/?$/, "") .replace(/\/$/, ""); return `${fullPathname}/${baseURL?.replace(/^\./, "")}/${url?.replace(/^\//, "")}`; } return `${baseURL}/${url?.replace(/^\//, "")}`; }