@@ -73,16 +73,14 @@ function AtImport(options) {
7373 }
7474 }
7575
76- var parsedStylesResult = parseStyles (
76+ return parseStyles (
7777 result ,
7878 styles ,
7979 opts ,
8080 state ,
8181 [ ] ,
8282 createProcessor ( result , options . plugins )
83- )
84-
85- function onParseEnd ( ignored ) {
83+ ) . then ( function ( ignored ) {
8684 addIgnoredAtRulesOnTop ( styles , ignored )
8785
8886 if (
@@ -96,9 +94,7 @@ function AtImport(options) {
9694 if ( typeof opts . onImport === "function" ) {
9795 opts . onImport ( Object . keys ( state . importedFiles ) )
9896 }
99- }
100-
101- return parsedStylesResult . then ( onParseEnd )
97+ } )
10298 }
10399}
104100
@@ -140,7 +136,6 @@ function parseStyles(
140136 var importResults = imports . map ( function ( instance ) {
141137 return readAtImport (
142138 result ,
143- instance . node ,
144139 instance ,
145140 options ,
146141 state ,
@@ -152,14 +147,8 @@ function parseStyles(
152147 return Promise . all ( importResults ) . then ( function ( result ) {
153148 // Flatten ignored instances
154149 return result . reduce ( function ( ignored , item ) {
155- if ( Array . isArray ( item ) ) {
156- item = item . filter ( function ( instance ) {
157- return instance
158- } )
159- ignored = ignored . concat ( item )
160- }
161- else if ( item ) {
162- ignored . push ( item )
150+ if ( item ) {
151+ return ignored . concat ( item )
163152 }
164153 return ignored
165154 } , [ ] )
@@ -240,25 +229,23 @@ function addIgnoredAtRulesOnTop(styles, ignoredAtRules) {
240229 */
241230function readAtImport (
242231 result ,
243- atRule ,
244232 parsedAtImport ,
245233 options ,
246234 state ,
247235 media ,
248236 processor
249237) {
238+ var atRule = parsedAtImport . node
250239 // adjust media according to current scope
251240 media = resolveMedia ( media , parsedAtImport . media )
252241
253242 // just update protocol base uri (protocol://url) or protocol-relative
254243 // (//url) if media needed
255244 if ( parsedAtImport . uri . match ( / ^ (?: [ a - z ] + : ) ? \/ \/ / i) ) {
256245 parsedAtImport . media = media
257-
258246 // detach
259247 atRule . remove ( )
260-
261- return Promise . resolve ( parsedAtImport )
248+ return parsedAtImport
262249 }
263250
264251 var dir = atRule . source && atRule . source . input && atRule . source . input . file
@@ -272,34 +259,19 @@ function readAtImport(
272259 return options . resolve ( parsedAtImport . uri , dir , options )
273260 }
274261 return resolveId ( parsedAtImport . uri , dir , options . path )
275- } ) . then ( function ( resolvedFilename ) {
276- if ( options . skipDuplicates ) {
277- // skip files already imported at the same scope
278- if (
279- state . importedFiles [ resolvedFilename ] &&
280- state . importedFiles [ resolvedFilename ] [ media ]
281- ) {
282- atRule . remove ( )
283- return Promise . resolve ( )
284- }
285-
286- // save imported files to skip them next time
287- if ( ! state . importedFiles [ resolvedFilename ] ) {
288- state . importedFiles [ resolvedFilename ] = { }
289- }
290- state . importedFiles [ resolvedFilename ] [ media ] = true
291- }
292-
262+ } ) . then ( function ( resolved ) {
263+ parsedAtImport . file = resolved
293264 return readImportedContent (
294265 result ,
295- atRule ,
296266 parsedAtImport ,
297267 assign ( { } , options ) ,
298- resolvedFilename ,
299268 state ,
300269 media ,
301270 processor
302271 )
272+ } ) . then ( function ( ignored ) {
273+ compoundInstance ( parsedAtImport )
274+ return ignored
303275 } ) . catch ( function ( err ) {
304276 result . warn ( err . message , { node : atRule } )
305277 } )
@@ -315,14 +287,30 @@ function readAtImport(
315287 */
316288function readImportedContent (
317289 result ,
318- atRule ,
319290 parsedAtImport ,
320291 options ,
321- resolvedFilename ,
322292 state ,
323293 media ,
324294 processor
325295) {
296+ var resolvedFilename = parsedAtImport . file
297+ var atRule = parsedAtImport . node
298+ if ( options . skipDuplicates ) {
299+ // skip files already imported at the same scope
300+ if (
301+ state . importedFiles [ resolvedFilename ] &&
302+ state . importedFiles [ resolvedFilename ] [ media ]
303+ ) {
304+ return
305+ }
306+
307+ // save imported files to skip them next time
308+ if ( ! state . importedFiles [ resolvedFilename ] ) {
309+ state . importedFiles [ resolvedFilename ] = { }
310+ }
311+ state . importedFiles [ resolvedFilename ] [ media ] = true
312+ }
313+
326314 // add directory containing the @imported file in the paths
327315 // to allow local import from this file
328316 var dirname = path . dirname ( resolvedFilename )
@@ -342,17 +330,15 @@ function readImportedContent(
342330
343331 if ( fileContent . trim ( ) === "" ) {
344332 result . warn ( resolvedFilename + " is empty" , { node : atRule } )
345- atRule . remove ( )
346- return Promise . resolve ( )
333+ return
347334 }
348335
349336 // skip previous imported files not containing @import rules
350337 if (
351338 state . hashFiles [ fileContent ] &&
352339 state . hashFiles [ fileContent ] [ media ]
353340 ) {
354- atRule . remove ( )
355- return Promise . resolve ( )
341+ return
356342 }
357343
358344 var newStyles = postcss . parse ( fileContent , options )
@@ -370,27 +356,19 @@ function readImportedContent(
370356 }
371357
372358 // recursion: import @import from imported file
373- var parsedResult = parseStyles (
359+ return parseStyles (
374360 result ,
375361 newStyles ,
376362 options ,
377363 state ,
378364 parsedAtImport . media ,
379365 processor
380- )
381-
382- var instances
383-
384- return parsedResult . then ( function ( result ) {
385- instances = result
386- return processor . process ( newStyles )
387- } )
388- . then ( function ( newResult ) {
389- result . messages = result . messages . concat ( newResult . messages )
390- } )
391- . then ( function ( ) {
392- insertRules ( atRule , parsedAtImport , newStyles )
393- return instances
366+ ) . then ( function ( ignored ) {
367+ return processor . process ( newStyles ) . then ( function ( newResult ) {
368+ result . messages = result . messages . concat ( newResult . messages )
369+ parsedAtImport . styles = newStyles
370+ return ignored
371+ } )
394372 } )
395373}
396374
@@ -401,32 +379,38 @@ function readImportedContent(
401379 * @param {Object } parsedAtImport
402380 * @param {Object } newStyles
403381 */
404- function insertRules ( atRule , parsedAtImport , newStyles ) {
405- var newNodes = newStyles . nodes
382+ function compoundInstance ( instance ) {
383+ if (
384+ ! instance . styles ||
385+ ! instance . styles . nodes ||
386+ ! instance . styles . nodes . length
387+ ) {
388+ instance . node . remove ( )
389+ return
390+ }
391+
392+ var nodes = instance . styles . nodes
406393
407394 // save styles
408- newNodes . forEach ( function ( node ) {
395+ nodes . forEach ( function ( node ) {
409396 node . parent = undefined
410397 } )
411398
412399 // wrap rules if the @import have a media query
413- if ( parsedAtImport . media . length && newNodes && newNodes . length ) {
400+ if ( instance . media . length ) {
414401 // better output
415- newNodes [ 0 ] . raws . before = newNodes [ 0 ] . raws . before || "\n"
402+ nodes [ 0 ] . raws . before = nodes [ 0 ] . raws . before || "\n"
416403
417- // wrap new rules with media (media query)
418- var wrapper = postcss . atRule ( {
404+ // wrap new rules with media query
405+ nodes = postcss . atRule ( {
419406 name : "media" ,
420- params : parsedAtImport . media . join ( ", " ) ,
421- source : atRule . source ,
422- } )
423-
424- // move nodes
425- newNodes = wrapper . append ( newNodes )
407+ params : instance . media . join ( ", " ) ,
408+ source : instance . node . source ,
409+ } ) . append ( nodes )
426410 }
427411
428412 // replace atRule by imported nodes
429- atRule . replaceWith ( newNodes )
413+ instance . node . replaceWith ( nodes )
430414}
431415
432416/**
0 commit comments