@rollup/plugin-node-resolve 11.0.1 → 11.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -8,6 +8,7 @@ var deepMerge = require('deepmerge');
8
8
  var isModule = require('is-module');
9
9
  var fs = require('fs');
10
10
  var util = require('util');
11
+ var url = require('url');
11
12
  var resolve = require('resolve');
12
13
  var pluginutils = require('@rollup/pluginutils');
13
14
 
@@ -20,10 +21,18 @@ var isModule__default = /*#__PURE__*/_interopDefaultLegacy(isModule);
20
21
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
21
22
  var resolve__default = /*#__PURE__*/_interopDefaultLegacy(resolve);
22
23
 
23
- const exists = util.promisify(fs__default['default'].exists);
24
+ const access = util.promisify(fs__default['default'].access);
24
25
  const readFile = util.promisify(fs__default['default'].readFile);
25
26
  const realpath = util.promisify(fs__default['default'].realpath);
26
27
  const stat = util.promisify(fs__default['default'].stat);
28
+ async function exists(filePath) {
29
+ try {
30
+ await access(filePath);
31
+ return true;
32
+ } catch {
33
+ return false;
34
+ }
35
+ }
27
36
 
28
37
  const onError = (error) => {
29
38
  if (error.code === 'ENOENT') {
@@ -115,7 +124,16 @@ function getMainFields(options) {
115
124
  }
116
125
 
117
126
  function getPackageInfo(options) {
118
- const { cache, extensions, pkg, mainFields, preserveSymlinks, useBrowserOverrides } = options;
127
+ const {
128
+ cache,
129
+ extensions,
130
+ pkg,
131
+ mainFields,
132
+ preserveSymlinks,
133
+ useBrowserOverrides,
134
+ rootDir,
135
+ ignoreSideEffectsForRoot
136
+ } = options;
119
137
  let { pkgPath } = options;
120
138
 
121
139
  if (cache.has(pkgPath)) {
@@ -205,13 +223,15 @@ function getPackageInfo(options) {
205
223
  packageInfo.browserMappedMain = false;
206
224
  }
207
225
 
208
- const packageSideEffects = pkg.sideEffects;
209
- if (typeof packageSideEffects === 'boolean') {
210
- internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
211
- } else if (Array.isArray(packageSideEffects)) {
212
- internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
213
- resolve: pkgRoot
214
- });
226
+ if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
227
+ const packageSideEffects = pkg.sideEffects;
228
+ if (typeof packageSideEffects === 'boolean') {
229
+ internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
230
+ } else if (Array.isArray(packageSideEffects)) {
231
+ internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
232
+ resolve: pkgRoot
233
+ });
234
+ }
215
235
  }
216
236
 
217
237
  cache.set(pkgPath, internalPackageInfo);
@@ -229,168 +249,332 @@ function normalizeInput(input) {
229
249
  return [input];
230
250
  }
231
251
 
232
- const resolveImportPath = util.promisify(resolve__default['default']);
233
- const readFile$1 = util.promisify(fs__default['default'].readFile);
252
+ /* eslint-disable no-await-in-loop */
253
+
254
+ const fileExists = util.promisify(fs__default['default'].exists);
255
+
256
+ function isModuleDir(current, moduleDirs) {
257
+ return moduleDirs.some((dir) => current.endsWith(dir));
258
+ }
259
+
260
+ async function findPackageJson(base, moduleDirs) {
261
+ const { root } = path__default['default'].parse(base);
262
+ let current = base;
234
263
 
235
- const pathNotFoundError = (importPath, importer, subPath, pkgPath) =>
236
- new Error(
237
- `Could not resolve import "${importPath}" in "${importer}".` +
238
- ` Package subpath "${subPath}" is not defined by "exports" in ${pkgPath}`
239
- );
264
+ while (current !== root && !isModuleDir(current, moduleDirs)) {
265
+ const pkgJsonPath = path__default['default'].join(current, 'package.json');
266
+ if (await fileExists(pkgJsonPath)) {
267
+ const pkgJsonString = fs__default['default'].readFileSync(pkgJsonPath, 'utf-8');
268
+ return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
269
+ }
270
+ current = path__default['default'].resolve(current, '..');
271
+ }
272
+ return null;
273
+ }
240
274
 
