filepath.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var filepath_exports = {};
  19. __export(filepath_exports, {
  20. getFilePath: () => getFilePath,
  21. getFilePathWithoutDefaultDocument: () => getFilePathWithoutDefaultDocument
  22. });
  23. module.exports = __toCommonJS(filepath_exports);
  24. const getFilePath = (options) => {
  25. let filename = options.filename;
  26. const defaultDocument = options.defaultDocument || "index.html";
  27. if (filename.endsWith("/")) {
  28. filename = filename.concat(defaultDocument);
  29. } else if (!filename.match(/\.[a-zA-Z0-9_-]+$/)) {
  30. filename = filename.concat("/" + defaultDocument);
  31. }
  32. const path = getFilePathWithoutDefaultDocument({
  33. root: options.root,
  34. filename
  35. });
  36. return path;
  37. };
  38. const getFilePathWithoutDefaultDocument = (options) => {
  39. let root = options.root || "";
  40. let filename = options.filename;
  41. if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {
  42. return;
  43. }
  44. filename = filename.replace(/^\.?[\/\\]/, "");
  45. filename = filename.replace(/\\/, "/");
  46. root = root.replace(/\/$/, "");
  47. let path = root ? root + "/" + filename : filename;
  48. path = path.replace(/^\.?\//, "");
  49. if (root[0] !== "/" && path[0] === "/") {
  50. return;
  51. }
  52. return path;
  53. };
  54. // Annotate the CommonJS export names for ESM import in node:
  55. 0 && (module.exports = {
  56. getFilePath,
  57. getFilePathWithoutDefaultDocument
  58. });