@rollup/plugin-node-resolve 11.0.0 → 11.2.0

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