@rollup/plugin-node-resolve 11.1.0 → 13.0.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,5 +1,62 @@
1
1
  # @rollup/plugin-node-resolve ChangeLog
2
2
 
3
+ ## v13.0.0
4
+
5
+ _2021-05-04_
6
+
7
+ ### Breaking Changes
8
+
9
+ - fix!: mark module as external if resolved module is external (#799)
10
+
11
+ ### Features
12
+
13
+ - feat: Follow up to #843, refining exports and browser field interaction (#866)
14
+
15
+ ## v12.0.0
16
+
17
+ _2021-05-04_
18
+
19
+ ### Breaking Changes
20
+
21
+ - fix!: mark module as external if resolved module is external (#799)
22
+
23
+ ### Features
24
+
25
+ - feat: Follow up to #843, refining exports and browser field interaction (#866)
26
+
27
+ ## v11.2.1
28
+
29
+ _2021-03-26_
30
+
31
+ ### Bugfixes
32
+
33
+ - fix: fs.exists is incorrectly promisified (#835)
34
+
35
+ ## v11.2.0
36
+
37
+ _2021-02-14_
38
+
39
+ ### Features
40
+
41
+ - feat: add `ignoreSideEffectsForRoot` option (#694)
42
+
43
+ ### Updates
44
+
45
+ - chore: mark `url` as an external and throw on warning (#783)
46
+ - docs: clearer "Resolving Built-Ins" doc (#782)
47
+
48
+ ## v11.1.1
49
+
50
+ _2021-01-29_
51
+
52
+ ### Bugfixes
53
+
54
+ - fix: only log last resolve error (#750)
55
+
56
+ ### Updates
57
+
58
+ - docs: add clarification on the order of package entrypoints (#768)
59
+
3
60
  ## v11.1.0
4
61
 
5
62
  _2021-01-15_
package/README.md CHANGED
@@ -44,7 +44,7 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma
44
44
 
45
45
  ## Package entrypoints
46
46
 
47
- This plugin supports the package entrypoints feature from node js, specified in the `exports` or `imports` field of a package. Check the [official documentation](https://nodejs.org/api/packages.html#packages_package_entry_points) for more information on how this works.
47
+ This plugin supports the package entrypoints feature from node js, specified in the `exports` or `imports` field of a package. Check the [official documentation](https://nodejs.org/api/packages.html#packages_package_entry_points) for more information on how this works. This is the default behavior. In the abscence of these fields, the fields in `mainFields` will be the ones to be used.
48
48
 
49
49
  ## Options
50
50
 
@@ -64,7 +64,7 @@ Setting this option will add extra conditions on top of the default conditions.
64
64
  Type: `Boolean`<br>
65
65
  Default: `false`
66
66
 
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`.
67
+ 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
68
 
69
69
  > This option does not work when a package is using [package entrypoints](https://nodejs.org/api/packages.html#packages_package_entry_points)
70
70
 
@@ -157,6 +157,10 @@ Specifies the root directory from which to resolve modules. Typically used when
157
157
  rootDir: path.join(process.cwd(), '..')
158
158
  ```
159
159
 
160
+ ## `ignoreSideEffectsForRoot`
161
+
162
+ 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.
163
+
160
164
  ## Preserving symlinks
161
165
 
162
166
  This plugin honours the rollup [`preserveSymlinks`](https://rollupjs.org/guide/en/#preservesymlinks) option.
@@ -187,14 +191,17 @@ By default this plugin will prefer built-ins over local modules, marking them as
187
191
 
188
192
  See [`preferBuiltins`](#preferbuiltins).
189
193
 
190
- To provide stubbed versions of Node built-ins, use a plugin like [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) or use [`builtin-modules`](https://github.com/sindresorhus/builtin-modules) with `external`, and set `preferBuiltins` to `false`. e.g.
194
+ To provide stubbed versions of Node built-ins, use a plugin like [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) and set `preferBuiltins` to `false`. e.g.
191
195
 
192
196
  ```js
193
197
  import { nodeResolve } from '@rollup/plugin-node-resolve';
194
- import builtins from 'builtin-modules'
198
+ import nodePolyfills from 'rollup-plugin-node-polyfills';
195
199
  export default ({
196
200
  input: ...,
197
- plugins: [nodeResolve()],
201
+ plugins: [
202
+ nodePolyfills(),
203
+ nodeResolve({ preferBuiltins: false })
204
+ ],
198
205
  external: builtins,
199
206
  output: ...
200
207
  })
package/dist/cjs/index.js CHANGED
@@ -21,10 +21,18 @@ 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
+ const access = 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
+ async function exists(filePath) {
29
+ try {
30
+ await access(filePath);
31
+ return true;
32
+ } catch {
33
+ return false;
34
+ }
35
+ }
28
36
 
29
37
  const onError = (error) => {
30
38
  if (error.code === 'ENOENT') {
@@ -78,7 +86,7 @@ const isFileCached = makeCache(async (file) => {
78
86
  }
79
87
  });
80
88
 
81
- const readCachedFile = makeCache(readFile);
89
+ const readCachedFile = makeCache(readFile$1);
82
90
 
83
91
  // returns the imported package name for bare module imports
84
92
  function getPackageName(id) {
@@ -116,7 +124,16 @@ function getMainFields(options) {
116
124
  }
117
125
 
118
126
  function getPackageInfo(options) {
119
- const { cache, extensions, pkg, mainFields, preserveSymlinks, useBrowserOverrides } = options;
127
+ const {
128
+ cache,
129
+ extensions,
130
+ pkg,
131
+ mainFields,
132
+ preserveSymlinks,
133
+ useBrowserOverrides,
134
+ rootDir,
135
+ ignoreSideEffectsForRoot
136
+ } = options;
120
137
  let { pkgPath } = options;
121
138
 
122
139
  if (cache.has(pkgPath)) {
@@ -206,13 +223,15 @@ function getPackageInfo(options) {
206
223
  packageInfo.browserMappedMain = false;
207
224
  }
208
225
 
209
- const packageSideEffects = pkg.sideEffects;
210
- if (typeof packageSideEffects === 'boolean') {
211
- internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
212
- } else if (Array.isArray(packageSideEffects)) {
213
- internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
214
- resolve: pkgRoot
215
- });
226
+ if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
227
+ const packageSideEffects = pkg.sideEffects;
228
+ if (typeof packageSideEffects === 'boolean') {
229
+ internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
230
+ } else if (Array.isArray(packageSideEffects)) {
231
+ internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
232
+ resolve: pkgRoot
233
+ });
234
+ }
216
235
  }
217
236
 
218
237
  cache.set(pkgPath, internalPackageInfo);
@@ -328,11 +347,11 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna
328
347
  target.replace(/\*/g, subpath),
329
348
  context.pkgURL.href
330
349
  );
331
- return result ? url.pathToFileURL(result.location) : null;
350
+ return result ? url.pathToFileURL(result.location).href : null;
332
351
  }
333
352
 
334
353
  const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
335
- return result ? url.pathToFileURL(result.location) : null;
354
+ return result ? url.pathToFileURL(result.location).href : null;
336
355
  }
337
356
  throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
338
357
  }
@@ -533,7 +552,7 @@ async function resolvePackageImports({
533
552
  }
534
553
 
535
554
  const resolveImportPath = util.promisify(resolve__default['default']);
536
- const readFile$1 = util.promisify(fs__default['default'].readFile);
555
+ const readFile = util.promisify(fs__default['default'].readFile);
537
556
 
538
557
  async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
539
558
  if (importer) {
@@ -546,7 +565,7 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
546
565
 
547
566
  try {
548
567
  const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
549
- const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
568
+ const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
550
569
  return { pkgJsonPath, pkgJson };
551
570
  } catch (_) {
552
571
  return null;
@@ -564,7 +583,9 @@ async function resolveId({
564
583
  preserveSymlinks,
565
584
  useBrowserOverrides,
566
585
  baseDir,
567
- moduleDirectories
586
+ moduleDirectories,
587
+ rootDir,
588
+ ignoreSideEffectsForRoot
568
589
  }) {
569
590
  let hasModuleSideEffects = () => null;
570
591
  let hasPackageEntry = true;
@@ -579,7 +600,9 @@ async function resolveId({
579
600
  pkgPath,
580
601
  mainFields,
581
602
  preserveSymlinks,
582
- useBrowserOverrides
603
+ useBrowserOverrides,
604
+ rootDir,
605
+ ignoreSideEffectsForRoot
583
606
  });
584
607
 
585
608
  ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
@@ -653,11 +676,10 @@ async function resolveId({
653
676
  );
654
677
  location = url.fileURLToPath(resolvedPackageExport);
655
678
  } catch (error) {
656
- if (!(error instanceof ResolveError)) {
657
- throw error;
679
+ if (error instanceof ResolveError) {
680
+ return error;
658
681
  }
659
- warn(error);
660
- return null;
682
+ throw error;
661
683
  }
662
684
  }
663
685
  }
@@ -702,11 +724,15 @@ async function resolveImportSpecifiers({
702
724
  preserveSymlinks,
703
725
  useBrowserOverrides,
704
726
  baseDir,
705
- moduleDirectories
727
+ moduleDirectories,
728
+ rootDir,
729
+ ignoreSideEffectsForRoot
706
730
  }) {
731
+ let lastResolveError;
732
+
707
733
  for (let i = 0; i < importSpecifierList.length; i++) {
708
734
  // eslint-disable-next-line no-await-in-loop
709
- const resolved = await resolveId({
735
+ const result = await resolveId({
710
736
  importer,
711
737
  importSpecifier: importSpecifierList[i],
712
738
  exportConditions,
@@ -717,12 +743,22 @@ async function resolveImportSpecifiers({
717
743
  preserveSymlinks,
718
744
  useBrowserOverrides,
719
745
  baseDir,
720
- moduleDirectories
746
+ moduleDirectories,
747
+ rootDir,
748
+ ignoreSideEffectsForRoot
721
749
  });
722
- if (resolved) {
723
- return resolved;
750
+
751
+ if (result instanceof ResolveError) {
752
+ lastResolveError = result;
753
+ } else if (result) {
754
+ return result;
724
755
  }
725
756
  }
757
+
758
+ if (lastResolveError) {
759
+ // only log the last failed resolve error
760
+ warn(lastResolveError);
761
+ }
726
762
  return null;
727
763
  }
728
764
 
@@ -798,7 +834,8 @@ const defaults = {
798
834
  // which deploy both ESM .mjs and CommonJS .js files as ESM.
799
835
  extensions: ['.mjs', '.js', '.json', '.node'],
800
836
  resolveOnly: [],
801
- moduleDirectories: ['node_modules']
837
+ moduleDirectories: ['node_modules'],
838
+ ignoreSideEffectsForRoot: false
802
839
  };
803
840
  const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
804
841
 
@@ -806,7 +843,7 @@ function nodeResolve(opts = {}) {
806
843
  const { warnings } = handleDeprecatedOptions(opts);
807
844
 
808
845
  const options = { ...defaults, ...opts };
809
- const { extensions, jail, moduleDirectories } = options;
846
+ const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
810
847
  const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
811
848
  const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
812
849
  const packageInfoCache = new Map();
@@ -815,7 +852,7 @@ function nodeResolve(opts = {}) {
815
852
  const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
816
853
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
817
854
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
818
- const rootDir = options.rootDir || process.cwd();
855
+ const rootDir = path.resolve(options.rootDir || process.cwd());
819
856
  let { dedupe } = options;
820
857
  let rollupOptions;
821
858
 
@@ -835,201 +872,218 @@ function nodeResolve(opts = {}) {
835
872
  const browserMapCache = new Map();
836
873
  let preserveSymlinks;
837
874
 
838
- return {
839
- name: 'node-resolve',
875
+ const doResolveId = async (context, importee, importer, opts) => {
876
+ // strip query params from import
877
+ const [importPath, params] = importee.split('?');
878
+ const importSuffix = `${params ? `?${params}` : ''}`;
879
+ importee = importPath;
840
880
 
841
- buildStart(options) {
842
- rollupOptions = options;
881
+ const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
843
882
 
844
- for (const warning of warnings) {
845
- this.warn(warning);
883
+ // https://github.com/defunctzombie/package-browser-field-spec
884
+ const browser = browserMapCache.get(importer);
885
+ if (useBrowserOverrides && browser) {
886
+ const resolvedImportee = path.resolve(baseDir, importee);
887
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
888
+ return { id: ES6_BROWSER_EMPTY };
846
889
  }
847
-
848
- ({ preserveSymlinks } = options);
849
- },
850
-
851
- generateBundle() {
852
- readCachedFile.clear();
853
- isFileCached.clear();
854
- isDirCached.clear();
855
- },
856
-
857
- async resolveId(importee, importer, opts) {
858
- if (importee === ES6_BROWSER_EMPTY) {
859
- return importee;
890
+ const browserImportee =
891
+ browser[importee] ||
892
+ browser[resolvedImportee] ||
893
+ browser[`${resolvedImportee}.js`] ||
894
+ browser[`${resolvedImportee}.json`];
895
+ if (browserImportee) {
896
+ importee = browserImportee;
860
897
  }
861
- // ignore IDs with null character, these belong to other plugins
862
- if (/\0/.test(importee)) return null;
898
+ }
863
899
 
864
- if (/\0/.test(importer)) {
865
- importer = undefined;
900
+ const parts = importee.split(/[/\\]/);
901
+ let id = parts.shift();
902
+ let isRelativeImport = false;
903
+
904
+ if (id[0] === '@' && parts.length > 0) {
905
+ // scoped packages
906
+ id += `/${parts.shift()}`;
907
+ } else if (id[0] === '.') {
908
+ // an import relative to the parent dir of the importer
909
+ id = path.resolve(baseDir, importee);
910
+ isRelativeImport = true;
911
+ }
912
+
913
+ if (
914
+ !isRelativeImport &&
915
+ resolveOnly.length &&
916
+ !resolveOnly.some((pattern) => pattern.test(id))
917
+ ) {
918
+ if (normalizeInput(rollupOptions.input).includes(importee)) {
919
+ return null;
866
920
  }
921
+ return false;
922
+ }
867
923
 
868
- // strip query params from import
869
- const [importPath, params] = importee.split('?');
870
- const importSuffix = `${params ? `?${params}` : ''}`;
871
- importee = importPath;
924
+ const importSpecifierList = [];
872
925
 
873
- const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
926
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
927
+ // For module graph roots (i.e. when importer is undefined), we
928
+ // need to handle 'path fragments` like `foo/bar` that are commonly
929
+ // found in rollup config files. If importee doesn't look like a
930
+ // relative or absolute path, we make it relative and attempt to
931
+ // resolve it. If we don't find anything, we try resolving it as we
932
+ // got it.
933
+ importSpecifierList.push(`./${importee}`);
934
+ }
874
935
 
875
- // https://github.com/defunctzombie/package-browser-field-spec
876
- const browser = browserMapCache.get(importer);
877
- if (useBrowserOverrides && browser) {
878
- const resolvedImportee = path.resolve(baseDir, importee);
879
- if (browser[importee] === false || browser[resolvedImportee] === false) {
880
- return ES6_BROWSER_EMPTY;
881
- }
882
- const browserImportee =
883
- browser[importee] ||
884
- browser[resolvedImportee] ||
885
- browser[`${resolvedImportee}.js`] ||
886
- browser[`${resolvedImportee}.json`];
887
- if (browserImportee) {
888
- importee = browserImportee;
889
- }
890
- }
936
+ const importeeIsBuiltin = builtins.has(importee);
891
937
 
892
- const parts = importee.split(/[/\\]/);
893
- let id = parts.shift();
894
- let isRelativeImport = false;
895
-
896
- if (id[0] === '@' && parts.length > 0) {
897
- // scoped packages
898
- id += `/${parts.shift()}`;
899
- } else if (id[0] === '.') {
900
- // an import relative to the parent dir of the importer
901
- id = path.resolve(baseDir, importee);
902
- isRelativeImport = true;
903
- }
938
+ if (importeeIsBuiltin) {
939
+ // The `resolve` library will not resolve packages with the same
940
+ // name as a node built-in module. If we're resolving something
941
+ // that's a builtin, and we don't prefer to find built-ins, we
942
+ // first try to look up a local module with that name. If we don't
943
+ // find anything, we resolve the builtin which just returns back
944
+ // the built-in's name.
945
+ importSpecifierList.push(`${importee}/`);
946
+ }
904
947
 
905
- if (
906
- !isRelativeImport &&
907
- resolveOnly.length &&
908
- !resolveOnly.some((pattern) => pattern.test(id))
909
- ) {
910
- if (normalizeInput(rollupOptions.input).includes(importee)) {
911
- return null;
948
+ // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
949
+ if (importer && importee.endsWith('.js')) {
950
+ for (const ext of ['.ts', '.tsx']) {
951
+ if (importer.endsWith(ext) && extensions.includes(ext)) {
952
+ importSpecifierList.push(importee.replace(/.js$/, ext));
912
953
  }
913
- return false;
914
954
  }
955
+ }
915
956
 
916
- const importSpecifierList = [];
957
+ importSpecifierList.push(importee);
917
958
 
918
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
919
- // For module graph roots (i.e. when importer is undefined), we
920
- // need to handle 'path fragments` like `foo/bar` that are commonly
921
- // found in rollup config files. If importee doesn't look like a
922
- // relative or absolute path, we make it relative and attempt to
923
- // resolve it. If we don't find anything, we try resolving it as we
924
- // got it.
925
- importSpecifierList.push(`./${importee}`);
926
- }
959
+ const warn = (...args) => context.warn(...args);
960
+ const isRequire =
961
+ opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
962
+ const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
927
963
 
928
- const importeeIsBuiltin = builtins.has(importee);
964
+ if (useBrowserOverrides && !exportConditions.includes('browser'))
965
+ exportConditions.push('browser');
929
966
 
930
- if (importeeIsBuiltin) {
931
- // The `resolve` library will not resolve packages with the same
932
- // name as a node built-in module. If we're resolving something
933
- // that's a builtin, and we don't prefer to find built-ins, we
934
- // first try to look up a local module with that name. If we don't
935
- // find anything, we resolve the builtin which just returns back
936
- // the built-in's name.
937
- importSpecifierList.push(`${importee}/`);
938
- }
967
+ const resolvedWithoutBuiltins = await resolveImportSpecifiers({
968
+ importer,
969
+ importSpecifierList,
970
+ exportConditions,
971
+ warn,
972
+ packageInfoCache,
973
+ extensions,
974
+ mainFields,
975
+ preserveSymlinks,
976
+ useBrowserOverrides,
977
+ baseDir,
978
+ moduleDirectories,
979
+ rootDir,
980
+ ignoreSideEffectsForRoot
981
+ });
939
982
 
940
- // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
941
- if (importer && importee.endsWith('.js')) {
942
- for (const ext of ['.ts', '.tsx']) {
943
- if (importer.endsWith(ext) && extensions.includes(ext)) {
944
- importSpecifierList.push(importee.replace(/.js$/, ext));
983
+ const resolved =
984
+ importeeIsBuiltin && preferBuiltins
985
+ ? {
986
+ packageInfo: undefined,
987
+ hasModuleSideEffects: () => null,
988
+ hasPackageEntry: true,
989
+ packageBrowserField: false
945
990
  }
991
+ : resolvedWithoutBuiltins;
992
+ if (!resolved) {
993
+ return null;
994
+ }
995
+
996
+ const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
997
+ let { location } = resolved;
998
+ if (packageBrowserField) {
999
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
1000
+ if (!packageBrowserField[location]) {
1001
+ browserMapCache.set(location, packageBrowserField);
1002
+ return { id: ES6_BROWSER_EMPTY };
946
1003
  }
1004
+ location = packageBrowserField[location];
947
1005
  }
1006
+ browserMapCache.set(location, packageBrowserField);
1007
+ }
948
1008
 
949
- importSpecifierList.push(importee);
950
-
951
- const warn = (...args) => this.warn(...args);
952
- const isRequire =
953
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
954
- const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
955
-
956
- const resolvedWithoutBuiltins = await resolveImportSpecifiers({
957
- importer,
958
- importSpecifierList,
959
- exportConditions,
960
- warn,
961
- packageInfoCache,
962
- extensions,
963
- mainFields,
964
- preserveSymlinks,
965
- useBrowserOverrides,
966
- baseDir,
967
- moduleDirectories
968
- });
1009
+ if (hasPackageEntry && !preserveSymlinks) {
1010
+ const fileExists = await exists(location);
1011
+ if (fileExists) {
1012
+ location = await realpath(location);
1013
+ }
1014
+ }
969
1015
 
970
- const resolved =
971
- importeeIsBuiltin && preferBuiltins
972
- ? {
973
- packageInfo: undefined,
974
- hasModuleSideEffects: () => null,
975
- hasPackageEntry: true,
976
- packageBrowserField: false
977
- }
978
- : resolvedWithoutBuiltins;
979
- if (!resolved) {
1016
+ idToPackageInfo.set(location, packageInfo);
1017
+
1018
+ if (hasPackageEntry) {
1019
+ if (importeeIsBuiltin && preferBuiltins) {
1020
+ if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1021
+ context.warn(
1022
+ `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`
1023
+ );
1024
+ }
1025
+ return false;
1026
+ } else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
980
1027
  return null;
981
1028
  }
1029
+ }
982
1030
 
983
- const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
984
- let { location } = resolved;
985
- if (packageBrowserField) {
986
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
987
- if (!packageBrowserField[location]) {
988
- browserMapCache.set(location, packageBrowserField);
989
- return ES6_BROWSER_EMPTY;
990
- }
991
- location = packageBrowserField[location];
992
- }
993
- browserMapCache.set(location, packageBrowserField);
1031
+ if (options.modulesOnly && (await exists(location))) {
1032
+ const code = await readFile$1(location, 'utf-8');
1033
+ if (isModule__default['default'](code)) {
1034
+ return {
1035
+ id: `${location}${importSuffix}`,
1036
+ moduleSideEffects: hasModuleSideEffects(location)
1037
+ };
994
1038
  }
1039
+ return null;
1040
+ }
1041
+ const result = {
1042
+ id: `${location}${importSuffix}`,
1043
+ moduleSideEffects: hasModuleSideEffects(location)
1044
+ };
1045
+ return result;
1046
+ };
995
1047
 
996
- if (hasPackageEntry && !preserveSymlinks) {
997
- const fileExists = await exists(location);
998
- if (fileExists) {
999
- location = await realpath(location);
1000
- }
1048
+ return {
1049
+ name: 'node-resolve',
1050
+
1051
+ buildStart(options) {
1052
+ rollupOptions = options;
1053
+
1054
+ for (const warning of warnings) {
1055
+ this.warn(warning);
1001
1056
  }
1002
1057
 
1003
- idToPackageInfo.set(location, packageInfo);
1058
+ ({ preserveSymlinks } = options);
1059
+ },
1004
1060
 
1005
- if (hasPackageEntry) {
1006
- if (importeeIsBuiltin && preferBuiltins) {
1007
- if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1008
- this.warn(
1009
- `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`
1010
- );
1011
- }
1012
- return false;
1013
- } else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
1014
- return null;
1015
- }
1061
+ generateBundle() {
1062
+ readCachedFile.clear();
1063
+ isFileCached.clear();
1064
+ isDirCached.clear();
1065
+ },
1066
+
1067
+ async resolveId(importee, importer, opts) {
1068
+ if (importee === ES6_BROWSER_EMPTY) {
1069
+ return importee;
1016
1070
  }
1071
+ // ignore IDs with null character, these belong to other plugins
1072
+ if (/\0/.test(importee)) return null;
1017
1073
 
1018
- if (options.modulesOnly && (await exists(location))) {
1019
- const code = await readFile(location, 'utf-8');
1020
- if (isModule__default['default'](code)) {
1021
- return {
1022
- id: `${location}${importSuffix}`,
1023
- moduleSideEffects: hasModuleSideEffects(location)
1024
- };
1074
+ if (/\0/.test(importer)) {
1075
+ importer = undefined;
1076
+ }
1077
+
1078
+ const resolved = await doResolveId(this, importee, importer, opts);
1079
+ if (resolved) {
1080
+ const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1081
+ const isExternal = !!(resolvedResolved && resolvedResolved.external);
1082
+ if (isExternal) {
1083
+ return false;
1025
1084
  }
1026
- return null;
1027
1085
  }
1028
- const result = {
1029
- id: `${location}${importSuffix}`,
1030
- moduleSideEffects: hasModuleSideEffects(location)
1031
- };
1032
- return result;
1086
+ return resolved;
1033
1087
  },
1034
1088
 
1035
1089
  load(importee) {
package/dist/es/index.js CHANGED
@@ -8,10 +8,18 @@ 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
+ const access = 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
+ async function exists(filePath) {
16
+ try {
17
+ await access(filePath);
18
+ return true;
19
+ } catch {
20
+ return false;
21
+ }
22
+ }
15
23
 
16
24
  const onError = (error) => {
17
25
  if (error.code === 'ENOENT') {
@@ -65,7 +73,7 @@ const isFileCached = makeCache(async (file) => {
65
73
  }
66
74
  });
67
75
 
68
- const readCachedFile = makeCache(readFile);
76
+ const readCachedFile = makeCache(readFile$1);
69
77
 
70
78
  // returns the imported package name for bare module imports
71
79
  function getPackageName(id) {
@@ -103,7 +111,16 @@ function getMainFields(options) {
103
111
  }
104
112
 
105
113
  function getPackageInfo(options) {
106
- const { cache, extensions, pkg, mainFields, preserveSymlinks, useBrowserOverrides } = options;
114
+ const {
115
+ cache,
116
+ extensions,
117
+ pkg,
118
+ mainFields,
119
+ preserveSymlinks,
120
+ useBrowserOverrides,
121
+ rootDir,
122
+ ignoreSideEffectsForRoot
123
+ } = options;
107
124
  let { pkgPath } = options;
108
125
 
109
126
  if (cache.has(pkgPath)) {
@@ -193,13 +210,15 @@ function getPackageInfo(options) {
193
210
  packageInfo.browserMappedMain = false;
194
211
  }
195
212
 
196
- const packageSideEffects = pkg.sideEffects;
197
- if (typeof packageSideEffects === 'boolean') {
198
- internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
199
- } else if (Array.isArray(packageSideEffects)) {
200
- internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
201
- resolve: pkgRoot
202
- });
213
+ if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
214
+ const packageSideEffects = pkg.sideEffects;
215
+ if (typeof packageSideEffects === 'boolean') {
216
+ internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
217
+ } else if (Array.isArray(packageSideEffects)) {
218
+ internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
219
+ resolve: pkgRoot
220
+ });
221
+ }
203
222
  }
204
223
 
205
224
  cache.set(pkgPath, internalPackageInfo);
@@ -315,11 +334,11 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna
315
334
  target.replace(/\*/g, subpath),
316
335
  context.pkgURL.href
317
336
  );
318
- return result ? pathToFileURL(result.location) : null;
337
+ return result ? pathToFileURL(result.location).href : null;
319
338
  }
320
339
 
321
340
  const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
322
- return result ? pathToFileURL(result.location) : null;
341
+ return result ? pathToFileURL(result.location).href : null;
323
342
  }
324
343
  throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
325
344
  }
@@ -520,7 +539,7 @@ async function resolvePackageImports({
520
539
  }
521
540
 
522
541
  const resolveImportPath = promisify(resolve$1);
523
- const readFile$1 = promisify(fs.readFile);
542
+ const readFile = promisify(fs.readFile);
524
543
 
525
544
  async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
526
545
  if (importer) {
@@ -533,7 +552,7 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
533
552
 
534
553
  try {
535
554
  const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
536
- const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
555
+ const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
537
556
  return { pkgJsonPath, pkgJson };
538
557
  } catch (_) {
539
558
  return null;
@@ -551,7 +570,9 @@ async function resolveId({
551
570
  preserveSymlinks,
552
571
  useBrowserOverrides,
553
572
  baseDir,
554
- moduleDirectories
573
+ moduleDirectories,
574
+ rootDir,
575
+ ignoreSideEffectsForRoot
555
576
  }) {
556
577
  let hasModuleSideEffects = () => null;
557
578
  let hasPackageEntry = true;
@@ -566,7 +587,9 @@ async function resolveId({
566
587
  pkgPath,
567
588
  mainFields,
568
589
  preserveSymlinks,
569
- useBrowserOverrides
590
+ useBrowserOverrides,
591
+ rootDir,
592
+ ignoreSideEffectsForRoot
570
593
  });
571
594
 
572
595
  ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
@@ -640,11 +663,10 @@ async function resolveId({
640
663
  );
641
664
  location = fileURLToPath(resolvedPackageExport);
642
665
  } catch (error) {
643
- if (!(error instanceof ResolveError)) {
644
- throw error;
666
+ if (error instanceof ResolveError) {
667
+ return error;
645
668
  }
646
- warn(error);
647
- return null;
669
+ throw error;
648
670
  }
649
671
  }
650
672
  }
@@ -689,11 +711,15 @@ async function resolveImportSpecifiers({
689
711
  preserveSymlinks,
690
712
  useBrowserOverrides,
691
713
  baseDir,
692
- moduleDirectories
714
+ moduleDirectories,
715
+ rootDir,
716
+ ignoreSideEffectsForRoot
693
717
  }) {
718
+ let lastResolveError;
719
+
694
720
  for (let i = 0; i < importSpecifierList.length; i++) {
695
721
  // eslint-disable-next-line no-await-in-loop
696
- const resolved = await resolveId({
722
+ const result = await resolveId({
697
723
  importer,
698
724
  importSpecifier: importSpecifierList[i],
699
725
  exportConditions,
@@ -704,12 +730,22 @@ async function resolveImportSpecifiers({
704
730
  preserveSymlinks,
705
731
  useBrowserOverrides,
706
732
  baseDir,
707
- moduleDirectories
733
+ moduleDirectories,
734
+ rootDir,
735
+ ignoreSideEffectsForRoot
708
736
  });
709
- if (resolved) {
710
- return resolved;
737
+
738
+ if (result instanceof ResolveError) {
739
+ lastResolveError = result;
740
+ } else if (result) {
741
+ return result;
711
742
  }
712
743
  }
744
+
745
+ if (lastResolveError) {
746
+ // only log the last failed resolve error
747
+ warn(lastResolveError);
748
+ }
713
749
  return null;
714
750
  }
715
751
 
@@ -785,7 +821,8 @@ const defaults = {
785
821
  // which deploy both ESM .mjs and CommonJS .js files as ESM.
786
822
  extensions: ['.mjs', '.js', '.json', '.node'],
787
823
  resolveOnly: [],
788
- moduleDirectories: ['node_modules']
824
+ moduleDirectories: ['node_modules'],
825
+ ignoreSideEffectsForRoot: false
789
826
  };
790
827
  const DEFAULTS = deepFreeze(deepMerge({}, defaults));
791
828
 
@@ -793,7 +830,7 @@ function nodeResolve(opts = {}) {
793
830
  const { warnings } = handleDeprecatedOptions(opts);
794
831
 
795
832
  const options = { ...defaults, ...opts };
796
- const { extensions, jail, moduleDirectories } = options;
833
+ const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
797
834
  const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
798
835
  const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
799
836
  const packageInfoCache = new Map();
@@ -802,7 +839,7 @@ function nodeResolve(opts = {}) {
802
839
  const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
803
840
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
804
841
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
805
- const rootDir = options.rootDir || process.cwd();
842
+ const rootDir = resolve(options.rootDir || process.cwd());
806
843
  let { dedupe } = options;
807
844
  let rollupOptions;
808
845
 
@@ -822,201 +859,218 @@ function nodeResolve(opts = {}) {
822
859
  const browserMapCache = new Map();
823
860
  let preserveSymlinks;
824
861
 
825
- return {
826
- name: 'node-resolve',
862
+ const doResolveId = async (context, importee, importer, opts) => {
863
+ // strip query params from import
864
+ const [importPath, params] = importee.split('?');
865
+ const importSuffix = `${params ? `?${params}` : ''}`;
866
+ importee = importPath;
827
867
 
828
- buildStart(options) {
829
- rollupOptions = options;
868
+ const baseDir = !importer || dedupe(importee) ? rootDir : dirname(importer);
830
869
 
831
- for (const warning of warnings) {
832
- this.warn(warning);
870
+ // https://github.com/defunctzombie/package-browser-field-spec
871
+ const browser = browserMapCache.get(importer);
872
+ if (useBrowserOverrides && browser) {
873
+ const resolvedImportee = resolve(baseDir, importee);
874
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
875
+ return { id: ES6_BROWSER_EMPTY };
833
876
  }
834
-
835
- ({ preserveSymlinks } = options);
836
- },
837
-
838
- generateBundle() {
839
- readCachedFile.clear();
840
- isFileCached.clear();
841
- isDirCached.clear();
842
- },
843
-
844
- async resolveId(importee, importer, opts) {
845
- if (importee === ES6_BROWSER_EMPTY) {
846
- return importee;
877
+ const browserImportee =
878
+ browser[importee] ||
879
+ browser[resolvedImportee] ||
880
+ browser[`${resolvedImportee}.js`] ||
881
+ browser[`${resolvedImportee}.json`];
882
+ if (browserImportee) {
883
+ importee = browserImportee;
847
884
  }
848
- // ignore IDs with null character, these belong to other plugins
849
- if (/\0/.test(importee)) return null;
885
+ }
850
886
 
851
- if (/\0/.test(importer)) {
852
- importer = undefined;
887
+ const parts = importee.split(/[/\\]/);
888
+ let id = parts.shift();
889
+ let isRelativeImport = false;
890
+
891
+ if (id[0] === '@' && parts.length > 0) {
892
+ // scoped packages
893
+ id += `/${parts.shift()}`;
894
+ } else if (id[0] === '.') {
895
+ // an import relative to the parent dir of the importer
896
+ id = resolve(baseDir, importee);
897
+ isRelativeImport = true;
898
+ }
899
+
900
+ if (
901
+ !isRelativeImport &&
902
+ resolveOnly.length &&
903
+ !resolveOnly.some((pattern) => pattern.test(id))
904
+ ) {
905
+ if (normalizeInput(rollupOptions.input).includes(importee)) {
906
+ return null;
853
907
  }
908
+ return false;
909
+ }
854
910
 
855
- // strip query params from import
856
- const [importPath, params] = importee.split('?');
857
- const importSuffix = `${params ? `?${params}` : ''}`;
858
- importee = importPath;
911
+ const importSpecifierList = [];
859
912
 
860
- const baseDir = !importer || dedupe(importee) ? rootDir : dirname(importer);
913
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
914
+ // For module graph roots (i.e. when importer is undefined), we
915
+ // need to handle 'path fragments` like `foo/bar` that are commonly
916
+ // found in rollup config files. If importee doesn't look like a
917
+ // relative or absolute path, we make it relative and attempt to
918
+ // resolve it. If we don't find anything, we try resolving it as we
919
+ // got it.
920
+ importSpecifierList.push(`./${importee}`);
921
+ }
861
922
 
862
- // https://github.com/defunctzombie/package-browser-field-spec
863
- const browser = browserMapCache.get(importer);
864
- if (useBrowserOverrides && browser) {
865
- const resolvedImportee = resolve(baseDir, importee);
866
- if (browser[importee] === false || browser[resolvedImportee] === false) {
867
- return ES6_BROWSER_EMPTY;
868
- }
869
- const browserImportee =
870
- browser[importee] ||
871
- browser[resolvedImportee] ||
872
- browser[`${resolvedImportee}.js`] ||
873
- browser[`${resolvedImportee}.json`];
874
- if (browserImportee) {
875
- importee = browserImportee;
876
- }
877
- }
923
+ const importeeIsBuiltin = builtins.has(importee);
878
924
 
879
- const parts = importee.split(/[/\\]/);
880
- let id = parts.shift();
881
- let isRelativeImport = false;
882
-
883
- if (id[0] === '@' && parts.length > 0) {
884
- // scoped packages
885
- id += `/${parts.shift()}`;
886
- } else if (id[0] === '.') {
887
- // an import relative to the parent dir of the importer
888
- id = resolve(baseDir, importee);
889
- isRelativeImport = true;
890
- }
925
+ if (importeeIsBuiltin) {
926
+ // The `resolve` library will not resolve packages with the same
927
+ // name as a node built-in module. If we're resolving something
928
+ // that's a builtin, and we don't prefer to find built-ins, we
929
+ // first try to look up a local module with that name. If we don't
930
+ // find anything, we resolve the builtin which just returns back
931
+ // the built-in's name.
932
+ importSpecifierList.push(`${importee}/`);
933
+ }
891
934
 
892
- if (
893
- !isRelativeImport &&
894
- resolveOnly.length &&
895
- !resolveOnly.some((pattern) => pattern.test(id))
896
- ) {
897
- if (normalizeInput(rollupOptions.input).includes(importee)) {
898
- return null;
935
+ // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
936
+ if (importer && importee.endsWith('.js')) {
937
+ for (const ext of ['.ts', '.tsx']) {
938
+ if (importer.endsWith(ext) && extensions.includes(ext)) {
939
+ importSpecifierList.push(importee.replace(/.js$/, ext));
899
940
  }
900
- return false;
901
941
  }
942
+ }
902
943
 
903
- const importSpecifierList = [];
944
+ importSpecifierList.push(importee);
904
945
 
905
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
906
- // For module graph roots (i.e. when importer is undefined), we
907
- // need to handle 'path fragments` like `foo/bar` that are commonly
908
- // found in rollup config files. If importee doesn't look like a
909
- // relative or absolute path, we make it relative and attempt to
910
- // resolve it. If we don't find anything, we try resolving it as we
911
- // got it.
912
- importSpecifierList.push(`./${importee}`);
913
- }
946
+ const warn = (...args) => context.warn(...args);
947
+ const isRequire =
948
+ opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
949
+ const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
914
950
 
915
- const importeeIsBuiltin = builtins.has(importee);
951
+ if (useBrowserOverrides && !exportConditions.includes('browser'))
952
+ exportConditions.push('browser');
916
953
 
917
- if (importeeIsBuiltin) {
918
- // The `resolve` library will not resolve packages with the same
919
- // name as a node built-in module. If we're resolving something
920
- // that's a builtin, and we don't prefer to find built-ins, we
921
- // first try to look up a local module with that name. If we don't
922
- // find anything, we resolve the builtin which just returns back
923
- // the built-in's name.
924
- importSpecifierList.push(`${importee}/`);
925
- }
954
+ const resolvedWithoutBuiltins = await resolveImportSpecifiers({
955
+ importer,
956
+ importSpecifierList,
957
+ exportConditions,
958
+ warn,
959
+ packageInfoCache,
960
+ extensions,
961
+ mainFields,
962
+ preserveSymlinks,
963
+ useBrowserOverrides,
964
+ baseDir,
965
+ moduleDirectories,
966
+ rootDir,
967
+ ignoreSideEffectsForRoot
968
+ });
926
969
 
927
- // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
928
- if (importer && importee.endsWith('.js')) {
929
- for (const ext of ['.ts', '.tsx']) {
930
- if (importer.endsWith(ext) && extensions.includes(ext)) {
931
- importSpecifierList.push(importee.replace(/.js$/, ext));
970
+ const resolved =
971
+ importeeIsBuiltin && preferBuiltins
972
+ ? {
973
+ packageInfo: undefined,
974
+ hasModuleSideEffects: () => null,
975
+ hasPackageEntry: true,
976
+ packageBrowserField: false
932
977
  }
978
+ : resolvedWithoutBuiltins;
979
+ if (!resolved) {
980
+ return null;
981
+ }
982
+
983
+ const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
984
+ let { location } = resolved;
985
+ if (packageBrowserField) {
986
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
987
+ if (!packageBrowserField[location]) {
988
+ browserMapCache.set(location, packageBrowserField);
989
+ return { id: ES6_BROWSER_EMPTY };
933
990
  }
991
+ location = packageBrowserField[location];
934
992
  }
993
+ browserMapCache.set(location, packageBrowserField);
994
+ }
935
995
 
936
- importSpecifierList.push(importee);
937
-
938
- const warn = (...args) => this.warn(...args);
939
- const isRequire =
940
- opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
941
- const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
942
-
943
- const resolvedWithoutBuiltins = await resolveImportSpecifiers({
944
- importer,
945
- importSpecifierList,
946
- exportConditions,
947
- warn,
948
- packageInfoCache,
949
- extensions,
950
- mainFields,
951
- preserveSymlinks,
952
- useBrowserOverrides,
953
- baseDir,
954
- moduleDirectories
955
- });
996
+ if (hasPackageEntry && !preserveSymlinks) {
997
+ const fileExists = await exists(location);
998
+ if (fileExists) {
999
+ location = await realpath(location);
1000
+ }
1001
+ }
956
1002
 
957
- const resolved =
958
- importeeIsBuiltin && preferBuiltins
959
- ? {
960
- packageInfo: undefined,
961
- hasModuleSideEffects: () => null,
962
- hasPackageEntry: true,
963
- packageBrowserField: false
964
- }
965
- : resolvedWithoutBuiltins;
966
- if (!resolved) {
1003
+ idToPackageInfo.set(location, packageInfo);
1004
+
1005
+ if (hasPackageEntry) {
1006
+ if (importeeIsBuiltin && preferBuiltins) {
1007
+ if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
1008
+ context.warn(
1009
+ `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`
1010
+ );
1011
+ }
1012
+ return false;
1013
+ } else if (jail && location.indexOf(normalize(jail.trim(sep))) !== 0) {
967
1014
  return null;
968
1015
  }
1016
+ }
969
1017
 
970
- const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
971
- let { location } = resolved;
972
- if (packageBrowserField) {
973
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
974
- if (!packageBrowserField[location]) {
975
- browserMapCache.set(location, packageBrowserField);
976
- return ES6_BROWSER_EMPTY;
977
- }
978
- location = packageBrowserField[location];
979
- }
980
- browserMapCache.set(location, packageBrowserField);
1018
+ if (options.modulesOnly && (await exists(location))) {
1019
+ const code = await readFile$1(location, 'utf-8');
1020
+ if (isModule(code)) {
1021
+ return {
1022
+ id: `${location}${importSuffix}`,
1023
+ moduleSideEffects: hasModuleSideEffects(location)
1024
+ };
981
1025
  }
1026
+ return null;
1027
+ }
1028
+ const result = {
1029
+ id: `${location}${importSuffix}`,
1030
+ moduleSideEffects: hasModuleSideEffects(location)
1031
+ };
1032
+ return result;
1033
+ };
982
1034
 
983
- if (hasPackageEntry && !preserveSymlinks) {
984
- const fileExists = await exists(location);
985
- if (fileExists) {
986
- location = await realpath(location);
987
- }
1035
+ return {
1036
+ name: 'node-resolve',
1037
+
1038
+ buildStart(options) {
1039
+ rollupOptions = options;
1040
+
1041
+ for (const warning of warnings) {
1042
+ this.warn(warning);
988
1043
  }
989
1044
 
990
- idToPackageInfo.set(location, packageInfo);
1045
+ ({ preserveSymlinks } = options);
1046
+ },
991
1047
 
992
- if (hasPackageEntry) {
993
- if (importeeIsBuiltin && preferBuiltins) {
994
- if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
995
- this.warn(
996
- `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`
997
- );
998
- }
999
- return false;
1000
- } else if (jail && location.indexOf(normalize(jail.trim(sep))) !== 0) {
1001
- return null;
1002
- }
1048
+ generateBundle() {
1049
+ readCachedFile.clear();
1050
+ isFileCached.clear();
1051
+ isDirCached.clear();
1052
+ },
1053
+
1054
+ async resolveId(importee, importer, opts) {
1055
+ if (importee === ES6_BROWSER_EMPTY) {
1056
+ return importee;
1003
1057
  }
1058
+ // ignore IDs with null character, these belong to other plugins
1059
+ if (/\0/.test(importee)) return null;
1004
1060
 
1005
- if (options.modulesOnly && (await exists(location))) {
1006
- const code = await readFile(location, 'utf-8');
1007
- if (isModule(code)) {
1008
- return {
1009
- id: `${location}${importSuffix}`,
1010
- moduleSideEffects: hasModuleSideEffects(location)
1011
- };
1061
+ if (/\0/.test(importer)) {
1062
+ importer = undefined;
1063
+ }
1064
+
1065
+ const resolved = await doResolveId(this, importee, importer, opts);
1066
+ if (resolved) {
1067
+ const resolvedResolved = await this.resolve(resolved.id, importer, { skipSelf: true });
1068
+ const isExternal = !!(resolvedResolved && resolvedResolved.external);
1069
+ if (isExternal) {
1070
+ return false;
1012
1071
  }
1013
- return null;
1014
1072
  }
1015
- const result = {
1016
- id: `${location}${importSuffix}`,
1017
- moduleSideEffects: hasModuleSideEffects(location)
1018
- };
1019
- return result;
1073
+ return resolved;
1020
1074
  },
1021
1075
 
1022
1076
  load(importee) {
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@rollup/plugin-node-resolve",
3
- "version": "11.1.0",
3
+ "version": "13.0.0",
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",
@@ -26,13 +29,9 @@
26
29
  "ci:lint": "pnpm run build && pnpm run lint",
27
30
  "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
28
31
  "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",
33
32
  "prebuild": "del-cli dist",
34
33
  "prepare": "pnpm run build",
35
- "prepublishOnly": "pnpm run lint && pnpm run test && pnpm run test:ts",
34
+ "prepublishOnly": "pnpm -w run lint && pnpm run test && pnpm run test:ts",
36
35
  "pretest": "pnpm run build",
37
36
  "test": "ava",
38
37
  "test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
@@ -51,7 +50,7 @@
51
50
  "modules"
52
51
  ],
53
52
  "peerDependencies": {
54
- "rollup": "^1.20.0||^2.0.0"
53
+ "rollup": "^2.42.0"
55
54
  },
56
55
  "dependencies": {
57
56
  "@rollup/pluginutils": "^3.1.0",
@@ -68,7 +67,7 @@
68
67
  "@rollup/plugin-commonjs": "^16.0.0",
69
68
  "@rollup/plugin-json": "^4.1.0",
70
69
  "es5-ext": "^0.10.53",
71
- "rollup": "^2.23.0",
70
+ "rollup": "^2.42.0",
72
71
  "source-map": "^0.7.3",
73
72
  "string-capitalize": "^1.0.1"
74
73
  },