source: trip-planner-front/node_modules/loader-utils/lib/stringifyRequest.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3const path = require('path');
4
5const matchRelativePath = /^\.\.?[/\\]/;
6
7function isAbsolutePath(str) {
8 return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
9}
10
11function isRelativePath(str) {
12 return matchRelativePath.test(str);
13}
14
15function stringifyRequest(loaderContext, request) {
16 const splitted = request.split('!');
17 const context =
18 loaderContext.context ||
19 (loaderContext.options && loaderContext.options.context);
20
21 return JSON.stringify(
22 splitted
23 .map((part) => {
24 // First, separate singlePath from query, because the query might contain paths again
25 const splittedPart = part.match(/^(.*?)(\?.*)/);
26 const query = splittedPart ? splittedPart[2] : '';
27 let singlePath = splittedPart ? splittedPart[1] : part;
28
29 if (isAbsolutePath(singlePath) && context) {
30 singlePath = path.relative(context, singlePath);
31
32 if (isAbsolutePath(singlePath)) {
33 // If singlePath still matches an absolute path, singlePath was on a different drive than context.
34 // In this case, we leave the path platform-specific without replacing any separators.
35 // @see https://github.com/webpack/loader-utils/pull/14
36 return singlePath + query;
37 }
38
39 if (isRelativePath(singlePath) === false) {
40 // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
41 singlePath = './' + singlePath;
42 }
43 }
44
45 return singlePath.replace(/\\/g, '/') + query;
46 })
47 .join('!')
48 );
49}
50
51module.exports = stringifyRequest;
Note: See TracBrowser for help on using the repository browser.