@rollup/plugin-node-resolve 11.2.0 → 13.0.2

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,5 +1,53 @@
1
1
  # @rollup/plugin-node-resolve ChangeLog
2
2
 
3
+ ## v13.0.2
4
+
5
+ _2021-07-15_
6
+
7
+ ### Bugfixes
8
+
9
+ - fix
10
+
11
+ ## v13.0.1
12
+
13
+ _2021-07-15_
14
+
15
+ ### Updates
16
+
17
+ - docs: Document how to get Node.js exports resolution (#884)
18
+
19
+ ## v13.0.0
20
+
21
+ _2021-05-04_
22
+
23
+ ### Breaking Changes
24
+
25
+ - fix!: mark module as external if resolved module is external (#799)
26
+
27
+ ### Features
28
+
29
+ - feat: Follow up to #843, refining exports and browser field interaction (#866)
30
+
31
+ ## v12.0.0
32
+
33
+ _2021-05-04_
34
+
35
+ ### Breaking Changes
36
+
37
+ - fix!: mark module as external if resolved module is external (#799)
38
+
39
+ ### Features
40
+
41
+ - feat: Follow up to #843, refining exports and browser field interaction (#866)
42
+
43
+ ## v11.2.1
44
+
45
+ _2021-03-26_
46
+
47
+ ### Bugfixes
48
+
49
+ - fix: fs.exists is incorrectly promisified (#835)
50
+
3
51
  ## v11.2.0
4
52
 
5
53
  _2021-02-14_
@@ -403,4 +451,4 @@ This release caches reading/statting of files, to improve speed.
403
451
 
404
452
  ## 0.1.0
405
453
 
406
- - First release
454
+ - First release
package/README.md CHANGED
@@ -59,12 +59,14 @@ When using `@rollup/plugin-commonjs` v16 or higher, this plugin will use the `['
59
59
 
60
60
  Setting this option will add extra conditions on top of the default conditions. See https://nodejs.org/api/packages.html#packages_conditional_exports for more information.
61
61
 
62
+ In order to get the [resolution behavior of Node.js](https://nodejs.org/api/packages.html#packages_conditional_exports), set this to `['node']`.
63
+
62
64
  ### `browser`
63
65
 
64
66
  Type: `Boolean`<br>
65
67
  Default: `false`
66
68
 
67
- If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.
69
+ If `true`, instructs the plugin to use the browser module resolutions in `package.json` and adds `'browser'` to `exportConditions` if it is not present so browser conditionals in `exports` are applied. If `false`, any browser properties in package files will be ignored. Alternatively, a value of `'browser'` can be added to both the `mainFields` and `exportConditions` options, however this option takes precedence over `mainFields`.
68
70
 
69
71
  > This option does not work when a package is using [package entrypoints](https://nodejs.org/api/packages.html#packages_package_entry_points)
70
72
 
package/dist/cjs/index.js CHANGED
@@ -21,11 +21,20 @@ 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
- const exists = util.promisify(fs__default['default'].exists);
25
- const readFile = util.promisify(fs__default['default'].readFile);
24
+ util.promisify(fs__default['default'].access);
25
+ const readFile$1 = util.promisify(fs__default['default'].readFile);
26
26
  const realpath = util.promisify(fs__default['default'].realpath);
27
27
  const stat = util.promisify(fs__default['default'].stat);
28
28
 
29
+ async function fileExists(filePath) {
30
+ try {
31
+ const res = await stat(filePath);
32
+ return res.isFile();
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+
29
38
  const onError = (error) => {
30
39
  if (error.code === 'ENOENT') {
31
40
  return false;
@@ -78,7 +87,7 @@ const isFileCached = makeCache(async (file) => {
78
87
  }
79
88
  });
80
89
 
81
- const readCachedFile = makeCache(readFile);
90
+ const readCachedFile = makeCache(readFile$1);
82
91
 
83
92
  // returns the imported package name for bare module imports
84
93
  function getPackageName(id) {
@@ -243,8 +252,6 @@ function normalizeInput(input) {
243
252
 
244
253
  /* eslint-disable no-await-in-loop */
245
254
 
246
- const fileExists = util.promisify(fs__default['default'].exists);
247
-
248
255
  function isModuleDir(current, moduleDirs) {
249
256
  return moduleDirs.some((dir) => current.endsWith(dir));
250
257
  }
@@ -339,11 +346,11 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna
339
346
  target.replace(/\*/g, subpath),
340
347
  context.pkgURL.href
341
348
  );
342
- return result ? url.pathToFileURL(result.location) : null;
349
+ return result ? url.pathToFileURL(result.location).href : null;
343
350
  }
344
351
 
345
352
  const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
346
- return result ? url.pathToFileURL(result.location) : null;
353
+ return result ? url.pathToFileURL(result.location).href : null;
347
354
  }
348
355
  throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
349
356
  }
@@ -544,7 +551,7 @@ async function resolvePackageImports({
544
551
  }
545
552
 
546
553
  const resolveImportPath = util.promisify(resolve__default['default']);
547
- const readFile$1 = util.promisify(fs__default['default'].readFile);
554
+ const readFile = util.promisify(fs__default['default'].readFile);
548
555
 
549
556
  async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
550
557
  if (importer) {
@@ -557,8 +564,8 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
557
564
 
558
565
  try {
559
566
  const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
560
- const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
561
- return { pkgJsonPath, pkgJson };
567
+ const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
568
+ return { pkgJsonPath, pkgJson, pkgPath: path.dirname(pkgJsonPath) };
562
569
  } catch (_) {
563
570
  return null;
564
571
  }
@@ -646,12 +653,11 @@ async function resolveId({
646
653
  const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
647
654
 
648
655
  if (result && result.pkgJson.exports) {
649
- const { pkgJson, pkgJsonPath } = result;
656
+ const { pkgJson, pkgJsonPath, pkgPath } = result;
650
657
  try {
651
658
  const subpath =
652
659
  pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
653
- const pkgDr = pkgJsonPath.replace('package.json', '');
654
- const pkgURL = url.pathToFileURL(pkgDr);
660
+ const pkgURL = url.pathToFileURL(`${pkgPath}/`);
655
661
 
656
662
  const context = {
657
663
  importer,
@@ -689,7 +695,7 @@ async function resolveId({
689
695
  }
690
696
 
691
697
  if (!preserveSymlinks) {
692
- if (await exists(location)) {
698
+ if (await fileExists(location)) {
693
699
  location = await realpath(location);
694
700
  }
695
701
  }
@@ -864,203 +870,218 @@ function nodeResolve(opts = {}) {
864
870
  const browserMapCache = new Map();
865
871
  let preserveSymlinks;
866
872
 
867
- return {
868
- name: 'node-resolve',
873
+ const doResolveId = async (context, importee, importer, opts) => {
874
+ // strip query params from import
875
+ const [importPath, params] = importee.split('?');
876
+ const importSuffix = `${params ? `?${params}` : ''}`;
877
+ importee = importPath;
869
878
 
870
- buildStart(options) {
871
- rollupOptions = options;
879
+ const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
872
880
 
873
- for (const warning of warnings) {
874
- this.warn(warning);
881
+ // https://github.com/defunctzombie/package-browser-field-spec
882
+ const browser = browserMapCache.get(importer);
883
+ if (useBrowserOverrides && browser) {
884
+ const resolvedImportee = path.resolve(baseDir, importee);
885
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
886
+ return { id: ES6_BROWSER_EMPTY };
875
887
  }
876
-
877
- ({ preserveSymlinks } = options);
878
- },
879
-
880
- generateBundle() {
881
- readCachedFile.clear();
882
- isFileCached.clear();
883
- isDirCached.clear();
884
- },
885
-
886
- async resolveId(importee, importer, opts) {
887
- if (importee === ES6_BROWSER_EMPTY) {
888
- return importee;
888
+ const browserImportee =
889
+ browser[importee] ||
890
+ browser[resolvedImportee] ||
891
+ browser[`${resolvedImportee}.js`] ||
892
+ browser[`${resolvedImportee}.json`];
893
+ if (browserImportee) {
894
+ importee = browserImportee;
889
895
  }
890
- // ignore IDs with null character, these belong to other plugins
891
- if (/\0/.test(importee)) return null;
896
+ }
892
897
 
893
- if (/\0/.test(importer)) {
894
- importer = undefined;
898
+ const parts = importee.split(/[/\\]/);
899
+ let id = parts.shift();
900
+ let isRelativeImport = false;
901
+
902
+ if (id[0] === '@' && parts.length > 0) {
903
+ // scoped packages
904
+ id += `/${parts.shift()}`;
905
+ } else if (id[0] === '.') {
906
+ // an import relative to the parent dir of the importer
907
+ id = path.resolve(baseDir, importee);
908
+ isRelativeImport = true;
909
+ }
910
+
911
+ if (
912
+ !isRelativeImport &&
913
+ resolveOnly.length &&
914
+ !resolveOnly.some((pattern) => pattern.test(id))
915
+ ) {
916
+ if (normalizeInput(rollupOptions.input).includes(importee)) {
917
+ return null;
895
918
  }
919
+ return false;
920
+ }
896
921
 
897
- // strip query params from import
898
- const [importPath, params] = importee.split('?');
899
- const importSuffix = `${params ? `?${params}` : ''}`;
900
- importee = importPath;
922
+ const importSpecifierList = [];
901
923
 
902
- const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
924
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
925
+ // For module graph roots (i.e. when importer is undefined), we
926
+ // need to handle 'path fragments` like `foo/bar` that are commonly
927
+ // found in rollup config files. If importee doesn't look like a
928
+ // 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.
931
+ importSpecifierList.push(`./${importee}`);
932
+ }
903
933
 
904
- // https://github.com/defunctzombie/package-browser-field-spec
905
- const browser = browserMapCache.get(importer);
906
- if (useBrowserOverrides && browser) {
907
- const resolvedImportee = path.resolve(baseDir, importee);
908
- if (browser[importee] === false || browser[resolvedImportee] === false) {
909
- return ES6_BROWSER_EMPTY;
910
- }
911
- const browserImportee =
912
- browser[importee] ||
913
- browser[resolvedImportee] ||
914
- browser[`${resolvedImportee}.js`] ||
915
- browser[`${resolvedImportee}.json`];
916
- if (browserImportee) {
917
- importee = browserImportee;
918
- }
919
- }
934
+ const importeeIsBuiltin = builtins.has(importee);
920
935
 
921
- const parts = importee.split(/[/\\]/);
922
- let id = parts.shift();
923
- let isRelativeImport = false;
924
-
925
- if (id[0] === '@' && parts.length > 0) {
926
- // scoped packages
927
- id += `/${parts.shift()}`;
928
- } else if (id[0] === '.') {
929
- // an import relative to the parent dir of the importer
930
- id = path.resolve(baseDir, importee);
931
- isRelativeImport = true;
932
- }
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
+ }
933
945
 
934
- if (
935
- !isRelativeImport &&
936
- resolveOnly.length &&
937
- !resolveOnly.some((pattern) => pattern.test(id))
938
- ) {
939
- if (normalizeInput(rollupOptions.input).includes(importee)) {
940
- return null;
946
+ // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
947
+ if (importer && importee.endsWith('.js')) {
948
+ for (const ext of ['.ts', '.tsx']) {
949
+ if (importer.endsWith(ext) && extensions.includes(ext)) {
950
+ importSpecifierList.push(importee.replace(/.js$/, ext));
941
951
  }
942
- return false;
943
952
  }
953
+ }
944
954
 
945
- const importSpecifierList = [];
955
+ importSpecifierList.push(importee);
946
956
 
947
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
948
- // For module graph roots (i.e. when importer is undefined), we
949
- // need to handle 'path fragments` like `foo/bar` that are commonly
950
- // found in rollup config files. If importee doesn't look like a
951
- // relative or absolute path, we make it relative and attempt to
952
- // resolve it. If we don't find anything, we try resolving it as we
953
- // got it.
954
- importSpecifierList.push(`./${importee}`);
955
- }
957
+ const warn = (...args) => context.warn(...args);
958
+ const isRequire =
959
+ opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
960
+ const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
956
961
 
957
- const importeeIsBuiltin = builtins.has(importee);
962
+ if (useBrowserOverrides && !exportConditions.includes('browser'))
963
+ exportConditions.push('browser');
958
964
 
959
- if (importeeIsBuiltin) {
960
- // The `resolve` library will not resolve packages with the same
961
- // name as a node built-in module. If we're resolving something
962
- // that's a builtin, and we don't prefer to find built-ins, we
963
- // first try to look up a local module with that name. If we don't
964
- // find anything, we resolve the builtin which just returns back
965
- // the built-in's name.
966
- importSpecifierList.push(`${importee}/`);
967
- }
965
+ const resolvedWithoutBuiltins = await resolveImportSpecifiers({
966
+ importer,
967
+ importSpecifierList,
968
+ exportConditions,
969
+ warn,
970
+ packageInfoCache,
971
+ extensions,
972
+ mainFields,
973
+ preserveSymlinks,
974
+ useBrowserOverrides,
975
+ baseDir,
976
+ moduleDirectories,
977
+ rootDir,
978
+ ignoreSideEffectsForRoot
979
+ });
968
980
 
969
- // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
970
- if (importer && importee.endsWith('.js')) {
971
- for (const ext of ['.ts', '.tsx']) {
972
- if (importer.endsWith(ext) && extensions.includes(ext)) {
973
- importSpecifierList.push(importee.replace(/.js$/, ext));
981
+ const resolved =
982
+ importeeIsBuiltin && preferBuiltins
983
+ ? {
984
+ packageInfo: undefined,
985
+ hasModuleSideEffects: () => null,
986
+ hasPackageEntry: true,
987
+ packageBrowserField: false
974
988
  }
989
+ : resolvedWithoutBuiltins;
990
+ if (!resolved) {
991
+ return null;
992
+ }
993
+
994
+ const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
995
+ let { location } = resolved;
996
+ if (packageBrowserField) {
997
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
998
+ if (!packageBrowserField[location]) {
999
+ browserMapCache.set(location, packageBrowserField);
1000
+ return { id: ES6_BROWSER_EMPTY };
975
1001
  }
1002
+ location = packageBrowserField[location];
976
1003
  }
1004
+ browserMapCache.set(location, packageBrowserField);
1005
+ }
977
1006
 
978
- importSpecifierList.push(importee);
979
-
980
- const warn = (...args) => this.warn(...args);
981
- const isRequire =
982
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
983
- const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
984
-
985
- const resolvedWithoutBuiltins = await resolveImportSpecifiers({
986
- importer,
987
- importSpecifierList,
988
- exportConditions,
989
- warn,
990
- packageInfoCache,
991
- extensions,
992
- mainFields,
993
- preserveSymlinks,
994
- useBrowserOverrides,
995
- baseDir,
996
- moduleDirectories,
997
- rootDir,
998
- ignoreSideEffectsForRoot
999
- });
1007
+ if (hasPackageEntry && !preserveSymlinks) {
1008
+ const exists = await fileExists(location);
1009
+ if (exists) {
1010
+ location = await realpath(location);
1011
+ }
1012
+ }
1000
1013
 
1001
- const resolved =
1002
- importeeIsBuiltin && preferBuiltins
1003
- ? {
1004
- packageInfo: undefined,
1005
- hasModuleSideEffects: () => null,
1006
- hasPackageEntry: true,
1007
- packageBrowserField: false
1008
- }
1009
- : resolvedWithoutBuiltins;
1010
- if (!resolved) {
1014
+ idToPackageInfo.set(location, packageInfo);
1015
+
1016
+ if (hasPackageEntry) {
1017
+ if (importeeIsBuiltin && preferBuiltins) {
1018
+ if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1019
+ context.warn(
1020
+ `preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
1021
+ );
1022
+ }
1023
+ return false;
1024
+ } else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
1011
1025
  return null;
1012
1026
  }
1027
+ }
1013
1028
 
1014
- const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
1015
- let { location } = resolved;
1016
- if (packageBrowserField) {
1017
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
1018
- if (!packageBrowserField[location]) {
1019
- browserMapCache.set(location, packageBrowserField);
1020
- return ES6_BROWSER_EMPTY;
1021
- }
1022
- location = packageBrowserField[location];
1023
- }
1024
- browserMapCache.set(location, packageBrowserField);
1029
+ if (options.modulesOnly && (await fileExists(location))) {
1030
+ const code = await readFile$1(location, 'utf-8');
1031
+ if (isModule__default['default'](code)) {
1032
+ return {
1033
+ id: `${location}${importSuffix}`,
1034
+ moduleSideEffects: hasModuleSideEffects(location)
1035
+ };
1025
1036
  }
1037
+ return null;
1038
+ }
1039
+ const result = {
1040
+ id: `${location}${importSuffix}`,
1041
+ moduleSideEffects: hasModuleSideEffects(location)
1042
+ };
1043
+ return result;
1044
+ };
1026
1045
 
1027
- if (hasPackageEntry && !preserveSymlinks) {
1028
- const fileExists = await exists(location);
1029
- if (fileExists) {
1030
- location = await realpath(location);
1031
- }
1046
+ return {
1047
+ name: 'node-resolve',
1048
+
1049
+ buildStart(options) {
1050
+ rollupOptions = options;
1051
+
1052
+ for (const warning of warnings) {
1053
+ this.warn(warning);
1032
1054
  }
1033
1055
 
1034
- idToPackageInfo.set(location, packageInfo);
1056
+ ({ preserveSymlinks } = options);
1057
+ },
1035
1058
 
1036
- if (hasPackageEntry) {
1037
- if (importeeIsBuiltin && preferBuiltins) {
1038
- if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1039
- this.warn(
1040
- `preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
1041
- );
1042
- }
1043
- return false;
1044
- } else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
1045
- return null;
1046
- }
1059
+ generateBundle() {
1060
+ readCachedFile.clear();
1061
+ isFileCached.clear();
1062
+ isDirCached.clear();
1063
+ },
1064
+
1065
+ async resolveId(importee, importer, opts) {
1066
+ if (importee === ES6_BROWSER_EMPTY) {
1067
+ return importee;
1068
+ }
1069
+ // ignore IDs with null character, these belong to other plugins
1070
+ if (/\0/.test(importee)) return null;
1071
+
1072
+ if (/\0/.test(importer)) {
1073
+ importer = undefined;
1047
1074
  }
1048
1075
 
1049
- if (options.modulesOnly && (await exists(location))) {
1050
- const code = await readFile(location, 'utf-8');
1051
- if (isModule__default['default'](code)) {
1052
- return {
1053
- id: `${location}${importSuffix}`,
1054
- moduleSideEffects: hasModuleSideEffects(location)
1055
- };
1076
+ const resolved = await doResolveId(this, importee, importer, opts);
1077
+ if (resolved) {
1078
+ const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1079
+ const isExternal = !!(resolvedResolved && resolvedResolved.external);
1080
+ if (isExternal) {
1081
+ return false;
1056
1082
  }
1057
- return null;
1058
1083
  }
1059
- const result = {
1060
- id: `${location}${importSuffix}`,
1061
- moduleSideEffects: hasModuleSideEffects(location)
1062
- };
1063
- return result;
1084
+ return resolved;
1064
1085
  },
1065
1086
 
1066
1087
  load(importee) {
package/dist/es/index.js CHANGED
@@ -8,11 +8,20 @@ import { pathToFileURL, fileURLToPath } from 'url';
8
8
  import resolve$1 from 'resolve';
9
9
  import { createFilter } from '@rollup/pluginutils';
10
10
 
11
- const exists = promisify(fs.exists);
12
- const readFile = promisify(fs.readFile);
11
+ promisify(fs.access);
12
+ const readFile$1 = promisify(fs.readFile);
13
13
  const realpath = promisify(fs.realpath);
14
14
  const stat = promisify(fs.stat);
15
15
 
16
+ async function fileExists(filePath) {
17
+ try {
18
+ const res = await stat(filePath);
19
+ return res.isFile();
20
+ } catch {
21
+ return false;
22
+ }
23
+ }
24
+
16
25
  const onError = (error) => {
17
26
  if (error.code === 'ENOENT') {
18
27
  return false;
@@ -65,7 +74,7 @@ const isFileCached = makeCache(async (file) => {
65
74
  }
66
75
  });
67
76
 
68
- const readCachedFile = makeCache(readFile);
77
+ const readCachedFile = makeCache(readFile$1);
69
78
 
70
79
  // returns the imported package name for bare module imports
71
80
  function getPackageName(id) {
@@ -230,8 +239,6 @@ function normalizeInput(input) {
230
239
 
231
240
  /* eslint-disable no-await-in-loop */
232
241
 
233
- const fileExists = promisify(fs.exists);
234
-
235
242
  function isModuleDir(current, moduleDirs) {
236
243
  return moduleDirs.some((dir) => current.endsWith(dir));
237
244
  }
@@ -326,11 +333,11 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna
326
333
  target.replace(/\*/g, subpath),
327
334
  context.pkgURL.href
328
335
  );
329
- return result ? pathToFileURL(result.location) : null;
336
+ return result ? pathToFileURL(result.location).href : null;
330
337
  }
331
338
 
332
339
  const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
333
- return result ? pathToFileURL(result.location) : null;
340
+ return result ? pathToFileURL(result.location).href : null;
334
341
  }
335
342
  throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
336
343
  }
@@ -531,7 +538,7 @@ async function resolvePackageImports({
531
538
  }
532
539
 
533
540
  const resolveImportPath = promisify(resolve$1);
534
- const readFile$1 = promisify(fs.readFile);
541
+ const readFile = promisify(fs.readFile);
535
542
 
536
543
  async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
537
544
  if (importer) {
@@ -544,8 +551,8 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
544
551
 
545
552
  try {
546
553
  const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
547
- const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
548
- return { pkgJsonPath, pkgJson };
554
+ const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
555
+ return { pkgJsonPath, pkgJson, pkgPath: dirname(pkgJsonPath) };
549
556
  } catch (_) {
550
557
  return null;
551
558
  }
@@ -633,12 +640,11 @@ async function resolveId({
633
640
  const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
634
641
 
635
642
  if (result && result.pkgJson.exports) {
636
- const { pkgJson, pkgJsonPath } = result;
643
+ const { pkgJson, pkgJsonPath, pkgPath } = result;
637
644
  try {
638
645
  const subpath =
639
646
  pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
640
- const pkgDr = pkgJsonPath.replace('package.json', '');
641
- const pkgURL = pathToFileURL(pkgDr);
647
+ const pkgURL = pathToFileURL(`${pkgPath}/`);
642
648
 
643
649
  const context = {
644
650
  importer,
@@ -676,7 +682,7 @@ async function resolveId({
676
682
  }
677
683
 
678
684
  if (!preserveSymlinks) {
679
- if (await exists(location)) {
685
+ if (await fileExists(location)) {
680
686
  location = await realpath(location);
681
687
  }
682
688
  }
@@ -851,203 +857,218 @@ function nodeResolve(opts = {}) {
851
857
  const browserMapCache = new Map();
852
858
  let preserveSymlinks;
853
859
 
854
- return {
855
- name: 'node-resolve',
860
+ const doResolveId = async (context, importee, importer, opts) => {
861
+ // strip query params from import
862
+ const [importPath, params] = importee.split('?');
863
+ const importSuffix = `${params ? `?${params}` : ''}`;
864
+ importee = importPath;
856
865
 
857
- buildStart(options) {
858
- rollupOptions = options;
866
+ const baseDir = !importer || dedupe(importee) ? rootDir : dirname(importer);
859
867
 
860
- for (const warning of warnings) {
861
- this.warn(warning);
868
+ // https://github.com/defunctzombie/package-browser-field-spec
869
+ const browser = browserMapCache.get(importer);
870
+ if (useBrowserOverrides && browser) {
871
+ const resolvedImportee = resolve(baseDir, importee);
872
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
873
+ return { id: ES6_BROWSER_EMPTY };
862
874
  }
863
-
864
- ({ preserveSymlinks } = options);
865
- },
866
-
867
- generateBundle() {
868
- readCachedFile.clear();
869
- isFileCached.clear();
870
- isDirCached.clear();
871
- },
872
-
873
- async resolveId(importee, importer, opts) {
874
- if (importee === ES6_BROWSER_EMPTY) {
875
- return importee;
875
+ const browserImportee =
876
+ browser[importee] ||
877
+ browser[resolvedImportee] ||
878
+ browser[`${resolvedImportee}.js`] ||
879
+ browser[`${resolvedImportee}.json`];
880
+ if (browserImportee) {
881
+ importee = browserImportee;
876
882
  }
877
- // ignore IDs with null character, these belong to other plugins
878
- if (/\0/.test(importee)) return null;
883
+ }
879
884
 
880
- if (/\0/.test(importer)) {
881
- importer = undefined;
885
+ const parts = importee.split(/[/\\]/);
886
+ let id = parts.shift();
887
+ let isRelativeImport = false;
888
+
889
+ if (id[0] === '@' && parts.length > 0) {
890
+ // scoped packages
891
+ id += `/${parts.shift()}`;
892
+ } else if (id[0] === '.') {
893
+ // an import relative to the parent dir of the importer
894
+ id = resolve(baseDir, importee);
895
+ isRelativeImport = true;
896
+ }
897
+
898
+ if (
899
+ !isRelativeImport &&
900
+ resolveOnly.length &&
901
+ !resolveOnly.some((pattern) => pattern.test(id))
902
+ ) {
903
+ if (normalizeInput(rollupOptions.input).includes(importee)) {
904
+ return null;
882
905
  }
906
+ return false;
907
+ }
883
908
 
884
- // strip query params from import
885
- const [importPath, params] = importee.split('?');
886
- const importSuffix = `${params ? `?${params}` : ''}`;
887
- importee = importPath;
909
+ const importSpecifierList = [];
888
910
 
889
- const baseDir = !importer || dedupe(importee) ? rootDir : dirname(importer);
911
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
912
+ // For module graph roots (i.e. when importer is undefined), we
913
+ // need to handle 'path fragments` like `foo/bar` that are commonly
914
+ // found in rollup config files. If importee doesn't look like a
915
+ // 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.
918
+ importSpecifierList.push(`./${importee}`);
919
+ }
890
920
 
891
- // https://github.com/defunctzombie/package-browser-field-spec
892
- const browser = browserMapCache.get(importer);
893
- if (useBrowserOverrides && browser) {
894
- const resolvedImportee = resolve(baseDir, importee);
895
- if (browser[importee] === false || browser[resolvedImportee] === false) {
896
- return ES6_BROWSER_EMPTY;
897
- }
898
- const browserImportee =
899
- browser[importee] ||
900
- browser[resolvedImportee] ||
901
- browser[`${resolvedImportee}.js`] ||
902
- browser[`${resolvedImportee}.json`];
903
- if (browserImportee) {
904
- importee = browserImportee;
905
- }
906
- }
921
+ const importeeIsBuiltin = builtins.has(importee);
907
922
 
908
- const parts = importee.split(/[/\\]/);
909
- let id = parts.shift();
910
- let isRelativeImport = false;
911
-
912
- if (id[0] === '@' && parts.length > 0) {
913
- // scoped packages
914
- id += `/${parts.shift()}`;
915
- } else if (id[0] === '.') {
916
- // an import relative to the parent dir of the importer
917
- id = resolve(baseDir, importee);
918
- isRelativeImport = true;
919
- }
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
+ }
920
932
 
921
- if (
922
- !isRelativeImport &&
923
- resolveOnly.length &&
924
- !resolveOnly.some((pattern) => pattern.test(id))
925
- ) {
926
- if (normalizeInput(rollupOptions.input).includes(importee)) {
927
- return null;
933
+ // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
934
+ if (importer && importee.endsWith('.js')) {
935
+ for (const ext of ['.ts', '.tsx']) {
936
+ if (importer.endsWith(ext) && extensions.includes(ext)) {
937
+ importSpecifierList.push(importee.replace(/.js$/, ext));
928
938
  }
929
- return false;
930
939
  }
940
+ }
931
941
 
932
- const importSpecifierList = [];
942
+ importSpecifierList.push(importee);
933
943
 
934
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
935
- // For module graph roots (i.e. when importer is undefined), we
936
- // need to handle 'path fragments` like `foo/bar` that are commonly
937
- // found in rollup config files. If importee doesn't look like a
938
- // relative or absolute path, we make it relative and attempt to
939
- // resolve it. If we don't find anything, we try resolving it as we
940
- // got it.
941
- importSpecifierList.push(`./${importee}`);
942
- }
944
+ const warn = (...args) => context.warn(...args);
945
+ const isRequire =
946
+ opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
947
+ const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
943
948
 
944
- const importeeIsBuiltin = builtins.has(importee);
949
+ if (useBrowserOverrides && !exportConditions.includes('browser'))
950
+ exportConditions.push('browser');
945
951
 
946
- if (importeeIsBuiltin) {
947
- // The `resolve` library will not resolve packages with the same
948
- // name as a node built-in module. If we're resolving something
949
- // that's a builtin, and we don't prefer to find built-ins, we
950
- // first try to look up a local module with that name. If we don't
951
- // find anything, we resolve the builtin which just returns back
952
- // the built-in's name.
953
- importSpecifierList.push(`${importee}/`);
954
- }
952
+ const resolvedWithoutBuiltins = await resolveImportSpecifiers({
953
+ importer,
954
+ importSpecifierList,
955
+ exportConditions,
956
+ warn,
957
+ packageInfoCache,
958
+ extensions,
959
+ mainFields,
960
+ preserveSymlinks,
961
+ useBrowserOverrides,
962
+ baseDir,
963
+ moduleDirectories,
964
+ rootDir,
965
+ ignoreSideEffectsForRoot
966
+ });
955
967
 
956
- // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
957
- if (importer && importee.endsWith('.js')) {
958
- for (const ext of ['.ts', '.tsx']) {
959
- if (importer.endsWith(ext) && extensions.includes(ext)) {
960
- importSpecifierList.push(importee.replace(/.js$/, ext));
968
+ const resolved =
969
+ importeeIsBuiltin && preferBuiltins
970
+ ? {
971
+ packageInfo: undefined,
972
+ hasModuleSideEffects: () => null,
973
+ hasPackageEntry: true,
974
+ packageBrowserField: false
961
975
  }
976
+ : resolvedWithoutBuiltins;
977
+ if (!resolved) {
978
+ return null;
979
+ }
980
+
981
+ const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
982
+ let { location } = resolved;
983
+ if (packageBrowserField) {
984
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
985
+ if (!packageBrowserField[location]) {
986
+ browserMapCache.set(location, packageBrowserField);
987
+ return { id: ES6_BROWSER_EMPTY };
962
988
  }
989
+ location = packageBrowserField[location];
963
990
  }
991
+ browserMapCache.set(location, packageBrowserField);
992
+ }
964
993
 
965
- importSpecifierList.push(importee);
966
-
967
- const warn = (...args) => this.warn(...args);
968
- const isRequire =
969
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
970
- const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
971
-
972
- const resolvedWithoutBuiltins = await resolveImportSpecifiers({
973
- importer,
974
- importSpecifierList,
975
- exportConditions,
976
- warn,
977
- packageInfoCache,
978
- extensions,
979
- mainFields,
980
- preserveSymlinks,
981
- useBrowserOverrides,
982
- baseDir,
983
- moduleDirectories,
984
- rootDir,
985
- ignoreSideEffectsForRoot
986
- });
994
+ if (hasPackageEntry && !preserveSymlinks) {
995
+ const exists = await fileExists(location);
996
+ if (exists) {
997
+ location = await realpath(location);
998
+ }
999
+ }
987
1000
 
988
- const resolved =
989
- importeeIsBuiltin && preferBuiltins
990
- ? {
991
- packageInfo: undefined,
992
- hasModuleSideEffects: () => null,
993
- hasPackageEntry: true,
994
- packageBrowserField: false
995
- }
996
- : resolvedWithoutBuiltins;
997
- if (!resolved) {
1001
+ idToPackageInfo.set(location, packageInfo);
1002
+
1003
+ if (hasPackageEntry) {
1004
+ if (importeeIsBuiltin && preferBuiltins) {
1005
+ if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1006
+ context.warn(
1007
+ `preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
1008
+ );
1009
+ }
1010
+ return false;
1011
+ } else if (jail && location.indexOf(normalize(jail.trim(sep))) !== 0) {
998
1012
  return null;
999
1013
  }
1014
+ }
1000
1015
 
1001
- const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
1002
- let { location } = resolved;
1003
- if (packageBrowserField) {
1004
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
1005
- if (!packageBrowserField[location]) {
1006
- browserMapCache.set(location, packageBrowserField);
1007
- return ES6_BROWSER_EMPTY;
1008
- }
1009
- location = packageBrowserField[location];
1010
- }
1011
- browserMapCache.set(location, packageBrowserField);
1016
+ if (options.modulesOnly && (await fileExists(location))) {
1017
+ const code = await readFile$1(location, 'utf-8');
1018
+ if (isModule(code)) {
1019
+ return {
1020
+ id: `${location}${importSuffix}`,
1021
+ moduleSideEffects: hasModuleSideEffects(location)
1022
+ };
1012
1023
  }
1024
+ return null;
1025
+ }
1026
+ const result = {
1027
+ id: `${location}${importSuffix}`,
1028
+ moduleSideEffects: hasModuleSideEffects(location)
1029
+ };
1030
+ return result;
1031
+ };
1013
1032
 
1014
- if (hasPackageEntry && !preserveSymlinks) {
1015
- const fileExists = await exists(location);
1016
- if (fileExists) {
1017
- location = await realpath(location);
1018
- }
1033
+ return {
1034
+ name: 'node-resolve',
1035
+
1036
+ buildStart(options) {
1037
+ rollupOptions = options;
1038
+
1039
+ for (const warning of warnings) {
1040
+ this.warn(warning);
1019
1041
  }
1020
1042
 
1021
- idToPackageInfo.set(location, packageInfo);
1043
+ ({ preserveSymlinks } = options);
1044
+ },
1022
1045
 
1023
- if (hasPackageEntry) {
1024
- if (importeeIsBuiltin && preferBuiltins) {
1025
- if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1026
- this.warn(
1027
- `preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
1028
- );
1029
- }
1030
- return false;
1031
- } else if (jail && location.indexOf(normalize(jail.trim(sep))) !== 0) {
1032
- return null;
1033
- }
1046
+ generateBundle() {
1047
+ readCachedFile.clear();
1048
+ isFileCached.clear();
1049
+ isDirCached.clear();
1050
+ },
1051
+
1052
+ async resolveId(importee, importer, opts) {
1053
+ if (importee === ES6_BROWSER_EMPTY) {
1054
+ return importee;
1055
+ }
1056
+ // ignore IDs with null character, these belong to other plugins
1057
+ if (/\0/.test(importee)) return null;
1058
+
1059
+ if (/\0/.test(importer)) {
1060
+ importer = undefined;
1034
1061
  }
1035
1062
 
1036
- if (options.modulesOnly && (await exists(location))) {
1037
- const code = await readFile(location, 'utf-8');
1038
- if (isModule(code)) {
1039
- return {
1040
- id: `${location}${importSuffix}`,
1041
- moduleSideEffects: hasModuleSideEffects(location)
1042
- };
1063
+ const resolved = await doResolveId(this, importee, importer, opts);
1064
+ if (resolved) {
1065
+ const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1066
+ const isExternal = !!(resolvedResolved && resolvedResolved.external);
1067
+ if (isExternal) {
1068
+ return false;
1043
1069
  }
1044
- return null;
1045
1070
  }
1046
- const result = {
1047
- id: `${location}${importSuffix}`,
1048
- moduleSideEffects: hasModuleSideEffects(location)
1049
- };
1050
- return result;
1071
+ return resolved;
1051
1072
  },
1052
1073
 
1053
1074
  load(importee) {
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@rollup/plugin-node-resolve",
3
- "version": "11.2.0",
3
+ "version": "13.0.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
7
  "description": "Locate and bundle third-party dependencies in node_modules",
8
8
  "license": "MIT",
9
- "repository": "rollup/plugins",
9
+ "repository": {
10
+ "url": "rollup/plugins",
11
+ "directory": "packages/node-resolve"
12
+ },
10
13
  "author": "Rich Harris <richard.a.harris@gmail.com>",
11
14
  "homepage": "https://github.com/rollup/plugins/tree/master/packages/node-resolve/#readme",
12
15
  "bugs": "https://github.com/rollup/plugins/issues",
@@ -22,19 +25,16 @@
22
25
  },
23
26
  "scripts": {
24
27
  "build": "rollup -c",
25
- "ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
26
- "ci:lint": "pnpm run build && pnpm run lint",
28
+ "ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
29
+ "ci:lint": "pnpm build && pnpm lint",
27
30
  "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
28
- "ci:test": "pnpm run test -- --verbose && pnpm run test:ts",
29
- "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
30
- "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md",
31
- "lint:js": "eslint --fix --cache src test types --ext .js,.ts",
32
- "lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
31
+ "ci:test": "pnpm test -- --verbose && pnpm test:ts",
33
32
  "prebuild": "del-cli dist",
34
- "prepare": "pnpm run build",
35
- "prepublishOnly": "pnpm run lint && pnpm run test && pnpm run test:ts",
36
- "pretest": "pnpm run build",
37
- "test": "ava",
33
+ "prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
34
+ "prerelease": "pnpm build",
35
+ "pretest": "pnpm build",
36
+ "release": "pnpm plugin:release --workspace-root -- --pkg $npm_package_name",
37
+ "test": "ava && pnpm test:ts",
38
38
  "test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
39
39
  },
40
40
  "files": [
@@ -51,7 +51,7 @@
51
51
  "modules"
52
52
  ],
53
53
  "peerDependencies": {
54
- "rollup": "^1.20.0||^2.0.0"
54
+ "rollup": "^2.42.0"
55
55
  },
56
56
  "dependencies": {
57
57
  "@rollup/pluginutils": "^3.1.0",
@@ -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.23.0",
71
+ "rollup": "^2.42.0",
72
72
  "source-map": "^0.7.3",
73
73
  "string-capitalize": "^1.0.1"
74
74
  },