Front end of the Slack clone application.

stringifyRequest.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. const path = require("path");
  3. const matchRelativePath = /^\.\.?[/\\]/;
  4. function isAbsolutePath(str) {
  5. return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
  6. }
  7. function isRelativePath(str) {
  8. return matchRelativePath.test(str);
  9. }
  10. function stringifyRequest(loaderContext, request) {
  11. const splitted = request.split("!");
  12. const context = loaderContext.context || (loaderContext.options && loaderContext.options.context);
  13. return JSON.stringify(splitted.map(part => {
  14. // First, separate singlePath from query, because the query might contain paths again
  15. const splittedPart = part.match(/^(.*?)(\?.*)/);
  16. let singlePath = splittedPart ? splittedPart[1] : part;
  17. const query = splittedPart ? splittedPart[2] : "";
  18. if(isAbsolutePath(singlePath) && context) {
  19. singlePath = path.relative(context, singlePath);
  20. if(isAbsolutePath(singlePath)) {
  21. // If singlePath still matches an absolute path, singlePath was on a different drive than context.
  22. // In this case, we leave the path platform-specific without replacing any separators.
  23. // @see https://github.com/webpack/loader-utils/pull/14
  24. return singlePath + query;
  25. }
  26. if(isRelativePath(singlePath) === false) {
  27. // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
  28. singlePath = "./" + singlePath;
  29. }
  30. }
  31. return singlePath.replace(/\\/g, "/") + query;
  32. }).join("!"));
  33. }
  34. module.exports = stringifyRequest;