@@ -11,6 +11,7 @@ var resolve = require("resolve")
1111var postcss = require ( "postcss" )
1212var helpers = require ( "postcss-message-helpers" )
1313var hash = require ( "string-hash" )
14+ var glob = require ( "glob" )
1415
1516/**
1617 * Constants
@@ -81,14 +82,57 @@ function AtImport(options) {
8182 */
8283function parseStyles ( styles , options , cb , importedFiles , ignoredAtRules , media , hashFiles ) {
8384 var imports = [ ]
84- styles . eachAtRule ( "import" , function checkAtRule ( atRule ) { imports . push ( atRule ) } )
85+ styles . eachAtRule ( "import" , function checkAtRule ( atRule ) {
86+ if ( options . glob && glob . hasMagic ( atRule . params ) ) {
87+ imports = parseGlob ( atRule , options , imports )
88+ }
89+ else {
90+ imports . push ( atRule )
91+ }
92+ } )
8593 imports . forEach ( function ( atRule ) {
8694 helpers . try ( function transformAtImport ( ) {
8795 readAtImport ( atRule , options , cb , importedFiles , ignoredAtRules , media , hashFiles )
8896 } , atRule . source )
8997 } )
9098}
9199
100+ /**
101+ * parse glob patterns (for relative paths only)
102+ *
103+ * @param {Object } atRule
104+ * @param {Object } options
105+ * @param {Array } imports
106+ */
107+ function parseGlob ( atRule , options , imports ) {
108+ var globPattern = atRule . params . replace ( / " / g, "" )
109+ var files = [ ]
110+ var dir = options . source && options . source . input && options . source . input . file ?
111+ path . dirname ( path . resolve ( options . root , options . source . input . file ) ) :
112+ options . root
113+ options . path . forEach ( function ( p ) {
114+ p = path . resolve ( dir , p )
115+ var globbed = glob . sync ( path . join ( p , globPattern ) )
116+ globbed . forEach ( function ( file ) {
117+ file = path . relative ( p , file )
118+ files . push ( file )
119+ } ) ;
120+ } ) ;
121+
122+ files . forEach ( function ( file ) {
123+ var deglobbedAtRule = atRule . clone ( {
124+ params : "\"" + file + "\""
125+ } )
126+ if ( deglobbedAtRule . source && deglobbedAtRule . source . input && deglobbedAtRule . source . input . css ) {
127+ deglobbedAtRule . source . input . css = atRule . source . input . css . replace ( globPattern , file )
128+ }
129+ atRule . parent . insertBefore ( atRule , deglobbedAtRule )
130+ imports . push ( deglobbedAtRule )
131+ } ) ;
132+ atRule . removeSelf ( )
133+ return imports ;
134+ }
135+
92136/**
93137 * put back at the top ignored url (absolute url)
94138 *
0 commit comments