6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { interpolateName } from 'loader-utils' ;
9
+ import * as path from 'path' ;
9
10
import * as postcss from 'postcss' ;
10
11
import * as url from 'url' ;
11
12
import * as webpack from 'webpack' ;
@@ -52,7 +53,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
52
53
53
54
const dedupeSlashes = ( url : string ) => url . replace ( / \/ \/ + / g, '/' ) ;
54
55
55
- const process = async ( inputUrl : string , resourceCache : Map < string , string > ) => {
56
+ const process = async ( inputUrl : string , context : string , resourceCache : Map < string , string > ) => {
56
57
// If root-relative or absolute, leave as is
57
58
if ( inputUrl . match ( / ^ (?: \w + : \/ \/ | d a t a : | c h r o m e : | # ) / ) ) {
58
59
return inputUrl ;
@@ -63,7 +64,8 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
63
64
return inputUrl . substr ( 1 ) ;
64
65
}
65
66
66
- const cachedUrl = resourceCache . get ( inputUrl ) ;
67
+ const cacheKey = path . resolve ( context , inputUrl ) ;
68
+ const cachedUrl = resourceCache . get ( cacheKey ) ;
67
69
if ( cachedUrl ) {
68
70
return cachedUrl ;
69
71
}
@@ -85,7 +87,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
85
87
outputUrl = dedupeSlashes ( `/${ baseHref } /${ deployUrl } /${ inputUrl } ` ) ;
86
88
}
87
89
88
- resourceCache . set ( inputUrl , outputUrl ) ;
90
+ resourceCache . set ( cacheKey , outputUrl ) ;
89
91
90
92
return outputUrl ;
91
93
}
@@ -102,7 +104,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
102
104
} ) ;
103
105
} ) ;
104
106
105
- const result = await resolve ( pathname as string , loader . context , resolver ) ;
107
+ const result = await resolve ( pathname as string , context , resolver ) ;
106
108
107
109
return new Promise < string > ( ( resolve , reject ) => {
108
110
loader . fs . readFile ( result , ( err : Error , content : Buffer ) => {
@@ -130,7 +132,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
130
132
outputUrl = url . resolve ( deployUrl , outputUrl ) ;
131
133
}
132
134
133
- resourceCache . set ( inputUrl , outputUrl ) ;
135
+ resourceCache . set ( cacheKey , outputUrl ) ;
134
136
resolve ( outputUrl ) ;
135
137
} ) ;
136
138
} ) ;
@@ -158,12 +160,16 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
158
160
let match ;
159
161
let lastIndex = 0 ;
160
162
let modified = false ;
163
+
164
+ // We want to load it relative to the file that imports
165
+ const context = path . dirname ( decl . source . input . file ) ;
166
+
161
167
// tslint:disable-next-line:no-conditional-assignment
162
168
while ( match = urlRegex . exec ( value ) ) {
163
169
const originalUrl = match [ 1 ] || match [ 2 ] || match [ 3 ] ;
164
170
let processedUrl ;
165
171
try {
166
- processedUrl = await process ( originalUrl , resourceCache ) ;
172
+ processedUrl = await process ( originalUrl , context , resourceCache ) ;
167
173
} catch ( err ) {
168
174
loader . emitError ( decl . error ( err . message , { word : originalUrl } ) . toString ( ) ) ;
169
175
continue ;
0 commit comments