@rollup/plugin-node-resolve 6.0.0 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,124 +3,155 @@
3
3
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
4
 
5
5
  var path = require('path');
6
- var fs = require('fs');
7
- var fs__default = _interopDefault(fs);
8
6
  var builtinList = _interopDefault(require('builtin-modules'));
9
- var resolveId = _interopDefault(require('resolve'));
10
7
  var isModule = _interopDefault(require('is-module'));
8
+ var fs = require('fs');
9
+ var fs__default = _interopDefault(fs);
10
+ var util = require('util');
11
11
  var pluginutils = require('@rollup/pluginutils');
12
+ var resolveModule = _interopDefault(require('resolve'));
12
13
 
13
- function _slicedToArray(arr, i) {
14
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
15
- }
16
-
17
- function _arrayWithHoles(arr) {
18
- if (Array.isArray(arr)) return arr;
19
- }
20
-
21
- function _iterableToArrayLimit(arr, i) {
22
- if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
14
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
15
+ try {
16
+ var info = gen[key](arg);
17
+ var value = info.value;
18
+ } catch (error) {
19
+ reject(error);
23
20
  return;
24
21
  }
25
22
 
26
- var _arr = [];
27
- var _n = true;
28
- var _d = false;
29
- var _e = undefined;
23
+ if (info.done) {
24
+ resolve(value);
25
+ } else {
26
+ Promise.resolve(value).then(_next, _throw);
27
+ }
28
+ }
30
29
 
31
- try {
32
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
33
- _arr.push(_s.value);
30
+ function _asyncToGenerator(fn) {
31
+ return function () {
32
+ var self = this,
33
+ args = arguments;
34
+ return new Promise(function (resolve, reject) {
35
+ var gen = fn.apply(self, args);
34
36
 
35
- if (i && _arr.length === i) break;
36
- }
37
- } catch (err) {
38
- _d = true;
39
- _e = err;
40
- } finally {
41
- try {
42
- if (!_n && _i["return"] != null) _i["return"]();
43
- } finally {
44
- if (_d) throw _e;
45
- }
46
- }
37
+ function _next(value) {
38
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
39
+ }
47
40
 
48
- return _arr;
49
- }
41
+ function _throw(err) {
42
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
43
+ }
50
44
 
51
- function _nonIterableRest() {
52
- throw new TypeError("Invalid attempt to destructure non-iterable instance");
45
+ _next(undefined);
46
+ });
47
+ };
53
48
  }
54
49
 
55
- var peerDependencies = {
56
- rollup: "^1.20.0"
50
+ const exists = util.promisify(fs__default.exists);
51
+ const readFile = util.promisify(fs__default.readFile);
52
+ const realpath = util.promisify(fs__default.realpath);
53
+ const stat = util.promisify(fs__default.stat);
54
+
55
+ const onError = error => {
56
+ if (error.code === 'ENOENT') {
57
+ return false;
58
+ }
59
+
60
+ throw error;
57
61
  };
58
62
 
59
- const builtins = new Set(builtinList);
60
- const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; // It is important that .mjs occur before .js so that Rollup will interpret npm modules
61
- // which deploy both ESM .mjs and CommonJS .js files as ESM.
63
+ const makeCache = fn => {
64
+ const cache = new Map();
62
65
 
63
- const DEFAULT_EXTS = ['.mjs', '.js', '.json', '.node'];
66
+ const wrapped =
67
+ /*#__PURE__*/
68
+ function () {
69
+ var _ref = _asyncToGenerator(function* (param, done) {
70
+ if (cache.has(param) === false) {
71
+ cache.set(param, fn(param).catch(err => {
72
+ cache.delete(param);
73
+ throw err;
74
+ }));
75
+ }
64
76
 
65
- const existsAsync = file => new Promise(fulfil => fs__default.exists(file, fulfil));
77
+ try {
78
+ const result = cache.get(param);
79
+ const value = yield result;
80
+ return done(null, value);
81
+ } catch (error) {
82
+ return done(error);
83
+ }
84
+ });
66
85
 
67
- const readFileAsync = file => new Promise((fulfil, reject) => fs__default.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));
86
+ return function wrapped(_x, _x2) {
87
+ return _ref.apply(this, arguments);
88
+ };
89
+ }();
68
90
 
69
- const realpathAsync = file => new Promise((fulfil, reject) => fs__default.realpath(file, (err, contents) => err ? reject(err) : fulfil(contents)));
91
+ wrapped.clear = () => cache.clear();
70
92
 
71
- const statAsync = file => new Promise((fulfil, reject) => fs__default.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));
93
+ return wrapped;
94
+ };
72
95
 
