1
1
import postcss from 'postcss' ;
2
2
import valueParser from 'postcss-value-parser' ;
3
- import { isUrlRequest , urlToRequest } from 'loader-utils' ;
4
3
5
- import { normalizeUrl , resolveRequests } from '../utils' ;
4
+ import { normalizeUrl , resolveRequests , isUrlRequestable } from '../utils' ;
6
5
7
6
const pluginName = 'postcss-import-parser' ;
8
7
@@ -77,23 +76,18 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
77
76
return ;
78
77
}
79
78
80
- let request ;
79
+ let normalizedUrl ;
81
80
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 ) ;
88
82
89
83
if ( isRequestable ) {
90
- url = normalizeUrl ( url , isStringValue ) ;
84
+ normalizedUrl = normalizeUrl ( url , isStringValue , options . rootContext ) ;
91
85
92
86
// Empty url after normalize - `@import '\
93
87
// \
94
88
// \
95
89
// ';
96
- if ( url . trim ( ) . length === 0 ) {
90
+ if ( normalizedUrl . trim ( ) . length === 0 ) {
97
91
result . warn ( `Unable to find uri in "${ atRule . toString ( ) } "` , {
98
92
node : atRule ,
99
93
} ) ;
@@ -104,7 +98,10 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
104
98
105
99
const media = valueParser . stringify ( nodes . slice ( 1 ) ) . trim ( ) . toLowerCase ( ) ;
106
100
107
- if ( options . filter && ! options . filter ( { url, media } ) ) {
101
+ if (
102
+ options . filter &&
103
+ ! options . filter ( { url : normalizedUrl || url , media } )
104
+ ) {
108
105
return ;
109
106
}
110
107
@@ -114,8 +111,8 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
114
111
115
112
tasks . push (
116
113
Promise . resolve ( index ) . then ( async ( currentIndex ) => {
117
- if ( isRequestable || request ) {
118
- const importKey = url ;
114
+ if ( isRequestable ) {
115
+ const importKey = normalizedUrl ;
119
116
let importName = importsMap . get ( importKey ) ;
120
117
121
118
if ( ! importName ) {
@@ -124,20 +121,13 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
124
121
125
122
const { resolver, context } = options ;
126
123
127
- const possibleRequest = [ url ] ;
128
-
129
- if ( request ) {
130
- possibleRequest . push ( request ) ;
131
- }
132
-
133
124
let resolvedUrl ;
134
125
135
126
try {
136
- resolvedUrl = await resolveRequests (
137
- resolver ,
138
- context ,
139
- possibleRequest
140
- ) ;
127
+ resolvedUrl = await resolveRequests ( resolver , context , [
128
+ normalizedUrl ,
129
+ url ,
130
+ ] ) ;
141
131
} catch ( error ) {
142
132
throw error ;
143
133
}
0 commit comments