@@ -123,10 +123,13 @@ module.exports = function loader(content, map) {
123123 . warnings ( )
124124 . forEach ( ( warning ) => this . emitWarning ( new Warning ( warning ) ) ) ;
125125
126+ const messages = result . messages || [ ] ;
126127 const { camelCase, exportOnlyLocals, importLoaders } = options ;
127- const { importItems, urlItems, exports } = parserOptions ;
128+ const { importItems, exports } = parserOptions ;
129+
128130 // Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
129131 const importUrlPrefix = getImportPrefix ( this , importLoaders ) ;
132+
130133 // Prepare replacer to change from `___CSS_LOADER_IMPORT___INDEX___` to `require('./file.css').locals`
131134 const importItemReplacer = ( item ) => {
132135 const match = placholderRegExps . importItem . exec ( item ) ;
@@ -203,40 +206,40 @@ module.exports = function loader(content, map) {
203206 // helper for ensuring valid CSS strings from requires
204207 let urlEscapeHelperCode = '' ;
205208
206- if ( resolveUrl && urlItems && urlItems . length > 0 ) {
207- urlEscapeHelperCode = `var escape = require(${ stringifyRequest (
208- this ,
209- require . resolve ( './runtime/escape.js' )
210- ) } );\n`;
211-
212- cssAsString = cssAsString . replace (
213- placholderRegExps . urlItemG ,
214- ( item ) => {
215- const match = placholderRegExps . urlItem . exec ( item ) ;
216- const idx = Number ( match [ 1 ] ) ;
217-
218- if ( ! urlItems [ idx ] ) {
219- return item ;
220- }
221-
222- const urlItem = urlItems [ idx ] ;
223- const { url } = urlItem ;
224- // Remove `#hash` and `?#hash` from `require`
225- const [ normalizedUrl , singleQuery , hashValue ] = url . split ( / ( \? ) ? # / ) ;
226- const hash =
227- singleQuery || hashValue
228- ? `"${ singleQuery ? '?' : '' } ${
229- hashValue ? `#${ hashValue } ` : ''
230- } "`
231- : '' ;
232-
233- return `" + escape(require(${ stringifyRequest (
209+ messages
210+ . filter ( ( message ) => message . type === 'url' && resolveUrl )
211+ . forEach ( ( message ) => {
212+ if ( ! urlEscapeHelperCode ) {
213+ urlEscapeHelperCode = `var escape = require(${ stringifyRequest (
234214 this ,
235- normalizedUrl
236- ) } )${ hash ? ` + ${ hash } ` : '' } ) + " `;
215+ require . resolve ( './runtime/escape.js' )
216+ ) } );\n `;
237217 }
238- ) ;
239- }
218+
219+ const { item } = message ;
220+ const { url, placeholder } = item ;
221+
222+ cssAsString = cssAsString . replace (
223+ new RegExp ( placeholder , 'g' ) ,
224+ ( ) => {
225+ // Remove `#hash` and `?#hash` from `require`
226+ const [ normalizedUrl , singleQuery , hashValue ] = url . split (
227+ / ( \? ) ? # /
228+ ) ;
229+ const hash =
230+ singleQuery || hashValue
231+ ? `"${ singleQuery ? '?' : '' } ${
232+ hashValue ? `#${ hashValue } ` : ''
233+ } "`
234+ : '' ;
235+
236+ return `" + escape(require(${ stringifyRequest (
237+ this ,
238+ normalizedUrl
239+ ) } )${ hash ? ` + ${ hash } ` : '' } ) + "`;
240+ }
241+ ) ;
242+ } ) ;
240243
241244 if ( exportCode ) {
242245 exportCode = `exports.locals = ${ exportCode } ;` ;
0 commit comments