73
- const cache = fn => {
74
- const cache = new Map();
96
+ const isDirCached = makeCache(
97
+ /*#__PURE__*/
98
+ function () {
99
+ var _ref2 = _asyncToGenerator(function* (file) {
100
+ try {
101
+ const stats = yield stat(file);
102
+ return stats.isDirectory();
103
+ } catch (error) {
104
+ return onError(error);
105
+ }
106
+ });
75
107
 
76
- const wrapped = (param, done) => {
77
- if (cache.has(param) === false) {
78
- cache.set(param, fn(param).catch(err => {
79
- cache.delete(param);
80
- throw err;
81
- }));
108
+ return function (_x3) {
109
+ return _ref2.apply(this, arguments);
110
+ };
111
+ }());
112
+ const isFileCached = makeCache(
113
+ /*#__PURE__*/
114
+ function () {
115
+ var _ref3 = _asyncToGenerator(function* (file) {
116
+ try {
117
+ const stats = yield stat(file);
118
+ return stats.isFile();
119
+ } catch (error) {
120
+ return onError(error);
82
121
  }
122
+ });
83
123
 
84
- return cache.get(param).then(result => done(null, result), done);
124
+ return function (_x4) {
125
+ return _ref3.apply(this, arguments);
85
126
  };
127
+ }());
128
+ const readCachedFile = makeCache(readFile);
86
129
 
87
- wrapped.clear = () => cache.clear();
130
+ const resolveId = util.promisify(resolveModule); // returns the imported package name for bare module imports
88
131
 
89
- return wrapped;
90
- };
132
+ function getPackageName(id) {
133
+ if (id.startsWith('.') || id.startsWith('/')) {
134
+ return null;
135
+ }
91
136
 
92
- const ignoreENOENT = err => {
93
- if (err.code === 'ENOENT') return false;
94
- throw err;
95
- };
137
+ const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package
138
+ // @my-scope/my-package -> @my-scope/my-package
139
+
140
+ if (split[0][0] === '@') {
141
+ return `${split[0]}/${split[1]}`;
142
+ } // my-package/foo.js -> my-package
143
+ // my-package -> my-package
96
144
 
97
- const readFileCached = cache(readFileAsync);
98
- const isDirCached = cache(file => statAsync(file).then(stat => stat.isDirectory(), ignoreENOENT));
99
- const isFileCached = cache(file => statAsync(file).then(stat => stat.isFile(), ignoreENOENT));
100
145
 
146
+ return split[0];
147
+ }
101
148
  function getMainFields(options) {
102
149
  let mainFields;
103
150
 
104
151
  if (options.mainFields) {
105
- if ('module' in options || 'main' in options || 'jsnext' in options) {
106
- throw new Error(`node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'`);
107
- }
108
-
109
152
  mainFields = options.mainFields;
110
153
  } else {
111
- mainFields = [];
112
- [['module', 'module', true], ['jsnext', 'jsnext:main', false], ['main', 'main', true]].forEach(([option, field, defaultIncluded]) => {
113
- if (option in options) {
114
- // eslint-disable-next-line no-console
115
- console.warn(`node-resolve: setting options.${option} is deprecated, please override options.mainFields instead`);
116
-
117
- if (options[option]) {
118
- mainFields.push(field);
119
- }
120
- } else if (defaultIncluded) {
121
- mainFields.push(field);
122
- }
123
- });
154
+ mainFields = ['module', 'main'];
124
155
  }
125
156
 
126
157
  if (options.browser && mainFields.indexOf('browser') === -1) {
@@ -128,331 +159,411 @@ function getMainFields(options) {
128
159
  }
129
160
 
130
161
  if (!mainFields.length) {
131
- throw new Error(`Please ensure at least one 'mainFields' value is specified`);
162
+ throw new Error('Please ensure at least one `mainFields` value is specified');
132
163
  }
133
164
 
134
165
  return mainFields;
135
166
  }
167
+ function getPackageInfo(options) {
168
+ const cache = options.cache,
169
+ extensions = options.extensions,
170
+ pkg = options.pkg,
171
+ mainFields = options.mainFields,
172
+ preserveSymlinks = options.preserveSymlinks,
173
+ useBrowserOverrides = options.useBrowserOverrides;
174
+ let pkgPath = options.pkgPath;
175
+
176
+ if (cache.has(pkgPath)) {
177
+ return cache.get(pkgPath);
178
+ } // browserify/resolve doesn't realpath paths returned in its packageFilter callback
179
+
180
+
181
+ if (!preserveSymlinks) {
182
+ pkgPath = fs.realpathSync(pkgPath);
183
+ }
184
+
185
+ const pkgRoot = path.dirname(pkgPath);
186
+ const packageInfo = {
187
+ // copy as we are about to munge the `main` field of `pkg`.
188
+ packageJson: Object.assign({}, pkg),
189
+ // path to package.json file
190
+ packageJsonPath: pkgPath,
191
+ // directory containing the package.json
192
+ root: pkgRoot,
193
+ // which main field was used during resolution of this module (main, module, or browser)
194
+ resolvedMainField: 'main',
195
+ // whether the browser map was used to resolve the entry point to this module
196
+ browserMappedMain: false,
197
+ // the entry point of the module with respect to the selected main field and any
198
+ // relevant browser mappings.
199
+ resolvedEntryPoint: ''
200
+ };
201
+ let overriddenMain = false;
202
+
203
+ for (let i = 0; i < mainFields.length; i++) {
204
+ const field = mainFields[i];
205
+
206
+ if (typeof pkg[field] === 'string') {
207
+ pkg.main = pkg[field];
208
+ packageInfo.resolvedMainField = field;
209
+ overriddenMain = true;
210
+ break;
211
+ }
212
+ }
213
+
214
+ const internalPackageInfo = {
215
+ cachedPkg: pkg,
216
+ hasModuleSideEffects: () => null,
217
+ hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
218
+ packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => {
219
+ let resolved = pkg.browser[key];
220
+
221
+ if (resolved && resolved[0] === '.') {
222
+ resolved = path.resolve(pkgRoot, resolved);
223
+ }
224
+ /* eslint-disable no-param-reassign */
136
225
 
137
- const alwaysNull = () => null;
138
226
 
139
- const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents))); // Resolve module specifiers in order. Promise resolves to the first
140
- // module that resolves successfully, or the error that resulted from
141
- // the last attempted module resolution.
227
+ browser[key] = resolved;
142
228
 
229
+ if (key[0] === '.') {
230
+ const absoluteKey = path.resolve(pkgRoot, key);
231
+ browser[absoluteKey] = resolved;
232
+
233
+ if (!path.extname(key)) {
234
+ extensions.reduce((subBrowser, ext) => {
235
+ subBrowser[absoluteKey + ext] = subBrowser[key];
236
+ return subBrowser;
237
+ }, browser);
238
+ }
239
+ }
240
+
241
+ return browser;
242
+ }, {}),
243
+ packageInfo
244
+ };
245
+ const browserMap = internalPackageInfo.packageBrowserField;
246
+
247
+ if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins
248
+ browserMap.hasOwnProperty(pkg.main)) {
249
+ packageInfo.resolvedEntryPoint = browserMap[pkg.main];
250
+ packageInfo.browserMappedMain = true;
251
+ } else {
252
+ // index.node is technically a valid default entrypoint as well...
253
+ packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js');
254
+ packageInfo.browserMappedMain = false;
255
+ }
256
+
257
+ const packageSideEffects = pkg.sideEffects;
258
+
259
+ if (typeof packageSideEffects === 'boolean') {
260
+ internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
261
+ } else if (Array.isArray(packageSideEffects)) {
262
+ internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
263
+ resolve: pkgRoot
264
+ });
265
+ }
266
+
267
+ cache.set(pkgPath, internalPackageInfo);
268
+ return internalPackageInfo;
269
+ }
270
+ function normalizeInput(input) {
271
+ if (Array.isArray(input)) {
272
+ return input;
273
+ } else if (typeof input === 'object') {
274
+ return Object.values(input);
275
+ } // otherwise it's a string
276
+
277
+
278
+ return input;
279
+ } // Resolve module specifiers in order. Promise resolves to the first module that resolves
280
+ // successfully, or the error that resulted from the last attempted module resolution.
143
281
 
