@@ -7,48 +7,64 @@ var moduleDirectories = [
77 "node_modules" ,
88]
99
10- module . exports = function ( id , base , paths ) {
11- try {
12- var resolveOpts = {
13- basedir : base ,
14- moduleDirectory : moduleDirectories ,
15- paths : paths ,
16- extensions : [ ".css" ] ,
17- packageFilter : function processPackage ( pkg ) {
18- pkg . main = pkg . style || "index.css"
19- return pkg
20- } ,
21- }
22- var file
23- try {
24- file = resolve . sync ( id , resolveOpts )
25- }
26- catch ( e ) {
27- // fix to try relative files on windows with "./"
28- // if it's look like it doesn't start with a relative path already
29- // if (id.match(/^\.\.?/)) {throw e}
30- try {
31- file = resolve . sync ( "./" + id , resolveOpts )
32- }
33- catch ( err ) {
34- // LAST HOPE
35- if ( ! paths . some ( function ( dir ) {
36- file = path . join ( dir , id )
37- return fs . existsSync ( file )
38- } ) ) {
39- throw err
10+ function isFilePromise ( path ) {
11+ return new Promise ( function ( resolve , reject ) {
12+ fs . stat ( path , function ( err , stat ) {
13+ if ( err ) {
14+ if ( err . code === "ENOENT" ) {
15+ return resolve ( false )
4016 }
17+ return reject ( err )
4118 }
42- }
19+ resolve ( stat . isFile ( ) || stat . isFIFO ( ) )
20+ } )
21+ } )
22+ }
4323
44- return path . normalize ( file )
45- }
46- catch ( e ) {
47- throw new Error ( [
48- "Failed to find '" + id + "'" ,
49- "in [ " ,
50- " " + paths . join ( ",\n " ) ,
51- "]" ,
52- ] . join ( "\n " ) )
24+ function resolvePromise ( id , opts ) {
25+ return new Promise ( function ( res , rej ) {
26+ resolve ( id , opts , function ( err , path ) {
27+ if ( err ) {
28+ return rej ( err )
29+ }
30+ res ( path )
31+ } )
32+ } )
33+ }
34+
35+ module . exports = function ( id , base , paths ) {
36+ var resolveOpts = {
37+ basedir : base ,
38+ moduleDirectory : moduleDirectories ,
39+ paths : paths ,
40+ extensions : [ ".css" ] ,
41+ packageFilter : function processPackage ( pkg ) {
42+ pkg . main = pkg . style || "index.css"
43+ return pkg
44+ } ,
5345 }
46+ return resolvePromise ( id , resolveOpts ) . catch ( function ( ) {
47+ // fix to try relative files on windows with "./"
48+ // if it's look like it doesn't start with a relative path already
49+ // if (id.match(/^\.\.?/)) {throw e}
50+ return resolvePromise ( "./" + id , resolveOpts )
51+ } ) . catch ( function ( ) {
52+ // LAST HOPE
53+ return Promise . all ( paths . map ( function ( p ) {
54+ return isFilePromise ( path . resolve ( p , id ) )
55+ } ) ) . then ( function ( results ) {
56+ for ( var i = 0 ; i < results . length ; i += 1 ) {
57+ if ( results [ i ] ) {
58+ return path . resolve ( paths [ i ] , id )
59+ }
60+ }
61+
62+ throw new Error ( [
63+ "Failed to find '" + id + "'" ,
64+ "in [ " ,
65+ " " + paths . join ( ",\n " ) ,
66+ "]" ,
67+ ] . join ( "\n " ) )
68+ } )
69+ } )
5470}
0 commit comments