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