144
282
  function resolveImportSpecifiers(importSpecifierList, resolveOptions) {
145
- let p = Promise.resolve();
283
+ let promise = Promise.resolve();
146
284
 
147
285
  for (let i = 0; i < importSpecifierList.length; i++) {
148
- p = p.then(v => {
286
+ promise = promise.then(value => {
149
287
  // if we've already resolved to something, just return it.
150
- if (v) return v;
151
- return resolveIdAsync(importSpecifierList[i], resolveOptions);
288
+ if (value) {
289
+ return value;
290
+ }
291
+
292
+ return resolveId(importSpecifierList[i], resolveOptions);
152
293
  });
153
294
 
154
295
  if (i < importSpecifierList.length - 1) {
155
296
  // swallow MODULE_NOT_FOUND errors from all but the last resolution
156
- p = p.catch(err => {
157
- if (err.code !== 'MODULE_NOT_FOUND') {
158
- throw err;
297
+ promise = promise.catch(error => {
298
+ if (error.code !== 'MODULE_NOT_FOUND') {
299
+ throw error;
159
300
  }
160
301
  });
161
302
  }
162
303
  }
163
304
 
164
- return p;
305
+ return promise;
165
306
  }
166
307
 
167
- function nodeResolve(options = {}) {
308
+ const builtins = new Set(builtinList);
309
+ const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
310
+
311
+ const nullFn = () => null;
312
+
313
+ const defaults = {
314
+ customResolveOptions: {},
315
+ dedupe: [],
316
+ // It's important that .mjs is listed before .js so that Rollup will interpret npm modules
317
+ // which deploy both ESM .mjs and CommonJS .js files as ESM.
318
+ extensions: ['.mjs', '.js', '.json', '.node'],
319
+ resolveOnly: []
320
+ };
321
+ function nodeResolve(opts = {}) {
322
+ const options = Object.assign({}, defaults, opts);
323
+ const customResolveOptions = options.customResolveOptions,
324
+ extensions = options.extensions,
325
+ jail = options.jail;
326
+ const warnings = [];
327
+ const packageInfoCache = new Map();
328
+ const idToPackageInfo = new Map();
168
329
  const mainFields = getMainFields(options);
169
330
  const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
170
- const dedupe = options.dedupe || [];
171
331
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
172
332
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
173
- const customResolveOptions = options.customResolveOptions || {};
174
- const jail = options.jail;
175
- const only = Array.isArray(options.only) ? options.only.map(o => o instanceof RegExp ? o : new RegExp(`^${String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')}$`)) : null;
176
- const browserMapCache = new Map();
177
- let preserveSymlinks;
333
+ const rootDir = options.rootDir || process.cwd();
334
+ let dedupe = options.dedupe;
335
+ let rollupOptions;
178
336
 
179
- if (options.skip) {
180
- throw new Error('options.skip is no longer supported you should use the main Rollup `external` option instead');
337
+ if (options.only) {
338
+ warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`');
339
+ options.resolveOnly = options.only;
181
340
  }
182
341
 
183
- const extensions = options.extensions || DEFAULT_EXTS;
184
- const packageInfoCache = new Map();
185
- const idToPackageInfo = new Map();
186
- const shouldDedupe = typeof dedupe === 'function' ? dedupe : importee => dedupe.includes(importee);
187
-
188
- function getCachedPackageInfo(pkg, pkgPath) {
189
- if (packageInfoCache.has(pkgPath)) {
190
- return packageInfoCache.get(pkgPath);
191
- } // browserify/resolve doesn't realpath paths returned in its packageFilter callback
192
-
193
-
194
- if (!preserveSymlinks) {
195
- pkgPath = fs.realpathSync(pkgPath);
196
- }
197
-
198
- const pkgRoot = path.dirname(pkgPath);
199
- const packageInfo = {
200
- // copy as we are about to munge the `main` field of `pkg`.
201
- packageJson: Object.assign({}, pkg),
202
- // path to package.json file
203
- packageJsonPath: pkgPath,
204
- // directory containing the package.json
205
- root: pkgRoot,
206
- // which main field was used during resolution of this module (main, module, or browser)
207
- resolvedMainField: 'main',
208
- // whether the browser map was used to resolve the entry point to this module
209
- browserMappedMain: false,
210
- // the entry point of the module with respect to the selected main field and any
211
- // relevant browser mappings.
212
- resolvedEntryPoint: ''
213
- };
214
- let overriddenMain = false;
215
-
216
- for (let i = 0; i < mainFields.length; i++) {
217
- const field = mainFields[i];
342
+ if (typeof dedupe !== 'function') {
343
+ dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
344
+ }
218
345
 
219
- if (typeof pkg[field] === 'string') {
220
- pkg.main = pkg[field];
221
- packageInfo.resolvedMainField = field;
222
- overriddenMain = true;
223
- break;
224
- }
346
+ const resolveOnly = options.resolveOnly.map(pattern => {
347
+ if (pattern instanceof RegExp) {
348
+ return pattern;
225
349
  }
226
350
 
227
- const internalPackageInfo = {
228
- cachedPkg: pkg,
229
- hasModuleSideEffects: alwaysNull,
230
- hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
231
- packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => {
232
- let resolved = pkg.browser[key];
351
+ const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
352
+ return new RegExp(`^${normalized}$`);
353
+ });
354
+ const browserMapCache = new Map();
355
+ let preserveSymlinks;
356
+ return {
357
+ name: 'node-resolve',
233
358
 
234
- if (resolved && resolved[0] === '.') {
235
- resolved = path.resolve(pkgRoot, resolved);
359
+ buildStart(options) {
360
+ rollupOptions = options;
361
+ var _iteratorNormalCompletion = true;
362
+ var _didIteratorError = false;
363
+ var _iteratorError = undefined;
364
+
365
+ try {
366
+ for (var _iterator = warnings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
367
+ const warning = _step.value;
368
+ this.warn(warning);
236
369
  }
237
-
238
- browser[key] = resolved;
239
-
240
- if (key[0] === '.') {
241
- const absoluteKey = path.resolve(pkgRoot, key);
242
- browser[absoluteKey] = resolved;
243
-
244
- if (!path.extname(key)) {
245
- extensions.reduce((browser, ext) => {
246
- browser[absoluteKey + ext] = browser[key];
247
- return browser;
248
- }, browser);
370
+ } catch (err) {
371
+ _didIteratorError = true;
372
+ _iteratorError = err;
373
+ } finally {
374
+ try {
375
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
376
+ _iterator.return();
377
+ }
378
+ } finally {
379
+ if (_didIteratorError) {
380
+ throw _iteratorError;
249
381
  }
250
382
  }
383
+ }
251
384
 
252
- return browser;
253
- }, {}),
254
- packageInfo
255
- };
256
- const browserMap = internalPackageInfo.packageBrowserField;
257
-
258
- if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins
259
- browserMap.hasOwnProperty(pkg.main)) {
260
- packageInfo.resolvedEntryPoint = browserMap[pkg.main];
261
- packageInfo.browserMappedMain = true;
262
- } else {
263
- // index.node is technically a valid default entrypoint as well...
264
- packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js');
265
- packageInfo.browserMappedMain = false;
266
- }
385
+ preserveSymlinks = options.preserveSymlinks;
386
+ },
267
387
 
268
- const packageSideEffects = pkg.sideEffects;
388
+ generateBundle() {
389
+ readCachedFile.clear();
390
+ isFileCached.clear();
391
+ isDirCached.clear();
392
+ },
269
393
 
270
- if (typeof packageSideEffects === 'boolean') {
271
- internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
272
- } else if (Array.isArray(packageSideEffects)) {
273
- internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
274
- resolve: pkgRoot
275
- });
276
- }
394
+ resolveId(importee, importer) {
395
+ var _this = this;
277
396
 
278
- packageInfoCache.set(pkgPath, internalPackageInfo);
279
- return internalPackageInfo;
280
- }
397
+ return _asyncToGenerator(function* () {
398
+ if (importee === ES6_BROWSER_EMPTY) {
399
+ return importee;
400
+ } // ignore IDs with null character, these belong to other plugins
281
401
 
282
- return {
283
- name: 'node-resolve',
284
402
 
285
- buildStart(options) {
286
- preserveSymlinks = options.preserveSymlinks;
403
+ if (/\0/.test(importee)) return null;
404
+ const basedir = !importer || dedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec
287
405
 
288
- const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number),
289
- _this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2),
290
- major = _this$meta$rollupVers2[0],
291
- minor = _this$meta$rollupVers2[1];
406
+ const browser = browserMapCache.get(importer);
292
407
 
293
- const minVersion = peerDependencies.rollup.slice(2);
408
+ if (useBrowserOverrides && browser) {
409
+ const resolvedImportee = path.resolve(basedir, importee);
294
410
 
295
- const _minVersion$split$map = minVersion.split('.').map(Number),
296
- _minVersion$split$map2 = _slicedToArray(_minVersion$split$map, 2),
297
- minMajor = _minVersion$split$map2[0],
298
- minMinor = _minVersion$split$map2[1];
411
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
412
+ return ES6_BROWSER_EMPTY;
413
+ }
299
414
 
300
- if (major < minMajor || major === minMajor && minor < minMinor) {
301
- this.error(`Insufficient Rollup version: "rollup-plugin-node-resolve" requires at least rollup@${minVersion} but found rollup@${this.meta.rollupVersion}.`);
302
- }
303
- },
415
+ const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
304
416
 
305
- generateBundle() {
306
- readFileCached.clear();
307
- isFileCached.clear();
308
- isDirCached.clear();
309
- },
417
+ if (browserImportee) {
418
+ importee = browserImportee;
419
+ }
420
+ }
310
421
 
311
- resolveId(importee, importer) {
312
- if (importee === ES6_BROWSER_EMPTY) {
313
- return importee;
314
- } // ignore IDs with null character, these belong to other plugins
422
+ const parts = importee.split(/[/\\]/);
423
+ let id = parts.shift();
315
424
 
425
+ if (id[0] === '@' && parts.length > 0) {
426
+ // scoped packages
427
+ id += `/${parts.shift()}`;
428
+ } else if (id[0] === '.') {
429
+ // an import relative to the parent dir of the importer
430
+ id = path.resolve(basedir, importee);
431
+ }
316
432
 
317
- if (/\0/.test(importee)) return null;
318
- const basedir = importer ? path.dirname(importer) : process.cwd();
433
+ const input = normalizeInput(rollupOptions.input);
319
434
 
320
- if (shouldDedupe(importee)) {
321
- importee = path.join(process.cwd(), 'node_modules', importee);
322
- } // https://github.com/defunctzombie/package-browser-field-spec
435
+ if (resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) {
436
+ if (input.includes(id)) {
437
+ return null;
438
+ }
323
439
 
440
+ return false;
441
+ }
324
442
 
325
- const browser = browserMapCache.get(importer);
443
+ let hasModuleSideEffects = nullFn;
444
+ let hasPackageEntry = true;
445
+ let packageBrowserField = false;
446
+ let packageInfo;
447
+
448
+ const filter = (pkg, pkgPath) => {
449
+ const info = getPackageInfo({
450
+ cache: packageInfoCache,
451
+ extensions,
452
+ pkg,
453
+ pkgPath,
454
+ mainFields,
455
+ preserveSymlinks,
456
+ useBrowserOverrides
457
+ });
458
+ packageInfo = info.packageInfo;
459
+ hasModuleSideEffects = info.hasModuleSideEffects;
460
+ hasPackageEntry = info.hasPackageEntry;
461
+ packageBrowserField = info.packageBrowserField;
462
+ return info.cachedPkg;
463
+ };
326
464
 
327
- if (useBrowserOverrides && browser) {
328
- const resolvedImportee = path.resolve(basedir, importee);
465
+ let resolveOptions = {
466
+ basedir,
467
+ packageFilter: filter,
468
+ readFile: readCachedFile,
469
+ isFile: isFileCached,
470
+ isDirectory: isDirCached,
471
+ extensions
472
+ };
329
473
 
330
- if (browser[importee] === false || browser[resolvedImportee] === false) {
331
- return ES6_BROWSER_EMPTY;
474
+ if (preserveSymlinks !== undefined) {
475
+ resolveOptions.preserveSymlinks = preserveSymlinks;
332
476
  }
333
477
 
334
- const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
478
+ const importSpecifierList = [];
335
479
 
336
- if (browserImportee) {
337
- importee = browserImportee;
480
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
481
+ // For module graph roots (i.e. when importer is undefined), we
482
+ // need to handle 'path fragments` like `foo/bar` that are commonly
483
+ // found in rollup config files. If importee doesn't look like a
484
+ // relative or absolute path, we make it relative and attempt to
485
+ // resolve it. If we don't find anything, we try resolving it as we
486
+ // got it.
487
+ importSpecifierList.push(`./${importee}`);
338
488
  }
339
- }
340
489
 
341
- const parts = importee.split(/[/\\]/);
342
- let id = parts.shift();
490
+ const importeeIsBuiltin = builtins.has(importee);
343
491
 
344
- if (id[0] === '@' && parts.length > 0) {
345
- // scoped packages
346
- id += `/${parts.shift()}`;
347
- } else if (id[0] === '.') {
348
- // an import relative to the parent dir of the importer
349
- id = path.resolve(basedir, importee);
350
- }
492
+ if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
493
+ // The `resolve` library will not resolve packages with the same
494
+ // name as a node built-in module. If we're resolving something
495
+ // that's a builtin, and we don't prefer to find built-ins, we
496
+ // first try to look up a local module with that name. If we don't
497
+ // find anything, we resolve the builtin which just returns back
498
+ // the built-in's name.
499
+ importSpecifierList.push(`${importee}/`);
500
+ }
351
501
 
352
- if (only && !only.some(pattern => pattern.test(id))) return null;
353
- let hasModuleSideEffects = alwaysNull;
354
- let hasPackageEntry = true;
355
- let packageBrowserField = false;
356
- let packageInfo;
357
- const resolveOptions = {
358
- basedir,
359
-
360
- packageFilter(pkg, pkgPath) {
361
- let cachedPkg;
362
-
363
- var _getCachedPackageInfo = getCachedPackageInfo(pkg, pkgPath);
364
-
365
- packageInfo = _getCachedPackageInfo.packageInfo;
366
- cachedPkg = _getCachedPackageInfo.cachedPkg;
367
- hasModuleSideEffects = _getCachedPackageInfo.hasModuleSideEffects;
368
- hasPackageEntry = _getCachedPackageInfo.hasPackageEntry;
369
- packageBrowserField = _getCachedPackageInfo.packageBrowserField;
370
- return cachedPkg;
371
- },
372
-
373
- readFile: readFileCached,
374
- isFile: isFileCached,
375
- isDirectory: isDirCached,
376
- extensions
377
- };
378
-
379
- if (preserveSymlinks !== undefined) {
380
- resolveOptions.preserveSymlinks = preserveSymlinks;
381
- }
502
+ importSpecifierList.push(importee);
503
+ resolveOptions = Object.assign(resolveOptions, customResolveOptions);
382
504
 
383
- const importSpecifierList = [];
505
+ try {
506
+ let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions);
384
507
 
385
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
386
- // For module graph roots (i.e. when importer is undefined), we
387
- // need to handle 'path fragments` like `foo/bar` that are commonly
388
- // found in rollup config files. If importee doesn't look like a
389
- // relative or absolute path, we make it relative and attempt to
390
- // resolve it. If we don't find anything, we try resolving it as we
391
- // got it.
392
- importSpecifierList.push(`./${importee}`);
393
- }
508
+ if (resolved && packageBrowserField) {
509
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
510
+ if (!packageBrowserField[resolved]) {
511
+ browserMapCache.set(resolved, packageBrowserField);
512
+ return ES6_BROWSER_EMPTY;
513
+ }
394
514
 
395
- const importeeIsBuiltin = builtins.has(importee);
515
+ resolved = packageBrowserField[resolved];
516
+ }
396
517
 
397
- if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
398
- // The `resolve` library will not resolve packages with the same
399
- // name as a node built-in module. If we're resolving something
400
- // that's a builtin, and we don't prefer to find built-ins, we
401
- // first try to look up a local module with that name. If we don't
402
- // find anything, we resolve the builtin which just returns back
403
- // the built-in's name.
404
- importSpecifierList.push(`${importee}/`);
405
- }
518
+ browserMapCache.set(resolved, packageBrowserField);
519
+ }
406
520
 
407
- importSpecifierList.push(importee);
408
- return resolveImportSpecifiers(importSpecifierList, Object.assign(resolveOptions, customResolveOptions)).then(resolved => {
409
- if (resolved && packageBrowserField) {
410
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
411
- if (!packageBrowserField[resolved]) {
412
- browserMapCache.set(resolved, packageBrowserField);
413
- return ES6_BROWSER_EMPTY;
414
- }
521
+ if (hasPackageEntry && !preserveSymlinks && resolved) {
522
+ const fileExists = yield exists(resolved);
415
523
 
416
- resolved = packageBrowserField[resolved];
524
+ if (fileExists) {
525
+ resolved = yield realpath(resolved);
526
+ }
417
527
  }
418
528
 
419
- browserMapCache.set(resolved, packageBrowserField);
420
- }
529
+ idToPackageInfo.set(resolved, packageInfo);
421
530
 
422
- if (hasPackageEntry && !preserveSymlinks && resolved) {
423
- return existsAsync(resolved).then(exists => exists ? realpathAsync(resolved) : resolved);
424
- }
531
+ if (hasPackageEntry) {
532
+ if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
533
+ return null;
534
+ } else if (importeeIsBuiltin && preferBuiltins) {
535
+ if (!isPreferBuiltinsSet) {
536
+ _this.warn(`preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`);
537
+ }
425
538
 
426
- return resolved;
427
- }).then(resolved => {
428
- idToPackageInfo.set(resolved, packageInfo);
539
+ return null;
540
+ } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
541
+ return null;
542
+ }
543
+ }
429
544
 
430
- if (hasPackageEntry) {
431
- if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
432
- return null;
433
- } else if (importeeIsBuiltin && preferBuiltins) {
434
- if (!isPreferBuiltinsSet) {
435
- this.warn(`preferring built-in module '${importee}' over local alternative ` + `at '${resolved}', pass 'preferBuiltins: false' to disable this ` + `behavior or 'preferBuiltins: true' to disable this warning`);
545
+ if (resolved && options.modulesOnly) {
546
+ const code = yield readFile(resolved, 'utf-8');
547
+
548
+ if (isModule(code)) {
549
+ return {
550
+ id: resolved,
551
+ moduleSideEffects: hasModuleSideEffects(resolved)
552
+ };
436
553
  }
437
554
 
438
- return null;
439
- } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
440
555
  return null;
441
556
  }
442
- }
443
557
 
444
- if (resolved && options.modulesOnly) {
445
- return readFileAsync(resolved).then(code => isModule(code) ? {
558
+ const result = {
446
559
  id: resolved,
447
560
  moduleSideEffects: hasModuleSideEffects(resolved)
448
- } : null);
561
+ };
562
+ return result;
563
+ } catch (error) {
564
+ return null;
449
565
  }
450
-
451
- return {
452
- id: resolved,
453
- moduleSideEffects: hasModuleSideEffects(resolved)
454
- };
455
- }).catch(() => null);
566
+ })();
456
567
  },
457
568
 
458
569
  load(importee) {