@@ -29,6 +29,7 @@ const node_modules = 'node_modules'
29
29
const packagejson = 'package.json'
30
30
const specruntime = 'node'
31
31
const specdefault = 'default'
32
+ const specbrowser = 'browser'
32
33
const specimport = 'import'
33
34
const specdot = '.'
34
35
const isobj = o => o && typeof o === 'object'
@@ -195,7 +196,7 @@ const esmparselist = (list, spec, specifier, key = list[0]) => {
195
196
|| esmparselist ( list . slice ( 1 ) , spec , specifier )
196
197
}
197
198
198
- const esmparse = ( spec , specifier ) => {
199
+ const esmparse = ( spec , specifier , opts = { } ) => {
199
200
let indexval = false
200
201
201
202
if ( typeof spec === 'string' )
@@ -209,7 +210,7 @@ const esmparse = (spec, specifier) => {
209
210
// }, "./index.cjs" ]
210
211
// }
211
212
indexval = spec
212
- . reduce ( ( p , elem ) => p || esmparse ( elem , specifier ) , null )
213
+ . reduce ( ( p , elem ) => p || esmparse ( elem , specifier , opts ) , null )
213
214
}
214
215
215
216
if ( ! indexval && isobj ( spec ) ) {
@@ -226,21 +227,23 @@ const esmparse = (spec, specifier) => {
226
227
// "require": "./feature-node.cjs"
227
228
// }
228
229
// }
229
- if ( ! indexval && spec [ specruntime ] )
230
- indexval = esmparse ( spec [ specruntime ] , specifier )
231
- if ( ! indexval && spec [ specdefault ] )
232
- indexval = esmparse ( spec [ specdefault ] , specifier )
230
+ if ( ! indexval )
231
+ indexval = ( opts . specprioritylist || [ specruntime , specdefault ] )
232
+ . reduce ( ( prev , specname ) => (
233
+ prev || esmparse ( spec [ specname ] , specifier , opts )
234
+ ) , false )
235
+
233
236
if ( ! indexval && spec [ specifier ] )
234
- indexval = esmparse ( spec [ specifier ] , specifier )
237
+ indexval = esmparse ( spec [ specifier ] , specifier , opts )
235
238
236
239
// "exports": "./lib/index.js",
237
240
// "exports": { "import": "./lib/index.js" },
238
241
// "exports": { ".": "./lib/index.js" },
239
242
// "exports": { ".": { "import": "./lib/index.js" } }
240
243
if ( ! indexval && spec [ specdot ] )
241
244
indexval = typeof spec [ specdot ] === 'string'
242
- ? specifier === specimport && esmparse ( spec [ specdot ] , specifier )
243
- : esmparse ( spec [ specdot ] , specifier )
245
+ ? specifier === specimport && esmparse ( spec [ specdot ] , specifier , opts )
246
+ : esmparse ( spec [ specdot ] , specifier , opts )
244
247
245
248
// "exports": {
246
249
// ".": "./lib/index.test.js",
@@ -255,8 +258,8 @@ const esmparse = (spec, specifier) => {
255
258
}
256
259
257
260
const gettargetindex = ( packagejson , opts ) => {
258
- let moduleobj = opts && opts . ismodule && packagejson . module ,
259
- browserobj = moduleobj || opts && opts . browser && packagejson . browser ,
261
+ let moduleobj = opts && opts . isimport && packagejson . module ,
262
+ browserobj = moduleobj || opts && opts . isbrowser && packagejson . browser ,
260
263
esmexportsobj = packagejson . exports ,
261
264
indexprop ,
262
265
indexval
@@ -272,7 +275,7 @@ const gettargetindex = (packagejson, opts) => {
272
275
}
273
276
274
277
if ( esmexportsobj ) {
275
- indexval = esmparse ( esmexportsobj , specimport )
278
+ indexval = esmparse ( esmexportsobj , specimport , opts )
276
279
}
277
280
278
281
return indexval
@@ -345,9 +348,9 @@ const getasfileordir = (moduleId, parent, opts) => {
345
348
// }
346
349
// }
347
350
// }
348
- const esmparseimport = ( targetpath , specifier , pjson ) => {
351
+ const esmparseimport = ( targetpath , specifier , pjson , opts ) => {
349
352
const pjsonimports = pjson && pjson . imports
350
- const firstmatch = esmparse ( pjsonimports , specifier )
353
+ const firstmatch = esmparse ( pjsonimports , specifier , opts )
351
354
352
355
return firstmatch && (
353
356
isRelPathRe . test ( firstmatch )
@@ -373,10 +376,11 @@ const esmparseimport = (targetpath, specifier, pjson) => {
373
376
// 7. Let packageSubpath be "." concatenated with the substring of
374
377
// packageSpecifier from the position at the length of packageName.
375
378
// (removed steps 8-12 related to urls and error cases)
376
- const esmparseexport = ( targetpath , pname , pspecifier , pjson ) => {
379
+ const esmparseexport = ( targetpath , pname , pspecifier , pjson , opts ) => {
377
380
const firstmatch = esmparse (
378
381
pjson && pjson . exports ,
379
- pspecifier ? './' + pspecifier : specimport )
382
+ pspecifier ? './' + pspecifier : specimport ,
383
+ opts )
380
384
381
385
return firstmatch && path . join ( targetpath , pname , firstmatch )
382
386
}
@@ -459,9 +463,19 @@ const begin = (moduleId, parent, opts) => {
459
463
}
460
464
461
465
const createopts = ( moduleId , parent , opts ) => {
466
+ const boolOr = ( v , def ) => typeof v === 'boolean' ? v : def
467
+
462
468
opts = opts || { }
463
- opts . isTypescript = typeof opts . isTypescript === 'boolean'
464
- ? opts . isTypescript : isTsExtnRe . test ( parent )
469
+ opts . isTypescript = boolOr ( opts . isTypescript , isTsExtnRe . test ( parent ) )
470
+ opts . isbrowser = boolOr ( opts . isbrowser , false )
471
+ opts . isimport = boolOr ( opts . isimport , true )
472
+
473
+ opts . specprioritylist = [ ]
474
+
475
+ if ( opts . isbrowser ) opts . specprioritylist . push ( specbrowser )
476
+ if ( opts . isimport ) opts . specprioritylist . push ( specimport )
477
+ opts . specprioritylist . push ( specruntime )
478
+ opts . specprioritylist . push ( specdefault )
465
479
466
480
return opts
467
481
}
0 commit comments