@rollup/plugin-node-resolve 7.0.0 → 7.1.3

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
+ }
29
+
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);
30
36
 
31
- try {
32
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
33
- _arr.push(_s.value);
37
+ function _next(value) {
38
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
39
+ }
34
40
 
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
- }
41
+ function _throw(err) {
42
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
43
+ }
47
44
 
48
- return _arr;
45
+ _next(undefined);
46
+ });
47
+ };
49
48
  }
50
49
 
51
- function _nonIterableRest() {
52
- throw new TypeError("Invalid attempt to destructure non-iterable instance");
53
- }
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
+ }
54
59
 
55
- var peerDependencies = {
56
- rollup: "^1.20.0"
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,345 +159,417 @@ 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).then(result => {
293
+ if (!resolveOptions.preserveSymlinks) {
294
+ result = fs.realpathSync(result);
295
+ }
296
+
297
+ return result;
298
+ });
152
299
  });
153
300
 
154
301
  if (i < importSpecifierList.length - 1) {
155
302
  // 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;
303
+ promise = promise.catch(error => {
304
+ if (error.code !== 'MODULE_NOT_FOUND') {
305
+ throw error;
159
306
  }
160
307
  });
161
308
  }
162
309
  }
163
310
 
164
- return p;
165
- } // returns the imported package name for bare module imports
166
-
167
-
168
- function getPackageName(id) {
169
- if (id.startsWith('.') || id.startsWith('/')) {
170
- return null;
171
- }
172
-
173
- const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package
174
- // @my-scope/my-package -> @my-scope/my-package
175
-
176
- if (split[0][0] === '@') {
177
- return `${split[0]}/${split[1]}`;
178
- } // my-package/foo.js -> my-package
179
- // my-package -> my-package
311
+ return promise;
312
+ }
180
313
 
314
+ const builtins = new Set(builtinList);
315
+ const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
181
316
 
182
- return split[0];
183
- }
317
+ const nullFn = () => null;
184
318
 
