66 * found in the LICENSE file at https://angular.io/license
77 */
88import { interpolateName } from 'loader-utils' ;
9+ import * as path from 'path' ;
910import * as postcss from 'postcss' ;
1011import * as url from 'url' ;
1112import * as webpack from 'webpack' ;
@@ -52,7 +53,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
5253
5354 const dedupeSlashes = ( url : string ) => url . replace ( / \/ \/ + / g, '/' ) ;
5455
55- const process = async ( inputUrl : string , resourceCache : Map < string , string > ) => {
56+ const process = async ( inputUrl : string , context : string , resourceCache : Map < string , string > ) => {
5657 // If root-relative or absolute, leave as is
5758 if ( inputUrl . match ( / ^ (?: \w + : \/ \/ | d a t a : | c h r o m e : | # ) / ) ) {
5859 return inputUrl ;
@@ -63,7 +64,8 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
6364 return inputUrl . substr ( 1 ) ;
6465 }
6566
66- const cachedUrl = resourceCache . get ( inputUrl ) ;
67+ const cacheKey = path . resolve ( context , inputUrl ) ;
68+ const cachedUrl = resourceCache . get ( cacheKey ) ;
6769 if ( cachedUrl ) {
6870 return cachedUrl ;
6971 }
@@ -85,7 +87,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
8587 outputUrl = dedupeSlashes ( `/${ baseHref } /${ deployUrl } /${ inputUrl } ` ) ;
8688 }
8789
88- resourceCache . set ( inputUrl , outputUrl ) ;
90+ resourceCache . set ( cacheKey , outputUrl ) ;
8991
9092 return outputUrl ;
9193 }
@@ -102,7 +104,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
102104 } ) ;
103105 } ) ;
104106
105- const result = await resolve ( pathname as string , loader . context , resolver ) ;
107+ const result = await resolve ( pathname as string , context , resolver ) ;
106108
107109 return new Promise < string > ( ( resolve , reject ) => {
108110 loader . fs . readFile ( result , ( err : Error , content : Buffer ) => {
@@ -130,7 +132,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
130132 outputUrl = url . resolve ( deployUrl , outputUrl ) ;
131133 }
132134
133- resourceCache . set ( inputUrl , outputUrl ) ;
135+ resourceCache . set ( cacheKey , outputUrl ) ;
134136 resolve ( outputUrl ) ;
135137 } ) ;
136138 } ) ;
@@ -158,12 +160,16 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
158160 let match ;
159161 let lastIndex = 0 ;
160162 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+
161167 // tslint:disable-next-line:no-conditional-assignment
162168 while ( match = urlRegex . exec ( value ) ) {
163169 const originalUrl = match [ 1 ] || match [ 2 ] || match [ 3 ] ;
164170 let processedUrl ;
165171 try {
166- processedUrl = await process ( originalUrl , resourceCache ) ;
172+ processedUrl = await process ( originalUrl , context , resourceCache ) ;
167173 } catch ( err ) {
168174 loader . emitError ( decl . error ( err . message , { word : originalUrl } ) . toString ( ) ) ;
169175 continue ;
0 commit comments