@@ -12,6 +12,11 @@ var helpers = require("postcss-message-helpers")
1212var hash = require ( "string-hash" )
1313var glob = require ( "glob" )
1414
15+ var Promise = global . Promise || require ( "es6-promise" ) . Promise
16+ var resolvedPromise = new Promise ( function ( resolvePromise ) {
17+ resolvePromise ( )
18+ } )
19+
1520/**
1621 * Constants
1722 */
@@ -72,22 +77,33 @@ function AtImport(options) {
7277
7378 var hashFiles = { }
7479
75- parseStyles (
80+ return parseStyles (
7681 result ,
7782 styles ,
7883 opts ,
79- insertRules ,
8084 importedFiles ,
8185 ignoredAtRules ,
8286 null ,
83- hashFiles
84- )
85- addIgnoredAtRulesOnTop ( styles , ignoredAtRules )
87+ hashFiles ,
88+ createProcessor ( result , options . plugins )
89+ ) . then ( function ( ) {
90+ addIgnoredAtRulesOnTop ( styles , ignoredAtRules )
8691
87- if ( typeof opts . onImport === "function" ) {
88- opts . onImport ( Object . keys ( importedFiles ) )
92+ if ( typeof opts . onImport === "function" ) {
93+ opts . onImport ( Object . keys ( importedFiles ) )
94+ }
95+ } )
96+ }
97+ }
98+
99+ function createProcessor ( result , plugins ) {
100+ if ( plugins ) {
101+ if ( ! Array . isArray ( plugins ) ) {
102+ throw new Error ( "plugins option must be an array" )
89103 }
104+ return postcss ( plugins )
90105 }
106+ return postcss ( )
91107}
92108
93109/**
@@ -100,11 +116,11 @@ function parseStyles(
100116 result ,
101117 styles ,
102118 options ,
103- cb ,
104119 importedFiles ,
105120 ignoredAtRules ,
106121 media ,
107- hashFiles
122+ hashFiles ,
123+ processor
108124) {
109125 var imports = [ ]
110126 styles . eachAtRule ( "import" , function checkAtRule ( atRule ) {
@@ -118,20 +134,20 @@ function parseStyles(
118134 imports . push ( atRule )
119135 }
120136 } )
121- imports . forEach ( function ( atRule ) {
122- helpers . try ( function transformAtImport ( ) {
123- readAtImport (
137+ return Promise . all ( imports . map ( function ( atRule ) {
138+ return helpers . try ( function transformAtImport ( ) {
139+ return readAtImport (
124140 result ,
125141 atRule ,
126142 options ,
127- cb ,
128143 importedFiles ,
129144 ignoredAtRules ,
130145 media ,
131- hashFiles
146+ hashFiles ,
147+ processor
132148 )
133149 } , atRule . source )
134- } )
150+ } ) )
135151}
136152
137153/**
@@ -220,11 +236,11 @@ function readAtImport(
220236 result ,
221237 atRule ,
222238 options ,
223- cb ,
224239 importedFiles ,
225240 ignoredAtRules ,
226241 media ,
227- hashFiles
242+ hashFiles ,
243+ processor
228244) {
229245 // parse-import module parse entire line
230246 // @todo extract what can be interesting from this one
@@ -246,7 +262,7 @@ function readAtImport(
246262 // detach
247263 detach ( atRule )
248264
249- return
265+ return resolvedPromise
250266 }
251267
252268 addInputToPath ( options )
@@ -264,7 +280,7 @@ function readAtImport(
264280 importedFiles [ resolvedFilename ] [ media ]
265281 ) {
266282 detach ( atRule )
267- return
283+ return resolvedPromise
268284 }
269285
270286 // save imported files to skip them next time
@@ -273,17 +289,17 @@ function readAtImport(
273289 }
274290 importedFiles [ resolvedFilename ] [ media ] = true
275291
276- readImportedContent (
292+ return readImportedContent (
277293 result ,
278294 atRule ,
279295 parsedAtImport ,
280296 clone ( options ) ,
281297 resolvedFilename ,
282- cb ,
283298 importedFiles ,
284299 ignoredAtRules ,
285300 media ,
286- hashFiles
301+ hashFiles ,
302+ processor
287303 )
288304}
289305
@@ -294,19 +310,18 @@ function readAtImport(
294310 * @param {Object } parsedAtImport
295311 * @param {Object } options
296312 * @param {String } resolvedFilename
297- * @param {Function } cb
298313 */
299314function readImportedContent (
300315 result ,
301316 atRule ,
302317 parsedAtImport ,
303318 options ,
304319 resolvedFilename ,
305- cb ,
306320 importedFiles ,
307321 ignoredAtRules ,
308322 media ,
309- hashFiles
323+ hashFiles ,
324+ processor
310325) {
311326 // add directory containing the @imported file in the paths
312327 // to allow local import from this file
@@ -328,7 +343,7 @@ function readImportedContent(
328343 if ( fileContent . trim ( ) === "" ) {
329344 result . warn ( resolvedFilename + " is empty" , { node : atRule } )
330345 detach ( atRule )
331- return
346+ return resolvedPromise
332347 }
333348
334349 // skip files wich only contain @import rules
@@ -339,7 +354,7 @@ function readImportedContent(
339354 // skip files already imported at the same scope and same hash
340355 if ( hashFiles [ fileContentHash ] && hashFiles [ fileContentHash ] [ media ] ) {
341356 detach ( atRule )
342- return
357+ return resolvedPromise
343358 }
344359
345360 // save hash files to skip them next time
@@ -352,18 +367,27 @@ function readImportedContent(
352367 var newStyles = postcss . parse ( fileContent , options )
353368
354369 // recursion: import @import from imported file
355- parseStyles (
370+ return parseStyles (
356371 result ,
357372 newStyles ,
358373 options ,
359- cb ,
360374 importedFiles ,
361375 ignoredAtRules ,
362376 parsedAtImport . media ,
363- hashFiles
377+ hashFiles ,
378+ processor
364379 )
365-
366- cb ( atRule , parsedAtImport , newStyles , resolvedFilename )
380+ . then ( function ( ) {
381+ return processor . process ( newStyles )
382+ . then ( function ( newResult ) {
383+ newResult . warnings ( ) . forEach ( function ( message ) {
384+ result . warn ( message )
385+ } )
386+ } )
387+ } )
388+ . then ( function ( ) {
389+ insertRules ( atRule , parsedAtImport , newStyles )
390+ } )
367391}
368392
369393/**
0 commit comments