185
- function nodeResolve(options = {}) {
319
+ const defaults = {
320
+ customResolveOptions: {},
321
+ dedupe: [],
322
+ // It's important that .mjs is listed before .js so that Rollup will interpret npm modules
323
+ // which deploy both ESM .mjs and CommonJS .js files as ESM.
324
+ extensions: ['.mjs', '.js', '.json', '.node'],
325
+ resolveOnly: []
326
+ };
327
+ function nodeResolve(opts = {}) {
328
+ const options = Object.assign({}, defaults, opts);
329
+ const customResolveOptions = options.customResolveOptions,
330
+ extensions = options.extensions,
331
+ jail = options.jail;
332
+ const warnings = [];
333
+ const packageInfoCache = new Map();
334
+ const idToPackageInfo = new Map();
186
335
  const mainFields = getMainFields(options);
187
336
  const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
188
- const dedupe = options.dedupe || [];
189
337
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
190
338
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
191
- const customResolveOptions = options.customResolveOptions || {};
192
339
  const rootDir = options.rootDir || process.cwd();
193
- const jail = options.jail;
194
- const only = Array.isArray(options.only) ? options.only.map(o => o instanceof RegExp ? o : new RegExp(`^${String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')}$`)) : null;
195
- const browserMapCache = new Map();
196
- let preserveSymlinks;
340
+ let dedupe = options.dedupe;
341
+ let rollupOptions;
197
342
 
198
- if (options.skip) {
199
- throw new Error('options.skip is no longer supported you should use the main Rollup `external` option instead');
343
+ if (options.only) {
344
+ warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`');
345
+ options.resolveOnly = options.only;
200
346
  }
201
347
 
202
- const extensions = options.extensions || DEFAULT_EXTS;
203
- const packageInfoCache = new Map();
204
- const idToPackageInfo = new Map();
205
- const shouldDedupe = typeof dedupe === 'function' ? dedupe : importee => dedupe.includes(importee) || dedupe.includes(getPackageName(importee));
206
-
207
- function getCachedPackageInfo(pkg, pkgPath) {
208
- if (packageInfoCache.has(pkgPath)) {
209
- return packageInfoCache.get(pkgPath);
210
- } // browserify/resolve doesn't realpath paths returned in its packageFilter callback
211
-
348
+ if (typeof dedupe !== 'function') {
349
+ dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
350
+ }
212
351
 
213
- if (!preserveSymlinks) {
214
- pkgPath = fs.realpathSync(pkgPath);
352
+ const resolveOnly = options.resolveOnly.map(pattern => {
353
+ if (pattern instanceof RegExp) {
354
+ return pattern;
215
355
  }
216
356
 
217
- const pkgRoot = path.dirname(pkgPath);
218
- const packageInfo = {
219
- // copy as we are about to munge the `main` field of `pkg`.
220
- packageJson: Object.assign({}, pkg),
221
- // path to package.json file
222
- packageJsonPath: pkgPath,
223
- // directory containing the package.json
224
- root: pkgRoot,
225
- // which main field was used during resolution of this module (main, module, or browser)
226
- resolvedMainField: 'main',
227
- // whether the browser map was used to resolve the entry point to this module
228
- browserMappedMain: false,
229
- // the entry point of the module with respect to the selected main field and any
230
- // relevant browser mappings.
231
- resolvedEntryPoint: ''
232
- };
233
- let overriddenMain = false;
234
-
235
- for (let i = 0; i < mainFields.length; i++) {
236
- const field = mainFields[i];
357
+ const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
358
+ return new RegExp(`^${normalized}$`);
359
+ });
360
+ const browserMapCache = new Map();
361
+ let preserveSymlinks;
362
+ return {
363
+ name: 'node-resolve',
237
364
 
238
- if (typeof pkg[field] === 'string') {
239
- pkg.main = pkg[field];
240
- packageInfo.resolvedMainField = field;
241
- overriddenMain = true;
242
- break;
365
+ buildStart(options) {
366
+ rollupOptions = options;
367
+ var _iteratorNormalCompletion = true;
368
+ var _didIteratorError = false;
369
+ var _iteratorError = undefined;
370
+
371
+ try {
372
+ for (var _iterator = warnings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
373
+ const warning = _step.value;
374
+ this.warn(warning);
375
+ }
376
+ } catch (err) {
377
+ _didIteratorError = true;
378
+ _iteratorError = err;
379
+ } finally {
380
+ try {
381
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
382
+ _iterator.return();
383
+ }
384
+ } finally {
385
+ if (_didIteratorError) {
386
+ throw _iteratorError;
387
+ }
388
+ }
243
389
  }
244
- }
245
390
 
246
- const internalPackageInfo = {
247
- cachedPkg: pkg,
248
- hasModuleSideEffects: alwaysNull,
249
- hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
250
- packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => {
251
- let resolved = pkg.browser[key];
391
+ preserveSymlinks = options.preserveSymlinks;
392
+ },
252
393
 
253
- if (resolved && resolved[0] === '.') {
254
- resolved = path.resolve(pkgRoot, resolved);
255
- }
394
+ generateBundle() {
395
+ readCachedFile.clear();
396
+ isFileCached.clear();
397
+ isDirCached.clear();
398
+ },
256
399
 
257
- browser[key] = resolved;
400
+ resolveId(importee, importer) {
401
+ var _this = this;
258
402
 
259
- if (key[0] === '.') {
260
- const absoluteKey = path.resolve(pkgRoot, key);
261
- browser[absoluteKey] = resolved;
403
+ return _asyncToGenerator(function* () {
404
+ if (importee === ES6_BROWSER_EMPTY) {
405
+ return importee;
406
+ } // ignore IDs with null character, these belong to other plugins
262
407
 
263
- if (!path.extname(key)) {
264
- extensions.reduce((browser, ext) => {
265
- browser[absoluteKey + ext] = browser[key];
266
- return browser;
267
- }, browser);
268
- }
269
- }
270
408
 
271
- return browser;
272
- }, {}),
273
- packageInfo
274
- };
275
- const browserMap = internalPackageInfo.packageBrowserField;
276
-
277
- if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins
278
- browserMap.hasOwnProperty(pkg.main)) {
279
- packageInfo.resolvedEntryPoint = browserMap[pkg.main];
280
- packageInfo.browserMappedMain = true;
281
- } else {
282
- // index.node is technically a valid default entrypoint as well...
283
- packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js');
284
- packageInfo.browserMappedMain = false;
285
- }
286
-
287
- const packageSideEffects = pkg.sideEffects;
409
+ if (/\0/.test(importee)) return null;
410
+ const basedir = !importer || dedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec
288
411
 
289
- if (typeof packageSideEffects === 'boolean') {
290
- internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
291
- } else if (Array.isArray(packageSideEffects)) {
292
- internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
293
- resolve: pkgRoot
294
- });
295
- }
412
+ const browser = browserMapCache.get(importer);
296
413
 
297
- packageInfoCache.set(pkgPath, internalPackageInfo);
298
- return internalPackageInfo;
299
- }
414
+ if (useBrowserOverrides && browser) {
415
+ const resolvedImportee = path.resolve(basedir, importee);
300
416
 
301
- return {
302
- name: 'node-resolve',
417
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
418
+ return ES6_BROWSER_EMPTY;
419
+ }
303
420
 
304
- buildStart(options) {
305
- preserveSymlinks = options.preserveSymlinks;
421
+ const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
306
422
 
307
- const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number),
308
- _this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2),
309
- major = _this$meta$rollupVers2[0],
310
- minor = _this$meta$rollupVers2[1];
423
+ if (browserImportee) {
424
+ importee = browserImportee;
425
+ }
426
+ }
311
427
 
312
- const minVersion = peerDependencies.rollup.slice(2);
428
+ const parts = importee.split(/[/\\]/);
429
+ let id = parts.shift();
313
430
 
314
- const _minVersion$split$map = minVersion.split('.').map(Number),
315
- _minVersion$split$map2 = _slicedToArray(_minVersion$split$map, 2),
316
- minMajor = _minVersion$split$map2[0],
317
- minMinor = _minVersion$split$map2[1];
431
+ if (id[0] === '@' && parts.length > 0) {
432
+ // scoped packages
433
+ id += `/${parts.shift()}`;
434
+ } else if (id[0] === '.') {
435
+ // an import relative to the parent dir of the importer
436
+ id = path.resolve(basedir, importee);
437
+ }
318
438
 
319
- if (major < minMajor || major === minMajor && minor < minMinor) {
320
- this.error(`Insufficient Rollup version: "rollup-plugin-node-resolve" requires at least rollup@${minVersion} but found rollup@${this.meta.rollupVersion}.`);
321
- }
322
- },
439
+ const input = normalizeInput(rollupOptions.input);
323
440
 
324
- generateBundle() {
325
- readFileCached.clear();
326
- isFileCached.clear();
327
- isDirCached.clear();
328
- },
441
+ if (resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) {
442
+ if (input.includes(id)) {
443
+ return null;
444
+ }
329
445
 
330
- resolveId(importee, importer) {
331
- if (importee === ES6_BROWSER_EMPTY) {
332
- return importee;
333
- } // ignore IDs with null character, these belong to other plugins
446
+ return false;
447
+ }
334
448
 
449
+ let hasModuleSideEffects = nullFn;
450
+ let hasPackageEntry = true;
451
+ let packageBrowserField = false;
452
+ let packageInfo;
453
+
454
+ const filter = (pkg, pkgPath) => {
455
+ const info = getPackageInfo({
456
+ cache: packageInfoCache,
457
+ extensions,
458
+ pkg,
459
+ pkgPath,
460
+ mainFields,
461
+ preserveSymlinks,
462
+ useBrowserOverrides
463
+ });
464
+ packageInfo = info.packageInfo;
465
+ hasModuleSideEffects = info.hasModuleSideEffects;
466
+ hasPackageEntry = info.hasPackageEntry;
467
+ packageBrowserField = info.packageBrowserField;
468
+ return info.cachedPkg;
469
+ };
335
470
 
336
- if (/\0/.test(importee)) return null;
337
- const basedir = !importer || shouldDedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec
471
+ let resolveOptions = {
472
+ basedir,
473
+ packageFilter: filter,
474
+ readFile: readCachedFile,
475
+ isFile: isFileCached,
476
+ isDirectory: isDirCached,
477
+ extensions
478
+ };
338
479
 
339
- const browser = browserMapCache.get(importer);
480
+ if (preserveSymlinks !== undefined) {
481
+ resolveOptions.preserveSymlinks = preserveSymlinks;
482
+ }
340
483
 
341
- if (useBrowserOverrides && browser) {
342
- const resolvedImportee = path.resolve(basedir, importee);
484
+ const importSpecifierList = [];
343
485
 
344
- if (browser[importee] === false || browser[resolvedImportee] === false) {
345
- return ES6_BROWSER_EMPTY;
486
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
487
+ // For module graph roots (i.e. when importer is undefined), we
488
+ // need to handle 'path fragments` like `foo/bar` that are commonly
489
+ // found in rollup config files. If importee doesn't look like a
490
+ // relative or absolute path, we make it relative and attempt to
491
+ // resolve it. If we don't find anything, we try resolving it as we
492
+ // got it.
493
+ importSpecifierList.push(`./${importee}`);
346
494
  }
347
495
 
348
- const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
496
+ const importeeIsBuiltin = builtins.has(importee);
349
497
 
350
- if (browserImportee) {
351
- importee = browserImportee;
498
+ if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
499
+ // The `resolve` library will not resolve packages with the same
500
+ // name as a node built-in module. If we're resolving something
501
+ // that's a builtin, and we don't prefer to find built-ins, we
502
+ // first try to look up a local module with that name. If we don't
503
+ // find anything, we resolve the builtin which just returns back
504
+ // the built-in's name.
505
+ importSpecifierList.push(`${importee}/`);
352
506
  }
353
- }
354
-
355
- const parts = importee.split(/[/\\]/);
356
- let id = parts.shift();
357
507
 
358
- if (id[0] === '@' && parts.length > 0) {
359
- // scoped packages
360
- id += `/${parts.shift()}`;
361
- } else if (id[0] === '.') {
362
- // an import relative to the parent dir of the importer
363
- id = path.resolve(basedir, importee);
364
- }
508
+ importSpecifierList.push(importee);
509
+ resolveOptions = Object.assign(resolveOptions, customResolveOptions);
365
510
 
366
- if (only && !only.some(pattern => pattern.test(id))) return null;
367
- let hasModuleSideEffects = alwaysNull;
368
- let hasPackageEntry = true;
369
- let packageBrowserField = false;
370
- let packageInfo;
371
- const resolveOptions = {
372
- basedir,
373
-
374
- packageFilter(pkg, pkgPath) {
375
- let cachedPkg;
376
-
377
- var _getCachedPackageInfo = getCachedPackageInfo(pkg, pkgPath);
378
-
379
- packageInfo = _getCachedPackageInfo.packageInfo;
380
- cachedPkg = _getCachedPackageInfo.cachedPkg;
381
- hasModuleSideEffects = _getCachedPackageInfo.hasModuleSideEffects;
382
- hasPackageEntry = _getCachedPackageInfo.hasPackageEntry;
383
- packageBrowserField = _getCachedPackageInfo.packageBrowserField;
384
- return cachedPkg;
385
- },
386
-
387
- readFile: readFileCached,
388
- isFile: isFileCached,
389
- isDirectory: isDirCached,
390
- extensions
391
- };
392
-
393
- if (preserveSymlinks !== undefined) {
394
- resolveOptions.preserveSymlinks = preserveSymlinks;
395
- }
511
+ try {
512
+ let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions);
396
513
 
397
- const importSpecifierList = [];
514
+ if (resolved && packageBrowserField) {
515
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
516
+ if (!packageBrowserField[resolved]) {
517
+ browserMapCache.set(resolved, packageBrowserField);
518
+ return ES6_BROWSER_EMPTY;
519
+ }
398
520
 
399
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
400
- // For module graph roots (i.e. when importer is undefined), we
401
- // need to handle 'path fragments` like `foo/bar` that are commonly
402
- // found in rollup config files. If importee doesn't look like a
403
- // relative or absolute path, we make it relative and attempt to
404
- // resolve it. If we don't find anything, we try resolving it as we
405
- // got it.
406
- importSpecifierList.push(`./${importee}`);
407
- }
521
+ resolved = packageBrowserField[resolved];
522
+ }
408
523
 
409
- const importeeIsBuiltin = builtins.has(importee);
524
+ browserMapCache.set(resolved, packageBrowserField);
525
+ }
410
526
 