241
- function findExportKeyMatch(exportMap, subPath) {
242
- if (subPath in exportMap) {
243
- return subPath;
275
+ function isUrl(str) {
276
+ try {
277
+ return !!new URL(str);
278
+ } catch (_) {
279
+ return false;
244
280
  }
281
+ }
245
282
 
246
- const matchKeys = Object.keys(exportMap)
247
- .filter((key) => key.endsWith('/') || key.endsWith('*'))
248
- .sort((a, b) => b.length - a.length);
283
+ function isConditions(exports) {
284
+ return typeof exports === 'object' && Object.keys(exports).every((k) => !k.startsWith('.'));
285
+ }
286
+
287
+ function isMappings(exports) {
288
+ return typeof exports === 'object' && !isConditions(exports);
289
+ }
290
+
291
+ function isMixedExports(exports) {
292
+ const keys = Object.keys(exports);
293
+ return keys.some((k) => k.startsWith('.')) && keys.some((k) => !k.startsWith('.'));
294
+ }
295
+
296
+ function createBaseErrorMsg(importSpecifier, importer) {
297
+ return `Could not resolve import "${importSpecifier}" in ${importer}`;
298
+ }
299
+
300
+ function createErrorMsg(context, reason, internal) {
301
+ const { importSpecifier, importer, pkgJsonPath } = context;
302
+ const base = createBaseErrorMsg(importSpecifier, importer);
303
+ const field = internal ? 'imports' : 'exports';
304
+ return `${base} using ${field} defined in ${pkgJsonPath}.${reason ? ` ${reason}` : ''}`;
305
+ }
306
+
307
+ class ResolveError extends Error {}
308
+
309
+ class InvalidConfigurationError extends ResolveError {
310
+ constructor(context, reason) {
311
+ super(createErrorMsg(context, `Invalid "exports" field. ${reason}`));
312
+ }
313
+ }
314
+
315
+ class InvalidModuleSpecifierError extends ResolveError {
316
+ constructor(context, internal) {
317
+ super(createErrorMsg(context, internal));
318
+ }
319
+ }
320
+
321
+ class InvalidPackageTargetError extends ResolveError {
322
+ constructor(context, reason) {
323
+ super(createErrorMsg(context, reason));
324
+ }
325
+ }
326
+
327
+ /* eslint-disable no-await-in-loop, no-undefined */
249
328
 
250
- for (const key of matchKeys) {
251
- if (key.endsWith('*')) {
252
- // star match: "./foo/*": "./foo/*.js"
253
- const keyWithoutStar = key.substring(0, key.length - 1);
254
- if (subPath.startsWith(keyWithoutStar)) {
255
- return key;
329
+ function includesInvalidSegments(pathSegments, moduleDirs) {
330
+ return pathSegments
331
+ .split('/')
332
+ .slice(1)
333
+ .some((t) => ['.', '..', ...moduleDirs].includes(t));
334
+ }
335
+
336
+ async function resolvePackageTarget(context, { target, subpath, pattern, internal }) {
337
+ if (typeof target === 'string') {
338
+ if (!pattern && subpath.length > 0 && !target.endsWith('/')) {
339
+ throw new InvalidModuleSpecifierError(context);
340
+ }
341
+
342
+ if (!target.startsWith('./')) {
343
+ if (internal && !['/', '../'].some((p) => target.startsWith(p)) && !isUrl(target)) {
344
+ // this is a bare package import, remap it and resolve it using regular node resolve
345
+ if (pattern) {
346
+ const result = await context.resolveId(
347
+ target.replace(/\*/g, subpath),
348
+ context.pkgURL.href
349
+ );
350
+ return result ? url.pathToFileURL(result.location) : null;
351
+ }
352
+
353
+ const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
354
+ return result ? url.pathToFileURL(result.location) : null;
256
355
  }
356
+ throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
357
+ }
358
+
359
+ if (includesInvalidSegments(target, context.moduleDirs)) {
360
+ throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
257
361
  }
258
362
 
259
- if (key.endsWith('/') && subPath.startsWith(key)) {
260
- // directory match (deprecated by node): "./foo/": "./foo/.js"
261
- return key;
363
+ const resolvedTarget = new URL(target, context.pkgURL);
364
+ if (!resolvedTarget.href.startsWith(context.pkgURL.href)) {
365
+ throw new InvalidPackageTargetError(
366
+ context,
367
+ `Resolved to ${resolvedTarget.href} which is outside package ${context.pkgURL.href}`
368
+ );
369
+ }
370
+
371
+ if (includesInvalidSegments(subpath, context.moduleDirs)) {
372
+ throw new InvalidModuleSpecifierError(context);
262
373
  }
263
374
 
264
- if (key === subPath) {
265
- // literal match
266
- return key;
375
+ if (pattern) {
376
+ return resolvedTarget.href.replace(/\*/g, subpath);
267
377
  }
378
+ return new URL(subpath, resolvedTarget).href;
268
379
  }
269
- return null;
270
- }
271
380
 
272
- function mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value }) {
273
- if (typeof value === 'string') {
274
- if (typeof key === 'string' && key.endsWith('*')) {
275
- // star match: "./foo/*": "./foo/*.js"
276
- const keyWithoutStar = key.substring(0, key.length - 1);
277
- const subPathAfterKey = subPath.substring(keyWithoutStar.length);
278
- return value.replace(/\*/g, subPathAfterKey);
381
+ if (Array.isArray(target)) {
382
+ let lastError;
383
+ for (const item of target) {
384
+ try {
385
+ const resolved = await resolvePackageTarget(context, {
386
+ target: item,
387
+ subpath,
388
+ pattern,
389
+ internal
390
+ });
391
+
392
+ // return if defined or null, but not undefined
393
+ if (resolved !== undefined) {
394
+ return resolved;
395
+ }
396
+ } catch (error) {
397
+ if (!(error instanceof InvalidPackageTargetError)) {
398
+ throw error;
399
+ } else {
400
+ lastError = error;
401
+ }
402
+ }
279
403
  }
280
404
 
281
- if (value.endsWith('/')) {
282
- // directory match (deprecated by node): "./foo/": "./foo/.js"
283
- return `${value}${subPath.substring(key.length)}`;
405
+ if (lastError) {
406
+ throw lastError;
284
407
  }
408
+ return null;
409
+ }
410
+
411
+ if (target && typeof target === 'object') {
412
+ for (const [key, value] of Object.entries(target)) {
413
+ if (key === 'default' || context.conditions.includes(key)) {
414
+ const resolved = await resolvePackageTarget(context, {
415
+ target: value,
416
+ subpath,
417
+ pattern,
418
+ internal
419
+ });
285
420
 
286
- // mapping is a string, for example { "./foo": "./dist/foo.js" }
287
- return value;
421
+ // return if defined or null, but not undefined
422
+ if (resolved !== undefined) {
423
+ return resolved;
424
+ }
425
+ }
426
+ }
427
+ return undefined;
288
428
  }
289
429
 
290
- if (Array.isArray(value)) {
291
- // mapping is an array with fallbacks, for example { "./foo": ["foo:bar", "./dist/foo.js"] }
292
- return value.find((v) => v.startsWith('./'));
430
+ if (target === null) {
431
+ return null;
293
432
  }
294
433
 
295
- throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
434
+ throw new InvalidPackageTargetError(context, `Invalid exports field.`);
296
435
  }
297
436
 
298
- function findEntrypoint({
299
- importPath,
300
- importer,
301
- pkgJsonPath,
302
- subPath,
303
- exportMap,
304
- conditions,
305
- key
306
- }) {
307
- if (typeof exportMap !== 'object') {
308
- return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value: exportMap });
437
+ /* eslint-disable no-await-in-loop */
438
+
439
+ async function resolvePackageImportsExports(context, { matchKey, matchObj, internal }) {
440
+ if (!matchKey.endsWith('*') && matchKey in matchObj) {
441
+ const target = matchObj[matchKey];
442
+ const resolved = await resolvePackageTarget(context, { target, subpath: '', internal });
443
+ return resolved;
309
444
  }
310
445
 
311
- // iterate conditions recursively, find the first that matches all conditions
312
- for (const [condition, subExportMap] of Object.entries(exportMap)) {
313
- if (conditions.includes(condition)) {
314
- const mappedSubPath = findEntrypoint({
315
- importPath,
316
- importer,
317
- pkgJsonPath,
318
- subPath,
319
- exportMap: subExportMap,
320
- conditions,
321
- key
446
+ const expansionKeys = Object.keys(matchObj)
447
+ .filter((k) => k.endsWith('/') || k.endsWith('*'))
448
+ .sort((a, b) => b.length - a.length);
449
+
450
+ for (const expansionKey of expansionKeys) {
451
+ const prefix = expansionKey.substring(0, expansionKey.length - 1);
452
+
453
+ if (expansionKey.endsWith('*') && matchKey.startsWith(prefix)) {
454
+ const target = matchObj[expansionKey];
455
+ const subpath = matchKey.substring(expansionKey.length - 1);
456
+ const resolved = await resolvePackageTarget(context, {
457
+ target,
458
+ subpath,
459
+ pattern: true,
460
+ internal
322
461
  });
323
- if (mappedSubPath) {
324
- return mappedSubPath;
325
- }
462
+ return resolved;
326
463
  }
327
- }
328
- throw pathNotFoundError(importer, subPath, pkgJsonPath);
329
- }
330
464
 
331
- function findEntrypointTopLevel({
332
- importPath,
333
- importer,
334
- pkgJsonPath,
335
- subPath,
336
- exportMap,
337
- conditions
338
- }) {
339
- if (typeof exportMap !== 'object') {
340
- // the export map shorthand, for example { exports: "./index.js" }
341
- if (subPath !== '.') {
342
- // shorthand only supports a main entrypoint
343
- throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
465
+ if (matchKey.startsWith(expansionKey)) {
466
+ const target = matchObj[expansionKey];
467
+ const subpath = matchKey.substring(expansionKey.length);
468
+
469
+ const resolved = await resolvePackageTarget(context, { target, subpath, internal });
470
+ return resolved;
344
471
  }
345
- return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key: null, value: exportMap });
346
472
  }
347
473
 
348
- // export map is an object, the top level can be either conditions or sub path mappings
349
- const keys = Object.keys(exportMap);
350
- const isConditions = keys.every((k) => !k.startsWith('.'));
351
- const isMappings = keys.every((k) => k.startsWith('.'));
474
+ throw new InvalidModuleSpecifierError(context, internal);
475
+ }
352
476
 
353
- if (!isConditions && !isMappings) {
354
- throw new Error(
355
- `Invalid package config ${pkgJsonPath}, "exports" cannot contain some keys starting with '.'` +
356
- ' and some not. The exports object must either be an object of package subpath keys or an object of main entry' +
357
- ' condition name keys only.'
477
+ async function resolvePackageExports(context, subpath, exports) {
478
+ if (isMixedExports(exports)) {
479
+ throw new InvalidConfigurationError(
480
+ context,
481
+ 'All keys must either start with ./, or without one.'
358
482
  );
359
483
  }
360
484
 
361
- let key = null;
362
- let exportMapForSubPath;
485
+ if (subpath === '.') {
486
+ let mainExport;
487
+ // If exports is a String or Array, or an Object containing no keys starting with ".", then
488
+ if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) {
489
+ mainExport = exports;
490
+ } else if (isMappings(exports)) {
491
+ mainExport = exports['.'];
492
+ }
363
493
 
364
- if (isConditions) {
365
- // top level is conditions, for example { "import": ..., "require": ..., "module": ... }
366
- if (subPath !== '.') {
367
- // package with top level conditions means it only supports a main entrypoint
368
- throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
494
+ if (mainExport) {
495
+ const resolved = await resolvePackageTarget(context, { target: mainExport, subpath: '' });
496
+ if (resolved) {
497
+ return resolved;
498
+ }
369
499
  }
370
- exportMapForSubPath = exportMap;
371
- } else {
372
- // top level is sub path mappings, for example { ".": ..., "./foo": ..., "./bar": ... }
373
- key = findExportKeyMatch(exportMap, subPath);
374
- if (!key) {
375
- throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
500
+ } else if (isMappings(exports)) {
501
+ const resolvedMatch = await resolvePackageImportsExports(context, {
502
+ matchKey: subpath,
503
+ matchObj: exports
504
+ });
505
+
506
+ if (resolvedMatch) {
507
+ return resolvedMatch;
376
508
  }
377
- exportMapForSubPath = exportMap[key];
378
509
  }
379
510
 
380
- return findEntrypoint({
381
- importPath,
511
+ throw new InvalidModuleSpecifierError(context);
512
+ }
513
+
514
+ async function resolvePackageImports({
515
+ importSpecifier,
516
+ importer,
517
+ moduleDirs,
518
+ conditions,
519
+ resolveId
520
+ }) {
521
+ const result = await findPackageJson(importer, moduleDirs);
522
+ if (!result) {
523
+ throw new Error(createBaseErrorMsg('. Could not find a parent package.json.'));
524
+ }
525
+
526
+ const { pkgPath, pkgJsonPath, pkgJson } = result;
527
+ const pkgURL = url.pathToFileURL(`${pkgPath}/`);
528
+ const context = {
382
529
  importer,
530
+ importSpecifier,
531
+ moduleDirs,
532
+ pkgURL,
383
533
  pkgJsonPath,
384
- subPath,
385
- exportMap: exportMapForSubPath,
386
534
  conditions,
387
- key
535
+ resolveId
536
+ };
537
+
538
+ const { imports } = pkgJson;
539
+ if (!imports) {
540
+ throw new InvalidModuleSpecifierError(context, true);
541
+ }
542
+
543
+ if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
544
+ throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
545
+ }
546
+
547
+ return resolvePackageImportsExports(context, {
548
+ matchKey: importSpecifier,
549
+ matchObj: imports,
550
+ internal: true
388
551
  });
389
552
  }
390
553
 
554
+ const resolveImportPath = util.promisify(resolve__default['default']);
555
+ const readFile$1 = util.promisify(fs__default['default'].readFile);
556
+
557
+ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
558
+ if (importer) {
559
+ const selfPackageJsonResult = await findPackageJson(importer, moduleDirectories);
560
+ if (selfPackageJsonResult && selfPackageJsonResult.pkgJson.name === pkgName) {
561
+ // the referenced package name is the current package
562
+ return selfPackageJsonResult;
563
+ }
564
+ }
565
+
566
+ try {
567
+ const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
568
+ const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
569
+ return { pkgJsonPath, pkgJson };
570
+ } catch (_) {
571
+ return null;
572
+ }
573
+ }
574
+
391
575
  async function resolveId({
392
576
  importer,
393
- importPath,
577
+ importSpecifier,
394
578
  exportConditions,
395
579
  warn,
396
580
  packageInfoCache,
@@ -399,7 +583,9 @@ async function resolveId({
399
583
  preserveSymlinks,
400
584
  useBrowserOverrides,
401
585
  baseDir,
402
- moduleDirectories
586
+ moduleDirectories,
587
+ rootDir,
588
+ ignoreSideEffectsForRoot
403
589
  }) {
404
590
  let hasModuleSideEffects = () => null;
405
591
  let hasPackageEntry = true;
@@ -414,7 +600,9 @@ async function resolveId({
414
600
  pkgPath,
415
601
  mainFields,
416
602
  preserveSymlinks,
417
- useBrowserOverrides
603
+ useBrowserOverrides,
604
+ rootDir,
605
+ ignoreSideEffectsForRoot
418
606
  });
419
607
 
420
608
  ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
@@ -436,41 +624,70 @@ async function resolveId({
436
624
 
437
625
  let location;
438
626
 
439
- const pkgName = getPackageName(importPath);
440
- if (pkgName) {
441
- let pkgJsonPath;
442
- let pkgJson;
443
- try {
444
- pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
445
- pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
446
- } catch (_) {
447
- // if there is no package.json we defer to regular resolve behavior
448
- }
627
+ const pkgName = getPackageName(importSpecifier);
628
+ if (importSpecifier.startsWith('#')) {
629
+ // this is a package internal import, resolve using package imports field
630
+ const resolveResult = await resolvePackageImports({
631
+ importSpecifier,
632
+ importer,
633
+ moduleDirs: moduleDirectories,
634
+ conditions: exportConditions,
635
+ resolveId(id, parent) {
636
+ return resolveId({
637
+ importSpecifier: id,
638
+ importer: parent,
639
+ exportConditions,
640
+ warn,
641
+ packageInfoCache,
642
+ extensions,
643
+ mainFields,
644
+ preserveSymlinks,
645
+ useBrowserOverrides,
646
+ baseDir,
647
+ moduleDirectories
648
+ });
649
+ }
650
+ });
651
+ location = url.fileURLToPath(resolveResult);
652
+ } else if (pkgName) {
653
+ // it's a bare import, find the package.json and resolve using package exports if available
654
+ const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
449
655
 
450
- if (pkgJsonPath && pkgJson && pkgJson.exports) {
656
+ if (result && result.pkgJson.exports) {
657
+ const { pkgJson, pkgJsonPath } = result;
451
658
  try {
452
- const packageSubPath =
453
- pkgName === importPath ? '.' : `.${importPath.substring(pkgName.length)}`;
454
- const mappedSubPath = findEntrypointTopLevel({
659
+ const subpath =
660
+ pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
661
+ const pkgDr = pkgJsonPath.replace('package.json', '');
662
+ const pkgURL = url.pathToFileURL(pkgDr);
663
+
664
+ const context = {
455
665
  importer,
456
- importPath,
666
+ importSpecifier,
667
+ moduleDirs: moduleDirectories,
668
+ pkgURL,
457
669
  pkgJsonPath,
458
- subPath: packageSubPath,
459
- exportMap: pkgJson.exports,
460
670
  conditions: exportConditions
461
- });
462
- const pkgDir = path__default['default'].dirname(pkgJsonPath);
463
- location = path__default['default'].join(pkgDir, mappedSubPath);
671
+ };
672
+ const resolvedPackageExport = await resolvePackageExports(
673
+ context,
674
+ subpath,
675
+ pkgJson.exports
676
+ );
677
+ location = url.fileURLToPath(resolvedPackageExport);
464
678
  } catch (error) {
465
- warn(error);
466
- return null;
679
+ if (error instanceof ResolveError) {
680
+ return error;
681
+ }
682
+ throw error;
467
683
  }
468
684
  }
469
685
  }
470
686
 
471
687
  if (!location) {
688
+ // package has no imports or exports, use classic node resolve
472
689
  try {
473
- location = await resolveImportPath(importPath, resolveOptions);
690
+ location = await resolveImportPath(importSpecifier, resolveOptions);
474
691
  } catch (error) {
475
692
  if (error.code !== 'MODULE_NOT_FOUND') {
476
693
  throw error;
@@ -507,13 +724,17 @@ async function resolveImportSpecifiers({
507
724
  preserveSymlinks,
508
725
  useBrowserOverrides,
509
726
  baseDir,
510
- moduleDirectories
727
+ moduleDirectories,
728
+ rootDir,
729
+ ignoreSideEffectsForRoot
511
730
  }) {
731
+ let lastResolveError;
732
+
512
733
  for (let i = 0; i < importSpecifierList.length; i++) {
513
734
  // eslint-disable-next-line no-await-in-loop
514
- const resolved = await resolveId({
735
+ const result = await resolveId({
515
736
  importer,
516
- importPath: importSpecifierList[i],
737
+ importSpecifier: importSpecifierList[i],
517
738
  exportConditions,
518
739
  warn,
519
740
  packageInfoCache,
@@ -522,12 +743,22 @@ async function resolveImportSpecifiers({
522
743
  preserveSymlinks,
523
744
  useBrowserOverrides,
524
745
  baseDir,
525
- moduleDirectories
746
+ moduleDirectories,
747
+ rootDir,
748
+ ignoreSideEffectsForRoot
526
749
  });
527
- if (resolved) {
528
- return resolved;
750
+
751
+ if (result instanceof ResolveError) {
752
+ lastResolveError = result;
753
+ } else if (result) {
754
+ return result;
529
755
  }
530
756
  }
757
+
758
+ if (lastResolveError) {
759
+ // only log the last failed resolve error
760
+ warn(lastResolveError);
761
+ }
531
762
  return null;
532
763
  }
533
764
 
@@ -603,7 +834,8 @@ const defaults = {
603
834
  // which deploy both ESM .mjs and CommonJS .js files as ESM.
604
835
  extensions: ['.mjs', '.js', '.json', '.node'],
605
836
  resolveOnly: [],
606
- moduleDirectories: ['node_modules']
837
+ moduleDirectories: ['node_modules'],
838
+ ignoreSideEffectsForRoot: false
607
839
  };
608
840
  const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
609
841
 
@@ -611,7 +843,7 @@ function nodeResolve(opts = {}) {
611
843
  const { warnings } = handleDeprecatedOptions(opts);
612
844
 
613
845
  const options = { ...defaults, ...opts };
614
- const { extensions, jail, moduleDirectories } = options;
846
+ const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
615
847
  const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
616
848
  const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
617
849
  const packageInfoCache = new Map();
@@ -620,7 +852,7 @@ function nodeResolve(opts = {}) {
620
852
  const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
621
853
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
622
854
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
623
- const rootDir = options.rootDir || process.cwd();
855
+ const rootDir = path.resolve(options.rootDir || process.cwd());
624
856
  let { dedupe } = options;
625
857
  let rollupOptions;
626
858
 
@@ -769,7 +1001,9 @@ function nodeResolve(opts = {}) {
769
1001
  preserveSymlinks,
770
1002
  useBrowserOverrides,
771
1003
  baseDir,
772
- moduleDirectories
1004
+ moduleDirectories,
1005
+ rootDir,
1006
+ ignoreSideEffectsForRoot
773
1007
  });
774
1008
 
775
1009
  const resolved =