UNPKG

@altano/html-cdnify

Version:

Transform the relative URLs in your HTML markup (e.g. scripts, stylesheets, images) to use your CDN URL.

29 lines (27 loc) 1.08 kB
import url from "node:url"; import path from "node:path"; //#region src/urlConverter.ts function isLocalPath(filePath) { return typeof filePath === "string" && filePath.length > 0 && filePath.indexOf("//") === -1 && filePath.indexOf("data:") !== 0; } /** * Resolve oldUrl against pathOldUrlIsRelativeTo, and then prepend newUrlBase */ var urlConverter_default = (newUrlBase, oldUrl, pathOldUrlIsRelativeTo) => { if (!isLocalPath(oldUrl)) return oldUrl; else { oldUrl = oldUrl.trim(); let relativePath = "."; if (pathOldUrlIsRelativeTo) { const isDirAlready = pathOldUrlIsRelativeTo.endsWith("/"); const fileDirName = isDirAlready ? pathOldUrlIsRelativeTo : path.dirname(pathOldUrlIsRelativeTo) + "/"; relativePath = url.resolve(fileDirName, oldUrl); } else relativePath = oldUrl; if (relativePath.startsWith("/")) relativePath = relativePath.slice(1); if (!newUrlBase.endsWith("/")) newUrlBase += "/"; return url.resolve(newUrlBase, relativePath); } }; //#endregion export { urlConverter_default as default }; //# sourceMappingURL=urlConverter.js.map