|
5 | 5 | import { fileURLToPath } from "url";
|
6 | 6 | import path from "path";
|
7 | 7 |
|
8 |
| -import { urlToRequest, interpolateName } from "loader-utils"; |
| 8 | +import { interpolateName } from "loader-utils"; |
9 | 9 | import modulesValues from "postcss-modules-values";
|
10 | 10 | import localByDefault from "postcss-modules-local-by-default";
|
11 | 11 | import extractImports from "postcss-modules-extract-imports";
|
12 | 12 | import modulesScope from "postcss-modules-scope";
|
13 | 13 |
|
14 | 14 | const WEBPACK_IGNORE_COMMENT_REGEXP = /webpackIgnore:(\s+)?(true|false)/;
|
15 | 15 |
|
| 16 | +const matchRelativePath = /^\.\.?[/\\]/; |
| 17 | + |
| 18 | +function isAbsolutePath(str) { |
| 19 | + return path.posix.isAbsolute(str) || path.win32.isAbsolute(str); |
| 20 | +} |
| 21 | + |
| 22 | +function isRelativePath(str) { |
| 23 | + return matchRelativePath.test(str); |
| 24 | +} |
| 25 | + |
| 26 | +function stringifyRequest(loaderContext, request) { |
| 27 | + const splitted = request.split("!"); |
| 28 | + const context = |
| 29 | + loaderContext.context || |
| 30 | + (loaderContext.options && loaderContext.options.context); |
| 31 | + |
| 32 | + return JSON.stringify( |
| 33 | + splitted |
| 34 | + .map((part) => { |
| 35 | + // First, separate singlePath from query, because the query might contain paths again |
| 36 | + const splittedPart = part.match(/^(.*?)(\?.*)/); |
| 37 | + const query = splittedPart ? splittedPart[2] : ""; |
| 38 | + let singlePath = splittedPart ? splittedPart[1] : part; |
| 39 | + |
| 40 | + if (isAbsolutePath(singlePath) && context) { |
| 41 | + singlePath = path.relative(context, singlePath); |
| 42 | + |
| 43 | + if (isAbsolutePath(singlePath)) { |
| 44 | + // If singlePath still matches an absolute path, singlePath was on a different drive than context. |
| 45 | + // In this case, we leave the path platform-specific without replacing any separators. |
| 46 | + // @see https://github.com/webpack/loader-utils/pull/14 |
| 47 | + return singlePath + query; |
| 48 | + } |
| 49 | + |
| 50 | + if (isRelativePath(singlePath) === false) { |
| 51 | + // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules). |
| 52 | + singlePath = `./${singlePath}`; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return singlePath.replace(/\\/g, "/") + query; |
| 57 | + }) |
| 58 | + .join("!") |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +// we can't use path.win32.isAbsolute because it also matches paths starting with a forward slash |
| 63 | +const matchNativeWin32Path = /^[A-Z]:[/\\]|^\\\\/i; |
| 64 | + |
| 65 | +function urlToRequest(url, root) { |
| 66 | + // Do not rewrite an empty url |
| 67 | + if (url === "") { |
| 68 | + return ""; |
| 69 | + } |
| 70 | + |
| 71 | + const moduleRequestRegex = /^[^?]*~/; |
| 72 | + let request; |
| 73 | + |
| 74 | + if (matchNativeWin32Path.test(url)) { |
| 75 | + // absolute windows path, keep it |
| 76 | + request = url; |
| 77 | + } else if (typeof root !== "undefined" && /^\//.test(url)) { |
| 78 | + // if root is set and the url is root-relative |
| 79 | + // special case: `~` roots convert to module request |
| 80 | + if (moduleRequestRegex.test(root)) { |
| 81 | + request = root.replace(/([^~/])$/, "$1/") + url.slice(1); |
| 82 | + } else { |
| 83 | + request = root + url; |
| 84 | + } |
| 85 | + } else if (/^\.\.?\//.test(url)) { |
| 86 | + // A relative url stays |
| 87 | + request = url; |
| 88 | + } else { |
| 89 | + // every other url is threaded like a relative url |
| 90 | + request = `./${url}`; |
| 91 | + } |
| 92 | + |
| 93 | + // A `~` makes the url an module |
| 94 | + if (moduleRequestRegex.test(request)) { |
| 95 | + request = request.replace(moduleRequestRegex, ""); |
| 96 | + } |
| 97 | + |
| 98 | + return request; |
| 99 | +} |
| 100 | + |
16 | 101 | // eslint-disable-next-line no-useless-escape
|
17 | 102 | const regexSingleEscape = /[ -,.\/:-@[\]\^`{-~]/;
|
18 | 103 | const regexExcessiveSpaces =
|
@@ -959,4 +1044,5 @@ export {
|
959 | 1044 | WEBPACK_IGNORE_COMMENT_REGEXP,
|
960 | 1045 | combineRequests,
|
961 | 1046 | camelCase,
|
| 1047 | + stringifyRequest, |
962 | 1048 | };
|
0 commit comments