Front end of the Slack clone application.

urlToRequest.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. // we can't use path.win32.isAbsolute because it also matches paths starting with a forward slash
  3. const matchNativeWin32Path = /^[A-Z]:[/\\]|^\\\\/i;
  4. function urlToRequest(url, root) {
  5. const moduleRequestRegex = /^[^?]*~/;
  6. let request;
  7. if(matchNativeWin32Path.test(url)) {
  8. // absolute windows path, keep it
  9. request = url;
  10. } else if(root !== undefined && root !== false && /^\//.test(url)) {
  11. // if root is set and the url is root-relative
  12. switch(typeof root) {
  13. // 1. root is a string: root is prefixed to the url
  14. case "string":
  15. // special case: `~` roots convert to module request
  16. if(moduleRequestRegex.test(root)) {
  17. request = root.replace(/([^~\/])$/, "$1/") + url.slice(1);
  18. } else {
  19. request = root + url;
  20. }
  21. break;
  22. // 2. root is `true`: absolute paths are allowed
  23. // *nix only, windows-style absolute paths are always allowed as they doesn't start with a `/`
  24. case "boolean":
  25. request = url;
  26. break;
  27. default:
  28. throw new Error("Unexpected parameters to loader-utils 'urlToRequest': url = " + url + ", root = " + root + ".");
  29. }
  30. } else if(/^\.\.?\//.test(url)) {
  31. // A relative url stays
  32. request = url;
  33. } else {
  34. // every other url is threaded like a relative url
  35. request = "./" + url;
  36. }
  37. // A `~` makes the url an module
  38. if(moduleRequestRegex.test(request)) {
  39. request = request.replace(moduleRequestRegex, "");
  40. }
  41. return request;
  42. }
  43. module.exports = urlToRequest;