@@ -25,7 +25,7 @@ function nodeModulesPaths (start, cb) {
25
25
return dirs ;
26
26
}
27
27
28
- function find_shims_in_package ( pkgJson , cur_path , shims ) {
28
+ function find_shims_in_package ( pkgJson , cur_path , shims , browser ) {
29
29
try {
30
30
var info = JSON . parse ( pkgJson ) ;
31
31
}
@@ -34,32 +34,28 @@ function find_shims_in_package(pkgJson, cur_path, shims) {
34
34
throw err ;
35
35
}
36
36
37
- // support legacy browserify field for easier migration from legacy
38
- // many packages used this field historically
39
- if ( typeof info . browserify === 'string' && ! info . browser ) {
40
- info . browser = info . browserify ;
41
- }
37
+ replacements = getReplacements ( info , browser ) ;
42
38
43
39
// no replacements, skip shims
44
- if ( ! info . browser ) {
40
+ if ( ! replacements ) {
45
41
return ;
46
42
}
47
43
48
- // if browser field is a string
44
+ // if browser mapping is a string
49
45
// then it just replaces the main entry point
50
- if ( typeof info . browser === 'string' ) {
46
+ if ( typeof replacements === 'string' ) {
51
47
var key = path . resolve ( cur_path , info . main || 'index.js' ) ;
52
- shims [ key ] = path . resolve ( cur_path , info . browser ) ;
48
+ shims [ key ] = path . resolve ( cur_path , replacements ) ;
53
49
return ;
54
50
}
55
51
56
52
// http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
57
- Object . keys ( info . browser ) . forEach ( function ( key ) {
58
- if ( info . browser [ key ] === false ) {
53
+ Object . keys ( replacements ) . forEach ( function ( key ) {
54
+ if ( replacements [ key ] === false ) {
59
55
return shims [ key ] = __dirname + '/empty.js' ;
60
56
}
61
57
62
- var val = info . browser [ key ] ;
58
+ var val = replacements [ key ] ;
63
59
64
60
// if target is a relative path, then resolve
65
61
// otherwise we assume target is a module
@@ -79,7 +75,7 @@ function find_shims_in_package(pkgJson, cur_path, shims) {
79
75
80
76
// paths is mutated
81
77
// load shims from first package.json file found
82
- function load_shims ( paths , cb ) {
78
+ function load_shims ( paths , browser , cb ) {
83
79
// identify if our file should be replaced per the browser field
84
80
// original filename|id -> replacement
85
81
var shims = { } ;
@@ -103,7 +99,7 @@ function load_shims(paths, cb) {
103
99
return cb ( err ) ;
104
100
}
105
101
try {
106
- find_shims_in_package ( data , cur_path , shims ) ;
102
+ find_shims_in_package ( data , cur_path , shims , browser ) ;
107
103
return cb ( null , shims ) ;
108
104
}
109
105
catch ( err ) {
@@ -115,7 +111,7 @@ function load_shims(paths, cb) {
115
111
116
112
// paths is mutated
117
113
// synchronously load shims from first package.json file found
118
- function load_shims_sync ( paths ) {
114
+ function load_shims_sync ( paths , browser ) {
119
115
// identify if our file should be replaced per the browser field
120
116
// original filename|id -> replacement
121
117
var shims = { } ;
@@ -126,7 +122,7 @@ function load_shims_sync(paths) {
126
122
127
123
try {
128
124
var data = fs . readFileSync ( pkg_path , 'utf8' ) ;
129
- find_shims_in_package ( data , cur_path , shims ) ;
125
+ find_shims_in_package ( data , cur_path , shims , browser ) ;
130
126
return shims ;
131
127
}
132
128
catch ( err ) {
@@ -144,34 +140,34 @@ function load_shims_sync(paths) {
144
140
145
141
function build_resolve_opts ( opts , base ) {
146
142
var packageFilter = opts . packageFilter ;
143
+ var browser = normalizeBrowserFieldName ( opts . browser )
147
144
148
145
opts . basedir = base ;
149
146
opts . packageFilter = function ( info , pkgdir ) {
150
147
if ( packageFilter ) info = packageFilter ( info , pkgdir ) ;
151
148
152
- // support legacy browserify field
153
- if ( typeof info . browserify === 'string' && ! info . browser ) {
154
- info . browser = info . browserify ;
155
- }
149
+ var replacements = getReplacements ( info , browser ) ;
156
150
157
151
// no browser field, keep info unchanged
158
- if ( ! info . browser ) {
152
+ if ( ! replacements ) {
159
153
return info ;
160
154
}
161
155
156
+ info [ browser ] = replacements ;
157
+
162
158
// replace main
163
- if ( typeof info . browser === 'string' ) {
164
- info . main = info . browser ;
159
+ if ( typeof replacements === 'string' ) {
160
+ info . main = replacements ;
165
161
return info ;
166
162
}
167
163
168
- var replace_main = info . browser [ info . main || './index.js' ] ||
169
- info . browser [ './' + info . main || './index.js' ] ;
164
+ var replace_main = replacements [ info . main || './index.js' ] ||
165
+ replacements [ './' + info . main || './index.js' ] ;
170
166
171
167
info . main = replace_main || info . main ;
172
168
return info ;
173
169
} ;
174
-
170
+
175
171
var pathFilter = opts . pathFilter ;
176
172
opts . pathFilter = function ( info , path , relativePath ) {
177
173
if ( relativePath [ 0 ] != '.' ) {
@@ -187,7 +183,7 @@ function build_resolve_opts(opts, base) {
187
183
if ( ! info . browser ) {
188
184
return ;
189
185
}
190
-
186
+
191
187
if ( typeof info . browser ) {
192
188
mappedPath = info . browser [ relativePath ] ;
193
189
if ( ! mappedPath && ( relativePath . lastIndexOf ( ".js" ) === relativePath . length - 3 ) ) {
@@ -210,11 +206,11 @@ function resolve(id, opts, cb) {
210
206
opts = opts || { } ;
211
207
212
208
var base = path . dirname ( opts . filename ) ;
213
-
209
+
214
210
if ( opts . basedir ) {
215
211
base = opts . basedir ;
216
212
}
217
-
213
+
218
214
var paths = nodeModulesPaths ( base ) ;
219
215
220
216
if ( opts . paths ) {
@@ -226,7 +222,7 @@ function resolve(id, opts, cb) {
226
222
} ) ;
227
223
228
224
// we must always load shims because the browser field could shim out a module
229
- load_shims ( paths , function ( err , shims ) {
225
+ load_shims ( paths , opts . browser , function ( err , shims ) {
230
226
if ( err ) {
231
227
return cb ( err ) ;
232
228
}
@@ -269,11 +265,11 @@ resolve.sync = function (id, opts) {
269
265
270
266
opts = opts || { } ;
271
267
var base = path . dirname ( opts . filename ) ;
272
-
268
+
273
269
if ( opts . basedir ) {
274
270
base = opts . basedir ;
275
271
}
276
-
272
+
277
273
var paths = nodeModulesPaths ( base ) ;
278
274
279
275
if ( opts . paths ) {
@@ -285,7 +281,7 @@ resolve.sync = function (id, opts) {
285
281
} ) ;
286
282
287
283
// we must always load shims because the browser field could shim out a module
288
- var shims = load_shims_sync ( paths ) ;
284
+ var shims = load_shims_sync ( paths , opts . browser ) ;
289
285
290
286
if ( shims [ id ] ) {
291
287
// if the shim was is an absolute path, it was fully resolved
@@ -310,5 +306,22 @@ resolve.sync = function (id, opts) {
310
306
return ( shims ) ? shims [ full ] || full : full ;
311
307
} ;
312
308
309
+ function normalizeBrowserFieldName ( browser ) {
310
+ return browser || 'browser' ;
311
+ }
312
+
313
+ function getReplacements ( info , browser ) {
314
+ browser = normalizeBrowserFieldName ( browser ) ;
315
+ var replacements = info [ browser ] ;
316
+
317
+ // support legacy browserify field for easier migration from legacy
318
+ // many packages used this field historically
319
+ if ( typeof info . browserify === 'string' && ! replacements ) {
320
+ replacements = info . browserify ;
321
+ }
322
+
323
+ return replacements ;
324
+ }
325
+
313
326
module . exports = resolve ;
314
327
0 commit comments