411
- if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
412
- // The `resolve` library will not resolve packages with the same
413
- // name as a node built-in module. If we're resolving something
414
- // that's a builtin, and we don't prefer to find built-ins, we
415
- // first try to look up a local module with that name. If we don't
416
- // find anything, we resolve the builtin which just returns back
417
- // the built-in's name.
418
- importSpecifierList.push(`${importee}/`);
419
- }
527
+ if (hasPackageEntry && !preserveSymlinks && resolved) {
528
+ const fileExists = yield exists(resolved);
420
529
 
421
- importSpecifierList.push(importee);
422
- return resolveImportSpecifiers(importSpecifierList, Object.assign(resolveOptions, customResolveOptions)).then(resolved => {
423
- if (resolved && packageBrowserField) {
424
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
425
- if (!packageBrowserField[resolved]) {
426
- browserMapCache.set(resolved, packageBrowserField);
427
- return ES6_BROWSER_EMPTY;
530
+ if (fileExists) {
531
+ resolved = yield realpath(resolved);
428
532
  }
429
-
430
- resolved = packageBrowserField[resolved];
431
533
  }
432
534
 
433
- browserMapCache.set(resolved, packageBrowserField);
434
- }
535
+ idToPackageInfo.set(resolved, packageInfo);
435
536
 
436
- if (hasPackageEntry && !preserveSymlinks && resolved) {
437
- return existsAsync(resolved).then(exists => exists ? realpathAsync(resolved) : resolved);
438
- }
537
+ if (hasPackageEntry) {
538
+ if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
539
+ return null;
540
+ } else if (importeeIsBuiltin && preferBuiltins) {
541
+ if (!isPreferBuiltinsSet) {
542
+ _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`);
543
+ }
439
544
 
440
- return resolved;
441
- }).then(resolved => {
442
- idToPackageInfo.set(resolved, packageInfo);
545
+ return null;
546
+ } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
547
+ return null;
548
+ }
549
+ }
443
550
 
444
- if (hasPackageEntry) {
445
- if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
446
- return null;
447
- } else if (importeeIsBuiltin && preferBuiltins) {
448
- if (!isPreferBuiltinsSet) {
449
- 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`);
551
+ if (resolved && options.modulesOnly) {
552
+ const code = yield readFile(resolved, 'utf-8');
553
+
554
+ if (isModule(code)) {
555
+ return {
556
+ id: resolved,
557
+ moduleSideEffects: hasModuleSideEffects(resolved)
558
+ };
450
559
  }
451
560
 
452
- return null;
453
- } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
454
561
  return null;
455
562
  }
456
- }
457
563
 
458
- if (resolved && options.modulesOnly) {
459
- return readFileAsync(resolved).then(code => isModule(code) ? {
564
+ const result = {
460
565
  id: resolved,
461
566
  moduleSideEffects: hasModuleSideEffects(resolved)
462
- } : null);
567
+ };
568
+ return result;
569
+ } catch (error) {
570
+ return null;
463
571
  }
464
-
465
- return {
466
- id: resolved,
467
- moduleSideEffects: hasModuleSideEffects(resolved)
468
- };
469
- }).catch(() => null);
572
+ })();
470
573
  },
471
574
 
472
575
  load(importee) {