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