1
|
'use strict';
|
2
|
|
3
|
const path = require('path');
|
4
|
|
5
|
const matchRelativePath = /^\.\.?[/\\]/;
|
6
|
|
7
|
function isAbsolutePath(str) {
|
8
|
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
|
9
|
}
|
10
|
|
11
|
function isRelativePath(str) {
|
12
|
return matchRelativePath.test(str);
|
13
|
}
|
14
|
|
15
|
function 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
|
|
51
|
module.exports = stringifyRequest;
|