Skip to content

Commit 3882705

Browse files
refactor: stringifyRequest and urlToRequest added in utils
1 parent 1f15510 commit 3882705

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
import { stringifyRequest } from "loader-utils";
5+
66
import postcss from "postcss";
77
import postcssPkg from "postcss/package.json";
88
import { satisfies } from "semver";
@@ -27,6 +27,7 @@ import {
2727
normalizeSourceMap,
2828
sort,
2929
combineRequests,
30+
stringifyRequest,
3031
} from "./utils";
3132

3233
export default async function loader(content, map, meta) {

src/utils.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,99 @@
55
import { fileURLToPath } from "url";
66
import path from "path";
77

8-
import { urlToRequest, interpolateName } from "loader-utils";
8+
import { interpolateName } from "loader-utils";
99
import modulesValues from "postcss-modules-values";
1010
import localByDefault from "postcss-modules-local-by-default";
1111
import extractImports from "postcss-modules-extract-imports";
1212
import modulesScope from "postcss-modules-scope";
1313

1414
const WEBPACK_IGNORE_COMMENT_REGEXP = /webpackIgnore:(\s+)?(true|false)/;
1515

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+
16101
// eslint-disable-next-line no-useless-escape
17102
const regexSingleEscape = /[ -,.\/:-@[\]\^`{-~]/;
18103
const regexExcessiveSpaces =
@@ -959,4 +1044,5 @@ export {
9591044
WEBPACK_IGNORE_COMMENT_REGEXP,
9601045
combineRequests,
9611046
camelCase,
1047+
stringifyRequest,
9621048
};

test/sourceMap-option.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ describe('"sourceMap" option', () => {
602602

603603
const extractedCSS = readAsset(chunkName, compiler, stats);
604604

605-
expect(chunkName).toBe("main.695010bdb768b7260e76.css");
605+
expect(chunkName).toBe("main.166835fe802ce402ea56.css");
606606

607607
expect(
608608
extractedCSS.replace(

0 commit comments

Comments
 (0)