utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.contextify = contextify;
  6. exports.getExposes = getExposes;
  7. exports.getNewUserRequest = getNewUserRequest;
  8. exports.interpolateName = interpolateName;
  9. exports.stringifyRequest = stringifyRequest;
  10. var _path = _interopRequireDefault(require("path"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function getNewUserRequest(request) {
  13. const splittedRequest = request.split("!");
  14. const lastPartRequest = splittedRequest.pop().split("?", 2);
  15. const pathObject = _path.default.parse(lastPartRequest[0]);
  16. pathObject.base = `${_path.default.basename(pathObject.base, pathObject.ext)}-exposed${pathObject.ext}`;
  17. lastPartRequest[0] = _path.default.format(pathObject);
  18. splittedRequest.push(lastPartRequest.join("?"));
  19. return splittedRequest.join("!");
  20. }
  21. function splitCommand(command) {
  22. const result = command.split("|").map(item => item.split(" ")).reduce((acc, val) => acc.concat(val), []);
  23. for (const item of result) {
  24. if (!item) {
  25. throw new Error(`Invalid command "${item}" in "${command}" for expose. There must be only one separator: " ", or "|".`);
  26. }
  27. }
  28. return result;
  29. }
  30. function parseBoolean(string, defaultValue = null) {
  31. if (typeof string === "undefined") {
  32. return defaultValue;
  33. }
  34. switch (string.toLowerCase()) {
  35. case "true":
  36. return true;
  37. case "false":
  38. return false;
  39. default:
  40. return defaultValue;
  41. }
  42. }
  43. function resolveExposes(item) {
  44. let result;
  45. if (typeof item === "string") {
  46. const splittedItem = splitCommand(item.trim());
  47. if (splittedItem.length > 3) {
  48. throw new Error(`Invalid "${item}" for exposes`);
  49. }
  50. result = {
  51. globalName: splittedItem[0],
  52. moduleLocalName: splittedItem[1],
  53. override: typeof splittedItem[2] !== "undefined" ? parseBoolean(splittedItem[2], false) :
  54. // eslint-disable-next-line no-undefined
  55. undefined
  56. };
  57. } else {
  58. result = item;
  59. }
  60. const nestedGlobalName = typeof result.globalName === "string" ? result.globalName.split(".") : result.globalName;
  61. return {
  62. ...result,
  63. globalName: nestedGlobalName
  64. };
  65. }
  66. function getExposes(items) {
  67. let result;
  68. const exposeItems = typeof items === "string" && items.includes(",") ? items.split(",") : items;
  69. if (typeof exposeItems === "string") {
  70. result = [resolveExposes(exposeItems)];
  71. } else {
  72. result = [].concat(exposeItems).map(item => resolveExposes(item));
  73. }
  74. return result;
  75. }
  76. // TODO simplify for the next major release
  77. function contextify(loaderContext, context, request) {
  78. if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") {
  79. loaderContext.utils.contextify(loaderContext.context, request);
  80. }
  81. return request.split("!").map(r => {
  82. const splitPath = r.split("?");
  83. if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
  84. splitPath[0] = _path.default.win32.relative(context, splitPath[0]);
  85. if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
  86. splitPath[0] = splitPath[0].replace(/\\/g, "/");
  87. }
  88. }
  89. if (/^\//.test(splitPath[0])) {
  90. splitPath[0] = _path.default.posix.relative(context, splitPath[0]);
  91. }
  92. if (!/^(\.\.\/|\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
  93. splitPath[0] = `./${splitPath[0]}`;
  94. }
  95. return splitPath.join("?");
  96. }).join("!");
  97. }
  98. function isAbsolutePath(str) {
  99. return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str);
  100. }
  101. // TODO simplify for the next major release
  102. function stringifyRequest(loaderContext, request) {
  103. if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") {
  104. return JSON.stringify(loaderContext.utils.contextify(loaderContext.context, request));
  105. }
  106. const splitted = request.split("!");
  107. const context = loaderContext.context || loaderContext.options && loaderContext.options.context;
  108. return JSON.stringify(splitted.map(part => {
  109. // First, separate singlePath from query, because the query might contain paths again
  110. const splittedPart = part.match(/^(.*?)(\?.*)/);
  111. const query = splittedPart ? splittedPart[2] : "";
  112. let singlePath = splittedPart ? splittedPart[1] : part;
  113. if (isAbsolutePath(singlePath) && context) {
  114. singlePath = _path.default.relative(context, singlePath);
  115. }
  116. return singlePath.replace(/\\/g, "/") + query;
  117. }).join("!"));
  118. }
  119. function interpolateName(loaderContext, filename) {
  120. let basename = "file";
  121. if (loaderContext.resourcePath) {
  122. const parsed = _path.default.parse(loaderContext.resourcePath);
  123. if (parsed.dir) {
  124. basename = parsed.name;
  125. }
  126. }
  127. return filename.replace(/\[name\]/gi, () => basename);
  128. }