@@ -50,10 +50,62 @@ function resolve(context, identifier, options, type, callback) {
5050}
5151
5252function doResolve ( context , identifier , options , type , callback ) {
53- if ( ! callback ) {
54- callback = options ;
55- options = { } ;
53+ var identifiers = identifier . replace ( / ^ ! | ! $ / g, "" ) . replace ( / ! ! / g, "!" ) . split ( / ! / g) ;
54+ var resource = identifiers . pop ( ) ;
55+ resolve ( context , resource , options , type , function ( err , resource ) {
56+ if ( err ) return callback ( err ) ;
57+ if ( identifier . indexOf ( "!" ) === - 1 ) {
58+ for ( var i = 0 ; i < options . loaders . length ; i ++ ) {
59+ var line = options . loaders [ i ] ;
60+ if ( line . test . test ( resource ) ) {
61+ Array . prototype . push . apply ( identifiers , line . loader . split ( / ! / g) ) ;
62+ break ;
63+ }
64+ }
65+ }
66+ resolveLoaders ( context , identifiers , options , function ( err , identifiers ) {
67+ identifiers . push ( resource ) ;
68+ var intermediateResult = identifiers . join ( "!" ) ;
69+ var postprocessors = options . postprocess [ type ] . slice ( 0 ) ;
70+ postprocessors . push ( function ( result ) {
71+ callback ( null , result ) ;
72+ } ) ;
73+ ( function next ( err , result ) {
74+ if ( err )
75+ return callback ( new Error ( "File \"" + intermediateResult + "\" is blocked by postprocessors: " + err ) ) ;
76+ postprocessors . shift ( ) ( result , next ) ;
77+ } ) ( null , intermediateResult ) ;
78+ } ) ;
79+ } ) ;
80+ }
81+
82+ function resolveLoaders ( context , identifiers , options , callback ) {
83+ var errors = [ ] ;
84+ var count = identifiers . length ;
85+ function endOne ( ) {
86+ count -- ;
87+ if ( count === 0 ) {
88+ if ( errors . length > 0 ) {
89+ callback ( new Error ( errors . join ( "\n" ) ) ) ;
90+ return ;
91+ }
92+ callback ( null , identifiers ) ;
93+ }
5694 }
95+ if ( count == 0 ) endOne ( count ++ ) ;
96+ identifiers . forEach ( function ( ident , index ) {
97+ resolve ( context , ident , options , "loader" , function ( err , filename ) {
98+ if ( err ) {
99+ errors . push ( err ) ;
100+ } else {
101+ identifiers [ index ] = filename ;
102+ }
103+ endOne ( )
104+ } ) ;
105+ } ) ;
106+ }
107+
108+ function setupDefaultOptions ( options ) {
57109 if ( ! options )
58110 options = { } ;
59111 if ( ! options . extensions )
@@ -76,53 +128,7 @@ function doResolve(context, identifier, options, type, callback) {
76128 options . postprocess . normal = [ ] ;
77129 if ( ! options . postprocess . context )
78130 options . postprocess . context = [ ] ;
79- var identifiers = identifier . replace ( / ^ ! | ! $ / g, "" ) . replace ( / ! ! / g, "!" ) . split ( / ! / g) ;
80- var resource = identifiers . pop ( ) ;
81- resolve ( context , resource , options , type , function ( err , resource ) {
82- if ( err ) return callback ( err ) ;
83- if ( identifier . indexOf ( "!" ) === - 1 ) {
84- for ( var i = 0 ; i < options . loaders . length ; i ++ ) {
85- var line = options . loaders [ i ] ;
86- if ( line . test . test ( resource ) ) {
87- Array . prototype . push . apply ( identifiers , line . loader . split ( / ! / g) ) ;
88- break ;
89- }
90- }
91- }
92- var errors = [ ] ;
93- var count = identifiers . length ;
94- function endOne ( ) {
95- count -- ;
96- if ( count === 0 ) {
97- if ( errors . length > 0 ) {
98- callback ( new Error ( errors . join ( "\n" ) ) ) ;
99- return ;
100- }
101- identifiers . push ( resource ) ;
102- var intermediateResult = identifiers . join ( "!" ) ;
103- var postprocessors = options . postprocess [ type ] . slice ( 0 ) ;
104- postprocessors . push ( function ( result ) {
105- callback ( null , result ) ;
106- } ) ;
107- ( function next ( err , result ) {
108- if ( err )
109- return callback ( new Error ( "File \"" + intermediateResult + "\" is blocked by postprocessors: " + err ) ) ;
110- postprocessors . shift ( ) ( result , next ) ;
111- } ) ( null , intermediateResult ) ;
112- }
113- }
114- if ( count == 0 ) endOne ( count ++ ) ;
115- identifiers . forEach ( function ( ident , index ) {
116- resolve ( context , ident , options , "loader" , function ( err , filename ) {
117- if ( err ) {
118- errors . push ( err ) ;
119- } else {
120- identifiers [ index ] = filename ;
121- }
122- endOne ( )
123- } ) ;
124- } ) ;
125- } ) ;
131+ return options ;
126132}
127133
128134/**
@@ -133,13 +139,37 @@ function doResolve(context, identifier, options, type, callback) {
133139 * callback: function(err, absoluteFilename)
134140 */
135141module . exports = function ( context , identifier , options , callback ) {
142+ if ( ! callback ) {
143+ callback = options ;
144+ options = { } ;
145+ }
146+ options = setupDefaultOptions ( options ) ;
136147 return doResolve ( context , identifier , options , "normal" , callback ) ;
137148}
138149
139150module . exports . context = function ( context , identifier , options , callback ) {
151+ if ( ! callback ) {
152+ callback = options ;
153+ options = { } ;
154+ }
155+ options = setupDefaultOptions ( options ) ;
140156 return doResolve ( context , identifier , options , "context" , callback ) ;
141157}
142158
159+ /**
160+ * callback: function(err, absoluteFilenamesArray)
161+ */
162+ module . exports . loaders = function ( context , identifier , options , callback ) {
163+ if ( ! callback ) {
164+ callback = options ;
165+ options = { } ;
166+ }
167+ options = setupDefaultOptions ( options ) ;
168+ var identifiers = identifier . replace ( / ^ ! | ! $ / g, "" ) . replace ( / ! ! / g, "!" ) . split ( / ! / g) ;
169+ if ( identifiers . length == 1 && identifiers [ 0 ] == "" ) return callback ( null , [ ] ) ;
170+ return resolveLoaders ( context , identifiers , options , callback ) ;
171+ }
172+
143173
144174function split ( a ) {
145175 return a . split ( / [ \/ \\ ] / g) ;
0 commit comments