Skip to content

Commit 671b8d5

Browse files
refactor: code
1 parent 4ae4e5c commit 671b8d5

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default function loader(content, map, meta) {
5353
mainFiles: ['index', '...'],
5454
extensions: ['.css'],
5555
restrictions: [/\.css$/i],
56+
conditionNames: ['style'],
5657
});
5758

5859
plugins.push(

src/plugins/postcss-import-parser.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
3-
import { isUrlRequest, urlToRequest } from 'loader-utils';
43

5-
import { normalizeUrl, resolveRequests } from '../utils';
4+
import { normalizeUrl, resolveRequests, isUrlRequestable } from '../utils';
65

76
const pluginName = 'postcss-import-parser';
87

@@ -77,23 +76,18 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
7776
return;
7877
}
7978

80-
let request;
79+
let normalizedUrl;
8180

82-
// May be url is server-relative url, but not //example.com
83-
if (url.charAt(0) === '/' && url.charAt(1) !== '/') {
84-
request = urlToRequest(url, options.rootContext);
85-
}
86-
87-
const isRequestable = isUrlRequest(url);
81+
const isRequestable = isUrlRequestable(url);
8882

8983
if (isRequestable) {
90-
url = normalizeUrl(url, isStringValue);
84+
normalizedUrl = normalizeUrl(url, isStringValue, options.rootContext);
9185

9286
// Empty url after normalize - `@import '\
9387
// \
9488
// \
9589
// ';
96-
if (url.trim().length === 0) {
90+
if (normalizedUrl.trim().length === 0) {
9791
result.warn(`Unable to find uri in "${atRule.toString()}"`, {
9892
node: atRule,
9993
});
@@ -104,7 +98,10 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
10498

10599
const media = valueParser.stringify(nodes.slice(1)).trim().toLowerCase();
106100

107-
if (options.filter && !options.filter({ url, media })) {
101+
if (
102+
options.filter &&
103+
!options.filter({ url: normalizedUrl || url, media })
104+
) {
108105
return;
109106
}
110107

@@ -114,8 +111,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
114111

115112
tasks.push(
116113
Promise.resolve(index).then(async (currentIndex) => {
117-
if (isRequestable || request) {
118-
const importKey = url;
114+
if (isRequestable) {
115+
const importKey = normalizedUrl;
119116
let importName = importsMap.get(importKey);
120117

121118
if (!importName) {
@@ -124,20 +121,13 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
124121

125122
const { resolver, context } = options;
126123

127-
const possibleRequest = [url];
128-
129-
if (request) {
130-
possibleRequest.push(request);
131-
}
132-
133124
let resolvedUrl;
134125

135126
try {
136-
resolvedUrl = await resolveRequests(
137-
resolver,
138-
context,
139-
possibleRequest
140-
);
127+
resolvedUrl = await resolveRequests(resolver, context, [
128+
normalizedUrl,
129+
url,
130+
]);
141131
} catch (error) {
142132
throw error;
143133
}

src/utils.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
*/
55
import path from 'path';
66

7-
import { stringifyRequest, urlToRequest, interpolateName } from 'loader-utils';
7+
import {
8+
stringifyRequest,
9+
urlToRequest,
10+
interpolateName,
11+
isUrlRequest,
12+
} from 'loader-utils';
813
import normalizePath from 'normalize-path';
914
import cssesc from 'cssesc';
1015
import modulesValues from 'postcss-modules-values';
@@ -18,6 +23,7 @@ const unescapeRegExp = new RegExp(
1823
`\\\\([\\da-f]{1,6}${whitespace}?|(${whitespace})|.)`,
1924
'ig'
2025
);
26+
const matchNativeWin32Path = /^[A-Z]:[/\\]|^\\\\/i;
2127

2228
function unescape(str) {
2329
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
@@ -72,17 +78,19 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
7278
).replace(/\\\[local\\\]/gi, localName);
7379
}
7480

75-
function normalizeUrl(url, isStringValue) {
76-
const matchNativeWin32Path = /^[A-Z]:[/\\]|^\\\\/i;
77-
81+
function normalizeUrl(url, isStringValue, rootContext) {
7882
let normalizedUrl = url;
7983

8084
if (isStringValue && /\\[\n]/.test(normalizedUrl)) {
8185
normalizedUrl = normalizedUrl.replace(/\\[\n]/g, '');
8286
}
8387

84-
return matchNativeWin32Path.test(url)
85-
? urlToRequest(url, true)
88+
if (matchNativeWin32Path.test(normalizedUrl)) {
89+
return normalizedUrl;
90+
}
91+
92+
return mayBeServerRelativeUrl(normalizedUrl)
93+
? urlToRequest(decodeURIComponent(unescape(normalizedUrl)), rootContext)
8694
: urlToRequest(decodeURIComponent(unescape(normalizedUrl)));
8795
}
8896

@@ -452,6 +460,25 @@ function sortByName(array, orderNames) {
452460
return result;
453461
}
454462

463+
/*
464+
* May be url is server-relative url, but not //example.com
465+
* */
466+
function mayBeServerRelativeUrl(url) {
467+
if (url.charAt(0) === '/' && !/^\/\//.test(url)) {
468+
return true;
469+
}
470+
471+
return false;
472+
}
473+
474+
function isUrlRequestable(url) {
475+
if (mayBeServerRelativeUrl(url)) {
476+
return true;
477+
}
478+
479+
return isUrlRequest(url);
480+
}
481+
455482
export {
456483
normalizeUrl,
457484
getFilter,
@@ -464,4 +491,5 @@ export {
464491
shouldUseModulesPlugins,
465492
resolveRequests,
466493
sortByName,
494+
isUrlRequestable,
467495
};

0 commit comments

Comments
 (0)