@@ -4,6 +4,7 @@ var path = require('path')
4
4
, util = require ( 'util' )
5
5
, parentDir = require ( 'find-parent-dir' )
6
6
, parseInlineShims = require ( './parse-inline-shims' )
7
+ , format = require ( 'util' ) . format
7
8
8
9
var shimsCache = { }
9
10
, byPath = { } ;
@@ -36,25 +37,34 @@ function updateCache(packageDir, resolvedShims) {
36
37
} ) ;
37
38
}
38
39
39
- function resolveDependsRelativeTo ( dir , browser , depends ) {
40
+ function resolveDependsRelativeTo ( dir , browser , depends , packDeps , messages ) {
41
+ var resolved ;
42
+
40
43
if ( ! depends ) return undefined ;
44
+
41
45
return Object . keys ( depends ) . reduce ( function ( acc , k ) {
42
46
if ( browser [ k ] ) {
43
- // exposed -- lets keep referring to it under its alias
44
47
acc [ k ] = depends [ k ] ;
48
+ messages . push ( format ( 'Found depends "%s" exposed in browser field' , k ) ) ;
45
49
} else if ( ! isPath ( k ) ) {
46
- // if it's not a path it refers to a module that is exposed in another package
47
50
acc [ k ] = depends [ k ] ;
51
+ if ( packDeps [ k ] ) {
52
+ messages . push ( format ( 'Found depends "%s" as an installed dependency of the package' , k ) ) ;
53
+ } else {
54
+ messages . push ( format ( 'WARNING, depends "%s" is not a path, nor is it exposed in the browser field, nor was it found in package dependencies.' , k ) ) ;
55
+ }
48
56
} else {
49
57
// otherwise resolve the path
50
- acc [ path . resolve ( dir , k ) ] = depends [ k ] ;
58
+ resolved = path . resolve ( dir , k ) ;
59
+ acc [ resolved ] = depends [ k ] ;
60
+ messages . push ( format ( 'Depends "%s" was resolved to be at [%s]' , k , resolved ) ) ;
51
61
}
52
62
53
63
return acc ;
54
64
} , { } )
55
65
}
56
66
57
- function resolvePaths ( packageDir , shimFileDir , browser , shims ) {
67
+ function resolvePaths ( packageDir , shimFileDir , browser , shims , packDeps , messages ) {
58
68
return Object . keys ( shims )
59
69
. reduce ( function ( acc , relPath ) {
60
70
var shim = shims [ relPath ] ;
@@ -66,56 +76,74 @@ function resolvePaths (packageDir, shimFileDir, browser, shims) {
66
76
// and it is referred to by this alias in the shims (either external or in package.json)
67
77
// i.e.: 'non-cjs': { ... } -> browser: { 'non-cjs': './vendor/non-cjs.js }
68
78
shimPath = path . resolve ( packageDir , exposed ) ;
79
+ messages . push ( format ( 'Found "%s" in browser field referencing "%s" and resolved it to "%s"' , relPath , exposed , shimPath ) ) ;
69
80
} else if ( shimFileDir ) {
70
81
// specified via relative path to shim file inside shim file
71
82
// i.e. './vendor/non-cjs': { exports: .. }
72
83
shimPath = path . resolve ( shimFileDir , relPath ) ;
84
+ messages . push ( format ( 'Resolved "%s" found in shim file to "%s"' , relPath , shimPath ) ) ;
73
85
} else {
74
86
// specified via relative path in package.json browserify-shim config
75
87
// i.e. 'browserify-shim': { './vendor/non-cjs': 'noncjs' }
76
88
shimPath = path . resolve ( packageDir , relPath ) ;
89
+ messages . push ( format ( 'Resolved "%s" found in package.json to "%s"' , relPath , shimPath ) ) ;
77
90
}
78
- var depends = resolveDependsRelativeTo ( shimFileDir || packageDir , browser , shim . depends ) ;
91
+ var depends = resolveDependsRelativeTo ( shimFileDir || packageDir , browser , shim . depends , packDeps , messages ) ;
79
92
80
93
acc [ shimPath ] = { exports : shim . exports , depends : depends } ;
81
94
return acc ;
82
95
} , { } ) ;
83
96
}
84
97
85
- function resolveFromShimFile ( packageDir , pack , shimField ) {
98
+ function resolveFromShimFile ( packageDir , pack , shimField , messages ) {
86
99
var shimFile = path . join ( packageDir , shimField )
87
100
, shimFileDir = path . dirname ( shimFile ) ;
88
101
89
102
var shims = require ( shimFile ) ;
90
103
91
- return resolvePaths ( packageDir , shimFileDir , pack . browser || { } , shims ) ;
104
+ return resolvePaths ( packageDir , shimFileDir , pack . browser || { } , shims , pack . dependencies || { } , messages ) ;
92
105
}
93
106
94
- function resolveInlineShims ( packageDir , pack , shimField ) {
107
+ function resolveInlineShims ( packageDir , pack , shimField , messages ) {
95
108
var shims = parseInlineShims ( shimField ) ;
96
-
97
- // shimFile is our package.json in this case
98
- return resolvePaths ( packageDir , null , pack . browser || { } , shims ) ;
109
+ return resolvePaths ( packageDir , null , pack . browser || { } , shims , pack . dependencies || { } , messages ) ;
99
110
}
100
111
101
- var resolve = module . exports = function resolveShims ( file , cb ) {
112
+ var resolve = module . exports = function resolveShims ( file , messages , cb ) {
102
113
parentDir ( file , 'package.json' , function ( err , packageDir ) {
103
114
if ( err ) return cb ( err ) ;
115
+
116
+ var packFile = path . join ( packageDir , 'package.json' ) ;
104
117
// we cached this before which means it was also grouped by file
105
- if ( shimsCache [ packageDir ] ) return cb ( null , byPath [ file ] ) ;
118
+ if ( shimsCache [ packageDir ] ) {
119
+ return cb ( null , {
120
+ package_json : packFile
121
+ , resolvedPreviously : true
122
+ , shim : byPath [ file ]
123
+ } ) ;
124
+ }
106
125
107
- var pack = require ( path . join ( packageDir , 'package.json' ) ) ;
126
+ try {
127
+ var pack = require ( packFile ) ;
108
128
109
- var shimField = pack [ 'browserify-shim' ] ;
110
- if ( ! shimField ) return cb ( ) ;
129
+ var shimField = pack [ 'browserify-shim' ] ;
130
+ if ( ! shimField ) return cb ( ) ;
111
131
112
- try {
113
132
var resolvedShims = typeof shimField === 'string'
114
- ? resolveFromShimFile ( packageDir , pack , shimField )
115
- : resolveInlineShims ( packageDir , pack , shimField ) ;
133
+ ? resolveFromShimFile ( packageDir , pack , shimField , messages )
134
+ : resolveInlineShims ( packageDir , pack , shimField , messages ) ;
116
135
136
+ messages . push ( { resolved : resolvedShims } ) ;
117
137
updateCache ( packageDir , resolvedShims ) ;
118
- cb ( null , byPath [ file ] ) ;
138
+
139
+ cb ( null , {
140
+ package_json : packFile
141
+ , shim : byPath [ file ]
142
+ , browser : pack . browser
143
+ , 'browserify-shim' : pack [ 'browserify-shim' ]
144
+ , dependencies : pack . dependencies
145
+ } ) ;
146
+
119
147
} catch ( err ) {
120
148
return cb ( err ) ;
121
149
}
0 commit comments