@@ -254,6 +254,29 @@ function getPackageName(id) {
254
254
}
255
255
return id . substring ( 0 , index )
256
256
}
257
+
258
+ /**
259
+ *
260
+ * @param {string } s
261
+ * @param {string } text
262
+ * @returns {undefined | string }
263
+ */
264
+ function isJustAPackageAndHasMainField ( s , text ) {
265
+ if ( s . indexOf ( '/' ) < 0 ) {
266
+ return
267
+ } else {
268
+ var mainField ;
269
+ try {
270
+ mainField = JSON . parse ( text ) . main
271
+ } catch ( _ ) {
272
+ }
273
+ if ( mainField === undefined ) {
274
+ return
275
+ } else {
276
+ return mainField
277
+ }
278
+ }
279
+ }
257
280
function getPackageJsPromise ( id , parent ) {
258
281
var idNodeModulesPrefix = './node_modules/' + id
259
282
var link = getPathWithJsSuffix ( idNodeModulesPrefix , parent )
@@ -269,6 +292,19 @@ function getPackageJsPromise(id, parent) {
269
292
. then (
270
293
function ( text ) {
271
294
if ( text !== false ) {
295
+ var mainField ;
296
+ if ( ( mainField = isJustAPackageAndHasMainField ( id , text ) ) !== undefined ) {
297
+ var packageLink = BsGetPath ( addSuffixJsIfNot ( `./node_modules/${ id } /${ mainField } ` ) , parent )
298
+ return cachedFetch ( packageLink )
299
+ . then ( function ( text ) {
300
+ if ( text !== false ) {
301
+ return { text, link : packageLink }
302
+ } else {
303
+ return getParentModulePromise ( id , parent )
304
+ }
305
+ } )
306
+
307
+ } else {
272
308
// package indeed exist
273
309
return cachedFetch ( link ) . then ( function ( text ) {
274
310
if ( text !== false ) {
@@ -289,6 +325,7 @@ function getPackageJsPromise(id, parent) {
289
325
}
290
326
} )
291
327
}
328
+ }
292
329
else {
293
330
return getParentModulePromise ( id , parent )
294
331
}
@@ -317,7 +354,19 @@ function getModulePromise(id, parent) {
317
354
function ( text ) {
318
355
if ( text !== false ) {
319
356
return { text, link }
320
- } else {
357
+ } else if ( ! id . endsWith ( '.js' ) ) {
358
+ // could be "./dir"
359
+ var newLink = getPathWithJsSuffix ( id + "/index.js" , parent )
360
+ return cachedFetch ( newLink )
361
+ . then ( function ( text ) {
362
+ if ( text !== false ) {
363
+ return { text, link : newLink }
364
+ } else {
365
+ throw new Error ( ` ${ id } : ${ parent } could not be resolved` )
366
+ }
367
+ } )
368
+ }
369
+ else {
321
370
throw new Error ( ` ${ id } : ${ parent } could not be resolved` )
322
371
}
323
372
}
0 commit comments