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