@rollup/plugin-node-resolve 13.0.2 → 13.0.6

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/CHANGELOG.md CHANGED
@@ -1,12 +1,44 @@
1
1
  # @rollup/plugin-node-resolve ChangeLog
2
2
 
3
+ ## v13.0.6
4
+
5
+ _2021-10-19_
6
+
7
+ ### Bugfixes
8
+
9
+ - fix: pass on isEntry flag (#1016)
10
+
11
+ ## v13.0.5
12
+
13
+ _2021-09-21_
14
+
15
+ ### Updates
16
+
17
+ - docs: fix readme heading depth (#999)
18
+
19
+ ## v13.0.4
20
+
21
+ _2021-07-24_
22
+
23
+ ### Bugfixes
24
+
25
+ - fix: Fix bug where JS import was converted to a TS import, resulting in an error when using export maps (#921)
26
+
27
+ ## v13.0.3
28
+
29
+ _2021-07-24_
30
+
31
+ ### Bugfixes
32
+
33
+ - fix: handle browser-mapped paths correctly in nested contexts (#920)
34
+
3
35
  ## v13.0.2
4
36
 
5
37
  _2021-07-15_
6
38
 
7
39
  ### Bugfixes
8
40
 
9
- - fix
41
+ - fix: handle "package.json" being in path (#927)
10
42
 
11
43
  ## v13.0.1
12
44
 
@@ -451,4 +483,4 @@ This release caches reading/statting of files, to improve speed.
451
483
 
452
484
  ## 0.1.0
453
485
 
454
- - First release
486
+ - First release
package/README.md CHANGED
@@ -159,7 +159,7 @@ Specifies the root directory from which to resolve modules. Typically used when
159
159
  rootDir: path.join(process.cwd(), '..')
160
160
  ```
161
161
 
162
- ## `ignoreSideEffectsForRoot`
162
+ ### `ignoreSideEffectsForRoot`
163
163
 
164
164
  If you use the `sideEffects` property in the package.json, by default this is respected for files in the root package. Set to `true` to ignore the `sideEffects` configuration for the root package.
165
165
 
package/dist/cjs/index.js CHANGED
@@ -21,10 +21,10 @@ var isModule__default = /*#__PURE__*/_interopDefaultLegacy(isModule);
21
21
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
22
22
  var resolve__default = /*#__PURE__*/_interopDefaultLegacy(resolve);
23
23
 
24
- util.promisify(fs__default['default'].access);
25
- const readFile$1 = util.promisify(fs__default['default'].readFile);
26
- const realpath = util.promisify(fs__default['default'].realpath);
27
- const stat = util.promisify(fs__default['default'].stat);
24
+ util.promisify(fs__default["default"].access);
25
+ const readFile$1 = util.promisify(fs__default["default"].readFile);
26
+ const realpath = util.promisify(fs__default["default"].realpath);
27
+ const stat = util.promisify(fs__default["default"].stat);
28
28
 
29
29
  async function fileExists(filePath) {
30
30
  try {
@@ -35,6 +35,10 @@ async function fileExists(filePath) {
35
35
  }
36
36
  }
37
37
 
38
+ async function resolveSymlink(path) {
39
+ return (await fileExists(path)) ? realpath(path) : path;
40
+ }
41
+
38
42
  const onError = (error) => {
39
43
  if (error.code === 'ENOENT') {
40
44
  return false;
@@ -257,16 +261,16 @@ function isModuleDir(current, moduleDirs) {
257
261
  }
258
262
 
259
263
  async function findPackageJson(base, moduleDirs) {
260
- const { root } = path__default['default'].parse(base);
264
+ const { root } = path__default["default"].parse(base);
261
265
  let current = base;
262
266
 
263
267
  while (current !== root && !isModuleDir(current, moduleDirs)) {
264
- const pkgJsonPath = path__default['default'].join(current, 'package.json');
268
+ const pkgJsonPath = path__default["default"].join(current, 'package.json');
265
269
  if (await fileExists(pkgJsonPath)) {
266
- const pkgJsonString = fs__default['default'].readFileSync(pkgJsonPath, 'utf-8');
270
+ const pkgJsonString = fs__default["default"].readFileSync(pkgJsonPath, 'utf-8');
267
271
  return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
268
272
  }
269
- current = path__default['default'].resolve(current, '..');
273
+ current = path__default["default"].resolve(current, '..');
270
274
  }
271
275
  return null;
272
276
  }
@@ -312,8 +316,8 @@ class InvalidConfigurationError extends ResolveError {
312
316
  }
313
317
 
314
318
  class InvalidModuleSpecifierError extends ResolveError {
315
- constructor(context, internal) {
316
- super(createErrorMsg(context, internal));
319
+ constructor(context, internal, reason) {
320
+ super(createErrorMsg(context, reason, internal));
317
321
  }
318
322
  }
319
323
 
@@ -540,7 +544,7 @@ async function resolvePackageImports({
540
544
  }
541
545
 
542
546
  if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
543
- throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
547
+ throw new InvalidModuleSpecifierError(context, true, 'Invalid import specifier.');
544
548
  }
545
549
 
546
550
  return resolvePackageImportsExports(context, {
@@ -550,8 +554,8 @@ async function resolvePackageImports({
550
554
  });
551
555
  }
552
556
 
553
- const resolveImportPath = util.promisify(resolve__default['default']);
554
- const readFile = util.promisify(fs__default['default'].readFile);
557
+ const resolveImportPath = util.promisify(resolve__default["default"]);
558
+ const readFile = util.promisify(fs__default["default"].readFile);
555
559
 
556
560
  async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
557
561
  if (importer) {
@@ -571,11 +575,8 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
571
575
  }
572
576
  }
573
577
 
574
- async function resolveId({
575
- importer,
578
+ async function resolveIdClassic({
576
579
  importSpecifier,
577
- exportConditions,
578
- warn,
579
580
  packageInfoCache,
580
581
  extensions,
581
582
  mainFields,
@@ -622,8 +623,38 @@ async function resolveId({
622
623
  };
623
624
 
624
625
  let location;
626
+ try {
627
+ location = await resolveImportPath(importSpecifier, resolveOptions);
628
+ } catch (error) {
629
+ if (error.code !== 'MODULE_NOT_FOUND') {
630
+ throw error;
631
+ }
632
+ return null;
633
+ }
625
634
 
626
- const pkgName = getPackageName(importSpecifier);
635
+ return {
636
+ location: preserveSymlinks ? location : await resolveSymlink(location),
637
+ hasModuleSideEffects,
638
+ hasPackageEntry,
639
+ packageBrowserField,
640
+ packageInfo
641
+ };
642
+ }
643
+
644
+ async function resolveWithExportMap({
645
+ importer,
646
+ importSpecifier,
647
+ exportConditions,
648
+ packageInfoCache,
649
+ extensions,
650
+ mainFields,
651
+ preserveSymlinks,
652
+ useBrowserOverrides,
653
+ baseDir,
654
+ moduleDirectories,
655
+ rootDir,
656
+ ignoreSideEffectsForRoot
657
+ }) {
627
658
  if (importSpecifier.startsWith('#')) {
628
659
  // this is a package internal import, resolve using package imports field
629
660
  const resolveResult = await resolvePackageImports({
@@ -631,12 +662,9 @@ async function resolveId({
631
662
  importer,
632
663
  moduleDirs: moduleDirectories,
633
664
  conditions: exportConditions,
634
- resolveId(id, parent) {
635
- return resolveId({
665
+ resolveId(id /* , parent*/) {
666
+ return resolveIdClassic({
636
667
  importSpecifier: id,
637
- importer: parent,
638
- exportConditions,
639
- warn,
640
668
  packageInfoCache,
641
669
  extensions,
642
670
  mainFields,
@@ -647,71 +675,91 @@ async function resolveId({
647
675
  });
648
676
  }
649
677
  });
650
- location = url.fileURLToPath(resolveResult);
651
- } else if (pkgName) {
678
+
679
+ const location = url.fileURLToPath(resolveResult);
680
+ return {
681
+ location: preserveSymlinks ? location : await resolveSymlink(location),
682
+ hasModuleSideEffects: () => null,
683
+ hasPackageEntry: true,
684
+ packageBrowserField: false,
685
+ // eslint-disable-next-line no-undefined
686
+ packageInfo: undefined
687
+ };
688
+ }
689
+
690
+ const pkgName = getPackageName(importSpecifier);
691
+ if (pkgName) {
652
692
  // it's a bare import, find the package.json and resolve using package exports if available
693
+ let hasModuleSideEffects = () => null;
694
+ let hasPackageEntry = true;
695
+ let packageBrowserField = false;
696
+ let packageInfo;
697
+
698
+ const filter = (pkg, pkgPath) => {
699
+ const info = getPackageInfo({
700
+ cache: packageInfoCache,
701
+ extensions,
702
+ pkg,
703
+ pkgPath,
704
+ mainFields,
705
+ preserveSymlinks,
706
+ useBrowserOverrides,
707
+ rootDir,
708
+ ignoreSideEffectsForRoot
709
+ });
710
+
711
+ ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
712
+
713
+ return info.cachedPkg;
714
+ };
715
+
716
+ const resolveOptions = {
717
+ basedir: baseDir,
718
+ readFile: readCachedFile,
719
+ isFile: isFileCached,
720
+ isDirectory: isDirCached,
721
+ extensions,
722
+ includeCoreModules: false,
723
+ moduleDirectory: moduleDirectories,
724
+ preserveSymlinks,
725
+ packageFilter: filter
726
+ };
727
+
653
728
  const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
654
729
 
655
730
  if (result && result.pkgJson.exports) {
656
- const { pkgJson, pkgJsonPath, pkgPath } = result;
657
- try {
658
- const subpath =
659
- pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
660
- const pkgURL = url.pathToFileURL(`${pkgPath}/`);
661
-
662
- const context = {
663
- importer,
664
- importSpecifier,
665
- moduleDirs: moduleDirectories,
666
- pkgURL,
667
- pkgJsonPath,
668
- conditions: exportConditions
731
+ const { pkgJson, pkgJsonPath } = result;
732
+ const subpath =
733
+ pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
734
+ const pkgDr = pkgJsonPath.replace('package.json', '');
735
+ const pkgURL = url.pathToFileURL(pkgDr);
736
+
737
+ const context = {
738
+ importer,
739
+ importSpecifier,
740
+ moduleDirs: moduleDirectories,
741
+ pkgURL,
742
+ pkgJsonPath,
743
+ conditions: exportConditions
744
+ };
745
+ const resolvedPackageExport = await resolvePackageExports(context, subpath, pkgJson.exports);
746
+ const location = url.fileURLToPath(resolvedPackageExport);
747
+ if (location) {
748
+ return {
749
+ location: preserveSymlinks ? location : await resolveSymlink(location),
750
+ hasModuleSideEffects,
751
+ hasPackageEntry,
752
+ packageBrowserField,
753
+ packageInfo
669
754
  };
670
- const resolvedPackageExport = await resolvePackageExports(
671
- context,
672
- subpath,
673
- pkgJson.exports
674
- );
675
- location = url.fileURLToPath(resolvedPackageExport);
676
- } catch (error) {
677
- if (error instanceof ResolveError) {
678
- return error;
679
- }
680
- throw error;
681
755
  }
682
756
  }
683
757
  }
684
758
 
685
- if (!location) {
686
- // package has no imports or exports, use classic node resolve
687
- try {
688
- location = await resolveImportPath(importSpecifier, resolveOptions);
689
- } catch (error) {
690
- if (error.code !== 'MODULE_NOT_FOUND') {
691
- throw error;
692
- }
693
- return null;
694
- }
695
- }
696
-
697
- if (!preserveSymlinks) {
698
- if (await fileExists(location)) {
699
- location = await realpath(location);
700
- }
701
- }
702
-
703
- return {
704
- location,
705
- hasModuleSideEffects,
706
- hasPackageEntry,
707
- packageBrowserField,
708
- packageInfo
709
- };
759
+ return null;
710
760
  }
711
761
 
712
- // Resolve module specifiers in order. Promise resolves to the first module that resolves
713
- // successfully, or the error that resulted from the last attempted module resolution.
714
- async function resolveImportSpecifiers({
762
+ async function resolveWithClassic({
715
763
  importer,
716
764
  importSpecifierList,
717
765
  exportConditions,
@@ -726,11 +774,9 @@ async function resolveImportSpecifiers({
726
774
  rootDir,
727
775
  ignoreSideEffectsForRoot
728
776
  }) {
729
- let lastResolveError;
730
-
731
777
  for (let i = 0; i < importSpecifierList.length; i++) {
732
778
  // eslint-disable-next-line no-await-in-loop
733
- const result = await resolveId({
779
+ const result = await resolveIdClassic({
734
780
  importer,
735
781
  importSpecifier: importSpecifierList[i],
736
782
  exportConditions,
@@ -746,20 +792,75 @@ async function resolveImportSpecifiers({
746
792
  ignoreSideEffectsForRoot
747
793
  });
748
794
 
749
- if (result instanceof ResolveError) {
750
- lastResolveError = result;
751
- } else if (result) {
795
+ if (result) {
752
796
  return result;
753
797
  }
754
798
  }
755
799
 
756
- if (lastResolveError) {
757
- // only log the last failed resolve error
758
- warn(lastResolveError);
759
- }
760
800
  return null;
761
801
  }
762
802
 
803
+ // Resolves to the module if found or `null`.
804
+ // The first import specificer will first be attempted with the exports algorithm.
805
+ // If this is unsuccesful because export maps are not being used, then all of `importSpecifierList`
806
+ // will be tried with the classic resolution algorithm
807
+ async function resolveImportSpecifiers({
808
+ importer,
809
+ importSpecifierList,
810
+ exportConditions,
811
+ warn,
812
+ packageInfoCache,
813
+ extensions,
814
+ mainFields,
815
+ preserveSymlinks,
816
+ useBrowserOverrides,
817
+ baseDir,
818
+ moduleDirectories,
819
+ rootDir,
820
+ ignoreSideEffectsForRoot
821
+ }) {
822
+ try {
823
+ const exportMapRes = await resolveWithExportMap({
824
+ importer,
825
+ importSpecifier: importSpecifierList[0],
826
+ exportConditions,
827
+ packageInfoCache,
828
+ extensions,
829
+ mainFields,
830
+ preserveSymlinks,
831
+ useBrowserOverrides,
832
+ baseDir,
833
+ moduleDirectories,
834
+ rootDir,
835
+ ignoreSideEffectsForRoot
836
+ });
837
+ if (exportMapRes) return exportMapRes;
838
+ } catch (error) {
839
+ if (error instanceof ResolveError) {
840
+ warn(error);
841
+ return null;
842
+ }
843
+ throw error;
844
+ }
845
+
846
+ // package has no imports or exports, use classic node resolve
847
+ return resolveWithClassic({
848
+ importer,
849
+ importSpecifierList,
850
+ exportConditions,
851
+ warn,
852
+ packageInfoCache,
853
+ extensions,
854
+ mainFields,
855
+ preserveSymlinks,
856
+ useBrowserOverrides,
857
+ baseDir,
858
+ moduleDirectories,
859
+ rootDir,
860
+ ignoreSideEffectsForRoot
861
+ });
862
+ }
863
+
763
864
  function handleDeprecatedOptions(opts) {
764
865
  const warnings = [];
765
866
 
@@ -809,7 +910,7 @@ function handleDeprecatedOptions(opts) {
809
910
 
810
911
  /* eslint-disable no-param-reassign, no-shadow, no-undefined */
811
912
 
812
- const builtins = new Set(builtinList__default['default']);
913
+ const builtins = new Set(builtinList__default["default"]);
813
914
  const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
814
915
  const deepFreeze = (object) => {
815
916
  Object.freeze(object);
@@ -835,7 +936,7 @@ const defaults = {
835
936
  moduleDirectories: ['node_modules'],
836
937
  ignoreSideEffectsForRoot: false
837
938
  };
838
- const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
939
+ const DEFAULTS = deepFreeze(deepMerge__default["default"]({}, defaults));
839
940
 
840
941
  function nodeResolve(opts = {}) {
841
942
  const { warnings } = handleDeprecatedOptions(opts);
@@ -870,7 +971,7 @@ function nodeResolve(opts = {}) {
870
971
  const browserMapCache = new Map();
871
972
  let preserveSymlinks;
872
973
 
873
- const doResolveId = async (context, importee, importer, opts) => {
974
+ const doResolveId = async (context, importee, importer, custom) => {
874
975
  // strip query params from import
875
976
  const [importPath, params] = importee.split('?');
876
977
  const importSuffix = `${params ? `?${params}` : ''}`;
@@ -886,7 +987,7 @@ function nodeResolve(opts = {}) {
886
987
  return { id: ES6_BROWSER_EMPTY };
887
988
  }
888
989
  const browserImportee =
889
- browser[importee] ||
990
+ (importee[0] !== '.' && browser[importee]) ||
890
991
  browser[resolvedImportee] ||
891
992
  browser[`${resolvedImportee}.js`] ||
892
993
  browser[`${resolvedImportee}.json`];
@@ -919,30 +1020,17 @@ function nodeResolve(opts = {}) {
919
1020
  return false;
920
1021
  }
921
1022
 
922
- const importSpecifierList = [];
1023
+ const importSpecifierList = [importee];
923
1024
 
924
1025
  if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
925
1026
  // For module graph roots (i.e. when importer is undefined), we
926
1027
  // need to handle 'path fragments` like `foo/bar` that are commonly
927
1028
  // found in rollup config files. If importee doesn't look like a
928
1029
  // relative or absolute path, we make it relative and attempt to
929
- // resolve it. If we don't find anything, we try resolving it as we
930
- // got it.
1030
+ // resolve it.
931
1031
  importSpecifierList.push(`./${importee}`);
932
1032
  }
933
1033
 
934
- const importeeIsBuiltin = builtins.has(importee);
935
-
936
- if (importeeIsBuiltin) {
937
- // The `resolve` library will not resolve packages with the same
938
- // name as a node built-in module. If we're resolving something
939
- // that's a builtin, and we don't prefer to find built-ins, we
940
- // first try to look up a local module with that name. If we don't
941
- // find anything, we resolve the builtin which just returns back
942
- // the built-in's name.
943
- importSpecifierList.push(`${importee}/`);
944
- }
945
-
946
1034
  // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
947
1035
  if (importer && importee.endsWith('.js')) {
948
1036
  for (const ext of ['.ts', '.tsx']) {
@@ -952,11 +1040,8 @@ function nodeResolve(opts = {}) {
952
1040
  }
953
1041
  }
954
1042
 
955
- importSpecifierList.push(importee);
956
-
957
1043
  const warn = (...args) => context.warn(...args);
958
- const isRequire =
959
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
1044
+ const isRequire = custom && custom['node-resolve'] && custom['node-resolve'].isRequire;
960
1045
  const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
961
1046
 
962
1047
  if (useBrowserOverrides && !exportConditions.includes('browser'))
@@ -978,6 +1063,7 @@ function nodeResolve(opts = {}) {
978
1063
  ignoreSideEffectsForRoot
979
1064
  });
980
1065
 
1066
+ const importeeIsBuiltin = builtins.has(importee);
981
1067
  const resolved =
982
1068
  importeeIsBuiltin && preferBuiltins
983
1069
  ? {
@@ -1028,7 +1114,7 @@ function nodeResolve(opts = {}) {
1028
1114
 
1029
1115
  if (options.modulesOnly && (await fileExists(location))) {
1030
1116
  const code = await readFile$1(location, 'utf-8');
1031
- if (isModule__default['default'](code)) {
1117
+ if (isModule__default["default"](code)) {
1032
1118
  return {
1033
1119
  id: `${location}${importSuffix}`,
1034
1120
  moduleSideEffects: hasModuleSideEffects(location)
@@ -1062,7 +1148,7 @@ function nodeResolve(opts = {}) {
1062
1148
  isDirCached.clear();
1063
1149
  },
1064
1150
 
1065
- async resolveId(importee, importer, opts) {
1151
+ async resolveId(importee, importer, resolveOptions) {
1066
1152
  if (importee === ES6_BROWSER_EMPTY) {
1067
1153
  return importee;
1068
1154
  }
@@ -1073,9 +1159,13 @@ function nodeResolve(opts = {}) {
1073
1159
  importer = undefined;
1074
1160
  }
1075
1161
 
1076
- const resolved = await doResolveId(this, importee, importer, opts);
1162
+ const resolved = await doResolveId(this, importee, importer, resolveOptions.custom);
1077
1163
  if (resolved) {
1078
- const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1164
+ const resolvedResolved = await this.resolve(
1165
+ resolved.id,
1166
+ importer,
1167
+ Object.assign({ skipSelf: true }, resolveOptions)
1168
+ );
1079
1169
  const isExternal = !!(resolvedResolved && resolvedResolved.external);
1080
1170
  if (isExternal) {
1081
1171
  return false;
@@ -1098,5 +1188,5 @@ function nodeResolve(opts = {}) {
1098
1188
  }
1099
1189
 
1100
1190
  exports.DEFAULTS = DEFAULTS;
1101
- exports.default = nodeResolve;
1191
+ exports["default"] = nodeResolve;
1102
1192
  exports.nodeResolve = nodeResolve;
package/dist/es/index.js CHANGED
@@ -22,6 +22,10 @@ async function fileExists(filePath) {
22
22
  }
23
23
  }
24
24
 
25
+ async function resolveSymlink(path) {
26
+ return (await fileExists(path)) ? realpath(path) : path;
27
+ }
28
+
25
29
  const onError = (error) => {
26
30
  if (error.code === 'ENOENT') {
27
31
  return false;
@@ -299,8 +303,8 @@ class InvalidConfigurationError extends ResolveError {
299
303
  }
300
304
 
301
305
  class InvalidModuleSpecifierError extends ResolveError {
302
- constructor(context, internal) {
303
- super(createErrorMsg(context, internal));
306
+ constructor(context, internal, reason) {
307
+ super(createErrorMsg(context, reason, internal));
304
308
  }
305
309
  }
306
310
 
@@ -527,7 +531,7 @@ async function resolvePackageImports({
527
531
  }
528
532
 
529
533
  if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
530
- throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
534
+ throw new InvalidModuleSpecifierError(context, true, 'Invalid import specifier.');
531
535
  }
532
536
 
533
537
  return resolvePackageImportsExports(context, {
@@ -558,11 +562,8 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
558
562
  }
559
563
  }
560
564
 
561
- async function resolveId({
562
- importer,
565
+ async function resolveIdClassic({
563
566
  importSpecifier,
564
- exportConditions,
565
- warn,
566
567
  packageInfoCache,
567
568
  extensions,
568
569
  mainFields,
@@ -609,8 +610,38 @@ async function resolveId({
609
610
  };
610
611
 
611
612
  let location;
613
+ try {
614
+ location = await resolveImportPath(importSpecifier, resolveOptions);
615
+ } catch (error) {
616
+ if (error.code !== 'MODULE_NOT_FOUND') {
617
+ throw error;
618
+ }
619
+ return null;
620
+ }
612
621
 
613
- const pkgName = getPackageName(importSpecifier);
622
+ return {
623
+ location: preserveSymlinks ? location : await resolveSymlink(location),
624
+ hasModuleSideEffects,
625
+ hasPackageEntry,
626
+ packageBrowserField,
627
+ packageInfo
628
+ };
629
+ }
630
+
631
+ async function resolveWithExportMap({
632
+ importer,
633
+ importSpecifier,
634
+ exportConditions,
635
+ packageInfoCache,
636
+ extensions,
637
+ mainFields,
638
+ preserveSymlinks,
639
+ useBrowserOverrides,
640
+ baseDir,
641
+ moduleDirectories,
642
+ rootDir,
643
+ ignoreSideEffectsForRoot
644
+ }) {
614
645
  if (importSpecifier.startsWith('#')) {
615
646
  // this is a package internal import, resolve using package imports field
616
647
  const resolveResult = await resolvePackageImports({
@@ -618,12 +649,9 @@ async function resolveId({
618
649
  importer,
619
650
  moduleDirs: moduleDirectories,
620
651
  conditions: exportConditions,
621
- resolveId(id, parent) {
622
- return resolveId({
652
+ resolveId(id /* , parent*/) {
653
+ return resolveIdClassic({
623
654
  importSpecifier: id,
624
- importer: parent,
625
- exportConditions,
626
- warn,
627
655
  packageInfoCache,
628
656
  extensions,
629
657
  mainFields,
@@ -634,71 +662,91 @@ async function resolveId({
634
662
  });
635
663
  }
636
664
  });
637
- location = fileURLToPath(resolveResult);
638
- } else if (pkgName) {
665
+
666
+ const location = fileURLToPath(resolveResult);
667
+ return {
668
+ location: preserveSymlinks ? location : await resolveSymlink(location),
669
+ hasModuleSideEffects: () => null,
670
+ hasPackageEntry: true,
671
+ packageBrowserField: false,
672
+ // eslint-disable-next-line no-undefined
673
+ packageInfo: undefined
674
+ };
675
+ }
676
+
677
+ const pkgName = getPackageName(importSpecifier);
678
+ if (pkgName) {
639
679
  // it's a bare import, find the package.json and resolve using package exports if available
680
+ let hasModuleSideEffects = () => null;
681
+ let hasPackageEntry = true;
682
+ let packageBrowserField = false;
683
+ let packageInfo;
684
+
685
+ const filter = (pkg, pkgPath) => {
686
+ const info = getPackageInfo({
687
+ cache: packageInfoCache,
688
+ extensions,
689
+ pkg,
690
+ pkgPath,
691
+ mainFields,
692
+ preserveSymlinks,
693
+ useBrowserOverrides,
694
+ rootDir,
695
+ ignoreSideEffectsForRoot
696
+ });
697
+
698
+ ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
699
+
700
+ return info.cachedPkg;
701
+ };
702
+
703
+ const resolveOptions = {
704
+ basedir: baseDir,
705
+ readFile: readCachedFile,
706
+ isFile: isFileCached,
707
+ isDirectory: isDirCached,
708
+ extensions,
709
+ includeCoreModules: false,
710
+ moduleDirectory: moduleDirectories,
711
+ preserveSymlinks,
712
+ packageFilter: filter
713
+ };
714
+
640
715
  const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
641
716
 
642
717
  if (result && result.pkgJson.exports) {
643
- const { pkgJson, pkgJsonPath, pkgPath } = result;
644
- try {
645
- const subpath =
646
- pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
647
- const pkgURL = pathToFileURL(`${pkgPath}/`);
648
-
649
- const context = {
650
- importer,
651
- importSpecifier,
652
- moduleDirs: moduleDirectories,
653
- pkgURL,
654
- pkgJsonPath,
655
- conditions: exportConditions
718
+ const { pkgJson, pkgJsonPath } = result;
719
+ const subpath =
720
+ pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
721
+ const pkgDr = pkgJsonPath.replace('package.json', '');
722
+ const pkgURL = pathToFileURL(pkgDr);
723
+
724
+ const context = {
725
+ importer,
726
+ importSpecifier,
727
+ moduleDirs: moduleDirectories,
728
+ pkgURL,
729
+ pkgJsonPath,
730
+ conditions: exportConditions
731
+ };
732
+ const resolvedPackageExport = await resolvePackageExports(context, subpath, pkgJson.exports);
733
+ const location = fileURLToPath(resolvedPackageExport);
734
+ if (location) {
735
+ return {
736
+ location: preserveSymlinks ? location : await resolveSymlink(location),
737
+ hasModuleSideEffects,
738
+ hasPackageEntry,
739
+ packageBrowserField,
740
+ packageInfo
656
741
  };
657
- const resolvedPackageExport = await resolvePackageExports(
658
- context,
659
- subpath,
660
- pkgJson.exports
661
- );
662
- location = fileURLToPath(resolvedPackageExport);
663
- } catch (error) {
664
- if (error instanceof ResolveError) {
665
- return error;
666
- }
667
- throw error;
668
742
  }
669
743
  }
670
744
  }
671
745
 
672
- if (!location) {
673
- // package has no imports or exports, use classic node resolve
674
- try {
675
- location = await resolveImportPath(importSpecifier, resolveOptions);
676
- } catch (error) {
677
- if (error.code !== 'MODULE_NOT_FOUND') {
678
- throw error;
679
- }
680
- return null;
681
- }
682
- }
683
-
684
- if (!preserveSymlinks) {
685
- if (await fileExists(location)) {
686
- location = await realpath(location);
687
- }
688
- }
689
-
690
- return {
691
- location,
692
- hasModuleSideEffects,
693
- hasPackageEntry,
694
- packageBrowserField,
695
- packageInfo
696
- };
746
+ return null;
697
747
  }
698
748
 
699
- // Resolve module specifiers in order. Promise resolves to the first module that resolves
700
- // successfully, or the error that resulted from the last attempted module resolution.
701
- async function resolveImportSpecifiers({
749
+ async function resolveWithClassic({
702
750
  importer,
703
751
  importSpecifierList,
704
752
  exportConditions,
@@ -713,11 +761,9 @@ async function resolveImportSpecifiers({
713
761
  rootDir,
714
762
  ignoreSideEffectsForRoot
715
763
  }) {
716
- let lastResolveError;
717
-
718
764
  for (let i = 0; i < importSpecifierList.length; i++) {
719
765
  // eslint-disable-next-line no-await-in-loop
720
- const result = await resolveId({
766
+ const result = await resolveIdClassic({
721
767
  importer,
722
768
  importSpecifier: importSpecifierList[i],
723
769
  exportConditions,
@@ -733,20 +779,75 @@ async function resolveImportSpecifiers({
733
779
  ignoreSideEffectsForRoot
734
780
  });
735
781
 
736
- if (result instanceof ResolveError) {
737
- lastResolveError = result;
738
- } else if (result) {
782
+ if (result) {
739
783
  return result;
740
784
  }
741
785
  }
742
786
 
743
- if (lastResolveError) {
744
- // only log the last failed resolve error
745
- warn(lastResolveError);
746
- }
747
787
  return null;
748
788
  }
749
789
 
790
+ // Resolves to the module if found or `null`.
791
+ // The first import specificer will first be attempted with the exports algorithm.
792
+ // If this is unsuccesful because export maps are not being used, then all of `importSpecifierList`
793
+ // will be tried with the classic resolution algorithm
794
+ async function resolveImportSpecifiers({
795
+ importer,
796
+ importSpecifierList,
797
+ exportConditions,
798
+ warn,
799
+ packageInfoCache,
800
+ extensions,
801
+ mainFields,
802
+ preserveSymlinks,
803
+ useBrowserOverrides,
804
+ baseDir,
805
+ moduleDirectories,
806
+ rootDir,
807
+ ignoreSideEffectsForRoot
808
+ }) {
809
+ try {
810
+ const exportMapRes = await resolveWithExportMap({
811
+ importer,
812
+ importSpecifier: importSpecifierList[0],
813
+ exportConditions,
814
+ packageInfoCache,
815
+ extensions,
816
+ mainFields,
817
+ preserveSymlinks,
818
+ useBrowserOverrides,
819
+ baseDir,
820
+ moduleDirectories,
821
+ rootDir,
822
+ ignoreSideEffectsForRoot
823
+ });
824
+ if (exportMapRes) return exportMapRes;
825
+ } catch (error) {
826
+ if (error instanceof ResolveError) {
827
+ warn(error);
828
+ return null;
829
+ }
830
+ throw error;
831
+ }
832
+
833
+ // package has no imports or exports, use classic node resolve
834
+ return resolveWithClassic({
835
+ importer,
836
+ importSpecifierList,
837
+ exportConditions,
838
+ warn,
839
+ packageInfoCache,
840
+ extensions,
841
+ mainFields,
842
+ preserveSymlinks,
843
+ useBrowserOverrides,
844
+ baseDir,
845
+ moduleDirectories,
846
+ rootDir,
847
+ ignoreSideEffectsForRoot
848
+ });
849
+ }
850
+
750
851
  function handleDeprecatedOptions(opts) {
751
852
  const warnings = [];
752
853
 
@@ -857,7 +958,7 @@ function nodeResolve(opts = {}) {
857
958
  const browserMapCache = new Map();
858
959
  let preserveSymlinks;
859
960
 
860
- const doResolveId = async (context, importee, importer, opts) => {
961
+ const doResolveId = async (context, importee, importer, custom) => {
861
962
  // strip query params from import
862
963
  const [importPath, params] = importee.split('?');
863
964
  const importSuffix = `${params ? `?${params}` : ''}`;
@@ -873,7 +974,7 @@ function nodeResolve(opts = {}) {
873
974
  return { id: ES6_BROWSER_EMPTY };
874
975
  }
875
976
  const browserImportee =
876
- browser[importee] ||
977
+ (importee[0] !== '.' && browser[importee]) ||
877
978
  browser[resolvedImportee] ||
878
979
  browser[`${resolvedImportee}.js`] ||
879
980
  browser[`${resolvedImportee}.json`];
@@ -906,30 +1007,17 @@ function nodeResolve(opts = {}) {
906
1007
  return false;
907
1008
  }
908
1009
 
909
- const importSpecifierList = [];
1010
+ const importSpecifierList = [importee];
910
1011
 
911
1012
  if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
912
1013
  // For module graph roots (i.e. when importer is undefined), we
913
1014
  // need to handle 'path fragments` like `foo/bar` that are commonly
914
1015
  // found in rollup config files. If importee doesn't look like a
915
1016
  // relative or absolute path, we make it relative and attempt to
916
- // resolve it. If we don't find anything, we try resolving it as we
917
- // got it.
1017
+ // resolve it.
918
1018
  importSpecifierList.push(`./${importee}`);
919
1019
  }
920
1020
 
921
- const importeeIsBuiltin = builtins.has(importee);
922
-
923
- if (importeeIsBuiltin) {
924
- // The `resolve` library will not resolve packages with the same
925
- // name as a node built-in module. If we're resolving something
926
- // that's a builtin, and we don't prefer to find built-ins, we
927
- // first try to look up a local module with that name. If we don't
928
- // find anything, we resolve the builtin which just returns back
929
- // the built-in's name.
930
- importSpecifierList.push(`${importee}/`);
931
- }
932
-
933
1021
  // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
934
1022
  if (importer && importee.endsWith('.js')) {
935
1023
  for (const ext of ['.ts', '.tsx']) {
@@ -939,11 +1027,8 @@ function nodeResolve(opts = {}) {
939
1027
  }
940
1028
  }
941
1029
 
942
- importSpecifierList.push(importee);
943
-
944
1030
  const warn = (...args) => context.warn(...args);
945
- const isRequire =
946
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
1031
+ const isRequire = custom && custom['node-resolve'] && custom['node-resolve'].isRequire;
947
1032
  const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
948
1033
 
949
1034
  if (useBrowserOverrides && !exportConditions.includes('browser'))
@@ -965,6 +1050,7 @@ function nodeResolve(opts = {}) {
965
1050
  ignoreSideEffectsForRoot
966
1051
  });
967
1052
 
1053
+ const importeeIsBuiltin = builtins.has(importee);
968
1054
  const resolved =
969
1055
  importeeIsBuiltin && preferBuiltins
970
1056
  ? {
@@ -1049,7 +1135,7 @@ function nodeResolve(opts = {}) {
1049
1135
  isDirCached.clear();
1050
1136
  },
1051
1137
 
1052
- async resolveId(importee, importer, opts) {
1138
+ async resolveId(importee, importer, resolveOptions) {
1053
1139
  if (importee === ES6_BROWSER_EMPTY) {
1054
1140
  return importee;
1055
1141
  }
@@ -1060,9 +1146,13 @@ function nodeResolve(opts = {}) {
1060
1146
  importer = undefined;
1061
1147
  }
1062
1148
 
1063
- const resolved = await doResolveId(this, importee, importer, opts);
1149
+ const resolved = await doResolveId(this, importee, importer, resolveOptions.custom);
1064
1150
  if (resolved) {
1065
- const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1151
+ const resolvedResolved = await this.resolve(
1152
+ resolved.id,
1153
+ importer,
1154
+ Object.assign({ skipSelf: true }, resolveOptions)
1155
+ );
1066
1156
  const isExternal = !!(resolvedResolved && resolvedResolved.external);
1067
1157
  if (isExternal) {
1068
1158
  return false;
@@ -1084,5 +1174,4 @@ function nodeResolve(opts = {}) {
1084
1174
  };
1085
1175
  }
1086
1176
 
1087
- export default nodeResolve;
1088
- export { DEFAULTS, nodeResolve };
1177
+ export { DEFAULTS, nodeResolve as default, nodeResolve };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rollup/plugin-node-resolve",
3
- "version": "13.0.2",
3
+ "version": "13.0.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -68,7 +68,7 @@
68
68
  "@rollup/plugin-commonjs": "^16.0.0",
69
69
  "@rollup/plugin-json": "^4.1.0",
70
70
  "es5-ext": "^0.10.53",
71
- "rollup": "^2.42.0",
71
+ "rollup": "^2.58.0",
72
72
  "source-map": "^0.7.3",
73
73
  "string-capitalize": "^1.0.1"
74
74
  },