@rollup/plugin-node-resolve 8.4.0 → 9.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/dist/cjs/index.js CHANGED
@@ -6,7 +6,6 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
6
6
 
7
7
  var path = require('path');
8
8
  var builtinList = _interopDefault(require('builtin-modules'));
9
- var deepFreeze = _interopDefault(require('deep-freeze'));
10
9
  var deepMerge = _interopDefault(require('deepmerge'));
11
10
  var isModule = _interopDefault(require('is-module'));
12
11
  var fs = require('fs');
@@ -15,303 +14,141 @@ var util = require('util');
15
14
  var pluginutils = require('@rollup/pluginutils');
16
15
  var resolveModule = _interopDefault(require('resolve'));
17
16
 
18
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
19
- try {
20
- var info = gen[key](arg);
21
- var value = info.value;
22
- } catch (error) {
23
- reject(error);
24
- return;
25
- }
26
-
27
- if (info.done) {
28
- resolve(value);
29
- } else {
30
- Promise.resolve(value).then(_next, _throw);
31
- }
32
- }
33
-
34
- function _asyncToGenerator(fn) {
35
- return function () {
36
- var self = this,
37
- args = arguments;
38
- return new Promise(function (resolve, reject) {
39
- var gen = fn.apply(self, args);
40
-
41
- function _next(value) {
42
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
43
- }
44
-
45
- function _throw(err) {
46
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
47
- }
48
-
49
- _next(undefined);
50
- });
51
- };
52
- }
53
-
54
- function _slicedToArray(arr, i) {
55
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
56
- }
57
-
58
- function _arrayWithHoles(arr) {
59
- if (Array.isArray(arr)) return arr;
60
- }
61
-
62
- function _iterableToArrayLimit(arr, i) {
63
- if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
64
- var _arr = [];
65
- var _n = true;
66
- var _d = false;
67
- var _e = undefined;
68
-
69
- try {
70
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
71
- _arr.push(_s.value);
72
-
73
- if (i && _arr.length === i) break;
74
- }
75
- } catch (err) {
76
- _d = true;
77
- _e = err;
78
- } finally {
79
- try {
80
- if (!_n && _i["return"] != null) _i["return"]();
81
- } finally {
82
- if (_d) throw _e;
83
- }
84
- }
85
-
86
- return _arr;
87
- }
88
-
89
- function _unsupportedIterableToArray(o, minLen) {
90
- if (!o) return;
91
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
92
- var n = Object.prototype.toString.call(o).slice(8, -1);
93
- if (n === "Object" && o.constructor) n = o.constructor.name;
94
- if (n === "Map" || n === "Set") return Array.from(n);
95
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
96
- }
97
-
98
- function _arrayLikeToArray(arr, len) {
99
- if (len == null || len > arr.length) len = arr.length;
100
-
101
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
102
-
103
- return arr2;
104
- }
105
-
106
- function _nonIterableRest() {
107
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
108
- }
109
-
110
- function _createForOfIteratorHelper(o) {
111
- if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
112
- if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
113
- var i = 0;
114
-
115
- var F = function () {};
116
-
117
- return {
118
- s: F,
119
- n: function () {
120
- if (i >= o.length) return {
121
- done: true
122
- };
123
- return {
124
- done: false,
125
- value: o[i++]
126
- };
127
- },
128
- e: function (e) {
129
- throw e;
130
- },
131
- f: F
132
- };
133
- }
134
-
135
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
136
- }
137
-
138
- var it,
139
- normalCompletion = true,
140
- didErr = false,
141
- err;
142
- return {
143
- s: function () {
144
- it = o[Symbol.iterator]();
145
- },
146
- n: function () {
147
- var step = it.next();
148
- normalCompletion = step.done;
149
- return step;
150
- },
151
- e: function (e) {
152
- didErr = true;
153
- err = e;
154
- },
155
- f: function () {
156
- try {
157
- if (!normalCompletion && it.return != null) it.return();
158
- } finally {
159
- if (didErr) throw err;
160
- }
161
- }
162
- };
163
- }
164
-
165
17
  const exists = util.promisify(fs__default.exists);
166
18
  const readFile = util.promisify(fs__default.readFile);
167
19
  const realpath = util.promisify(fs__default.realpath);
168
20
  const stat = util.promisify(fs__default.stat);
169
21
 
170
- const onError = error => {
22
+ const onError = (error) => {
171
23
  if (error.code === 'ENOENT') {
172
24
  return false;
173
25
  }
174
-
175
26
  throw error;
176
27
  };
177
28
 
178
- const makeCache = fn => {
29
+ const makeCache = (fn) => {
179
30
  const cache = new Map();
180
-
181
- const wrapped = /*#__PURE__*/function () {
182
- var _ref = _asyncToGenerator(function* (param, done) {
183
- if (cache.has(param) === false) {
184
- cache.set(param, fn(param).catch(err => {
31
+ const wrapped = async (param, done) => {
32
+ if (cache.has(param) === false) {
33
+ cache.set(
34
+ param,
35
+ fn(param).catch((err) => {
185
36
  cache.delete(param);
186
37
  throw err;
187
- }));
188
- }
189
-
190
- try {
191
- const result = cache.get(param);
192
- const value = yield result;
193
- return done(null, value);
194
- } catch (error) {
195
- return done(error);
196
- }
197
- });
38
+ })
39
+ );
40
+ }
198
41
 
199
- return function wrapped(_x, _x2) {
200
- return _ref.apply(this, arguments);
201
- };
202
- }();
42
+ try {
43
+ const result = cache.get(param);
44
+ const value = await result;
45
+ return done(null, value);
46
+ } catch (error) {
47
+ return done(error);
48
+ }
49
+ };
203
50
 
204
51
  wrapped.clear = () => cache.clear();
205
52
 
206
53
  return wrapped;
207
54
  };
208
55
 
209
- const isDirCached = makeCache( /*#__PURE__*/function () {
210
- var _ref2 = _asyncToGenerator(function* (file) {
211
- try {
212
- const stats = yield stat(file);
213
- return stats.isDirectory();
214
- } catch (error) {
215
- return onError(error);
216
- }
217
- });
56
+ const isDirCached = makeCache(async (file) => {
57
+ try {
58
+ const stats = await stat(file);
59
+ return stats.isDirectory();
60
+ } catch (error) {
61
+ return onError(error);
62
+ }
63
+ });
218
64
 
219
- return function (_x3) {
220
- return _ref2.apply(this, arguments);
221
- };
222
- }());
223
- const isFileCached = makeCache( /*#__PURE__*/function () {
224
- var _ref3 = _asyncToGenerator(function* (file) {
225
- try {
226
- const stats = yield stat(file);
227
- return stats.isFile();
228
- } catch (error) {
229
- return onError(error);
230
- }
231
- });
65
+ const isFileCached = makeCache(async (file) => {
66
+ try {
67
+ const stats = await stat(file);
68
+ return stats.isFile();
69
+ } catch (error) {
70
+ return onError(error);
71
+ }
72
+ });
232
73
 
233
- return function (_x4) {
234
- return _ref3.apply(this, arguments);
235
- };
236
- }());
237
74
  const readCachedFile = makeCache(readFile);
238
75
 
239
- const resolveId = util.promisify(resolveModule); // returns the imported package name for bare module imports
76
+ const resolveId = util.promisify(resolveModule);
240
77
 
78
+ // returns the imported package name for bare module imports
241
79
  function getPackageName(id) {
242
80
  if (id.startsWith('.') || id.startsWith('/')) {
243
81
  return null;
244
82
  }
245
83
 
246
- const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package
247
- // @my-scope/my-package -> @my-scope/my-package
84
+ const split = id.split('/');
248
85
 
86
+ // @my-scope/my-package/foo.js -> @my-scope/my-package
87
+ // @my-scope/my-package -> @my-scope/my-package
249
88
  if (split[0][0] === '@') {
250
89
  return `${split[0]}/${split[1]}`;
251
- } // my-package/foo.js -> my-package
252
- // my-package -> my-package
253
-
90
+ }
254
91
 
92
+ // my-package/foo.js -> my-package
93
+ // my-package -> my-package
255
94
  return split[0];
256
95
  }
96
+
257
97
  function getMainFields(options) {
258
98
  let mainFields;
259
-
260
99
  if (options.mainFields) {
261
- mainFields = options.mainFields;
100
+ ({ mainFields } = options);
262
101
  } else {
263
102
  mainFields = ['module', 'main'];
264
103
  }
265
-
266
104
  if (options.browser && mainFields.indexOf('browser') === -1) {
267
105
  return ['browser'].concat(mainFields);
268
106
  }
269
-
270
107
  if (!mainFields.length) {
271
108
  throw new Error('Please ensure at least one `mainFields` value is specified');
272
109
  }
273
-
274
110
  return mainFields;
275
111
  }
112
+
276
113
  function getPackageInfo(options) {
277
- const cache = options.cache,
278
- extensions = options.extensions,
279
- pkg = options.pkg,
280
- mainFields = options.mainFields,
281
- preserveSymlinks = options.preserveSymlinks,
282
- useBrowserOverrides = options.useBrowserOverrides;
283
- let pkgPath = options.pkgPath;
114
+ const { cache, extensions, pkg, mainFields, preserveSymlinks, useBrowserOverrides } = options;
115
+ let { pkgPath } = options;
284
116
 
285
117
  if (cache.has(pkgPath)) {
286
118
  return cache.get(pkgPath);
287
- } // browserify/resolve doesn't realpath paths returned in its packageFilter callback
288
-
119
+ }
289
120
 
121
+ // browserify/resolve doesn't realpath paths returned in its packageFilter callback
290
122
  if (!preserveSymlinks) {
291
123
  pkgPath = fs.realpathSync(pkgPath);
292
124
  }
293
125
 
294
126
  const pkgRoot = path.dirname(pkgPath);
127
+
295
128
  const packageInfo = {
296
129
  // copy as we are about to munge the `main` field of `pkg`.
297
130
  packageJson: Object.assign({}, pkg),
131
+
298
132
  // path to package.json file
299
133
  packageJsonPath: pkgPath,
134
+
300
135
  // directory containing the package.json
301
136
  root: pkgRoot,
137
+
302
138
  // which main field was used during resolution of this module (main, module, or browser)
303
139
  resolvedMainField: 'main',
140
+
304
141
  // whether the browser map was used to resolve the entry point to this module
305
142
  browserMappedMain: false,
143
+
306
144
  // the entry point of the module with respect to the selected main field and any
307
145
  // relevant browser mappings.
308
146
  resolvedEntryPoint: ''
309
147
  };
310
- let overriddenMain = false;
311
148
 
149
+ let overriddenMain = false;
312
150
  for (let i = 0; i < mainFields.length; i++) {
313
151
  const field = mainFields[i];
314
-
315
152
  if (typeof pkg[field] === 'string') {
316
153
  pkg.main = pkg[field];
317
154
  packageInfo.resolvedMainField = field;
@@ -324,37 +161,38 @@ function getPackageInfo(options) {
324
161
  cachedPkg: pkg,
325
162
  hasModuleSideEffects: () => null,
326
163
  hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
327
- packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => {
328
- let resolved = pkg.browser[key];
329
-
330
- if (resolved && resolved[0] === '.') {
331
- resolved = path.resolve(pkgRoot, resolved);
332
- }
333
- /* eslint-disable no-param-reassign */
334
-
335
-
336
- browser[key] = resolved;
337
-
338
- if (key[0] === '.') {
339
- const absoluteKey = path.resolve(pkgRoot, key);
340
- browser[absoluteKey] = resolved;
341
-
342
- if (!path.extname(key)) {
343
- extensions.reduce((subBrowser, ext) => {
344
- subBrowser[absoluteKey + ext] = subBrowser[key];
345
- return subBrowser;
346
- }, browser);
164
+ packageBrowserField:
165
+ useBrowserOverrides &&
166
+ typeof pkg.browser === 'object' &&
167
+ Object.keys(pkg.browser).reduce((browser, key) => {
168
+ let resolved = pkg.browser[key];
169
+ if (resolved && resolved[0] === '.') {
170
+ resolved = path.resolve(pkgRoot, resolved);
347
171
  }
348
- }
349
-
350
- return browser;
351
- }, {}),
172
+ /* eslint-disable no-param-reassign */
173
+ browser[key] = resolved;
174
+ if (key[0] === '.') {
175
+ const absoluteKey = path.resolve(pkgRoot, key);
176
+ browser[absoluteKey] = resolved;
177
+ if (!path.extname(key)) {
178
+ extensions.reduce((subBrowser, ext) => {
179
+ subBrowser[absoluteKey + ext] = subBrowser[key];
180
+ return subBrowser;
181
+ }, browser);
182
+ }
183
+ }
184
+ return browser;
185
+ }, {}),
352
186
  packageInfo
353
187
  };
354
- const browserMap = internalPackageInfo.packageBrowserField;
355
188
 
356
- if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins
357
- browserMap.hasOwnProperty(pkg.main)) {
189
+ const browserMap = internalPackageInfo.packageBrowserField;
190
+ if (
191
+ useBrowserOverrides &&
192
+ typeof pkg.browser === 'object' &&
193
+ // eslint-disable-next-line no-prototype-builtins
194
+ browserMap.hasOwnProperty(pkg.main)
195
+ ) {
358
196
  packageInfo.resolvedEntryPoint = browserMap[pkg.main];
359
197
  packageInfo.browserMappedMain = true;
360
198
  } else {
@@ -364,7 +202,6 @@ function getPackageInfo(options) {
364
202
  }
365
203
 
366
204
  const packageSideEffects = pkg.sideEffects;
367
-
368
205
  if (typeof packageSideEffects === 'boolean') {
369
206
  internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
370
207
  } else if (Array.isArray(packageSideEffects)) {
@@ -376,40 +213,41 @@ function getPackageInfo(options) {
376
213
  cache.set(pkgPath, internalPackageInfo);
377
214
  return internalPackageInfo;
378
215
  }
216
+
379
217
  function normalizeInput(input) {
380
218
  if (Array.isArray(input)) {
381
219
  return input;
382
220
  } else if (typeof input === 'object') {
383
221
  return Object.values(input);
384
- } // otherwise it's a string
385
-
222
+ }
386
223
 
224
+ // otherwise it's a string
387
225
  return [input];
388
- } // Resolve module specifiers in order. Promise resolves to the first module that resolves
389
- // successfully, or the error that resulted from the last attempted module resolution.
226
+ }
390
227
 
228
+ // Resolve module specifiers in order. Promise resolves to the first module that resolves
229
+ // successfully, or the error that resulted from the last attempted module resolution.
391
230
  function resolveImportSpecifiers(importSpecifierList, resolveOptions) {
392
231
  let promise = Promise.resolve();
393
232
 
394
233
  for (let i = 0; i < importSpecifierList.length; i++) {
395
- promise = promise.then(value => {
234
+ promise = promise.then((value) => {
396
235
  // if we've already resolved to something, just return it.
397
236
  if (value) {
398
237
  return value;
399
238
  }
400
239
 
401
- return resolveId(importSpecifierList[i], resolveOptions).then(result => {
240
+ return resolveId(importSpecifierList[i], resolveOptions).then((result) => {
402
241
  if (!resolveOptions.preserveSymlinks) {
403
242
  result = fs.realpathSync(result);
404
243
  }
405
-
406
244
  return result;
407
245
  });
408
246
  });
409
247
 
410
248
  if (i < importSpecifierList.length - 1) {
411
249
  // swallow MODULE_NOT_FOUND errors from all but the last resolution
412
- promise = promise.catch(error => {
250
+ promise = promise.catch((error) => {
413
251
  if (error.code !== 'MODULE_NOT_FOUND') {
414
252
  throw error;
415
253
  }
@@ -420,11 +258,22 @@ function resolveImportSpecifiers(importSpecifierList, resolveOptions) {
420
258
  return promise;
421
259
  }
422
260
 
261
+ /* eslint-disable no-param-reassign, no-shadow, no-undefined */
262
+
423
263
  const builtins = new Set(builtinList);
424
264
  const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
425
-
426
265
  const nullFn = () => null;
266
+ const deepFreeze = (object) => {
267
+ Object.freeze(object);
268
+
269
+ for (const value of Object.values(object)) {
270
+ if (typeof value === 'object' && !Object.isFrozen(value)) {
271
+ deepFreeze(value);
272
+ }
273
+ }
427
274
 
275
+ return object;
276
+ };
428
277
  const defaults = {
429
278
  customResolveOptions: {},
430
279
  dedupe: [],
@@ -434,11 +283,10 @@ const defaults = {
434
283
  resolveOnly: []
435
284
  };
436
285
  const DEFAULTS = deepFreeze(deepMerge({}, defaults));
286
+
437
287
  function nodeResolve(opts = {}) {
438
288
  const options = Object.assign({}, defaults, opts);
439
- const customResolveOptions = options.customResolveOptions,
440
- extensions = options.extensions,
441
- jail = options.jail;
289
+ const { customResolveOptions, extensions, jail } = options;
442
290
  const warnings = [];
443
291
  const packageInfoCache = new Map();
444
292
  const idToPackageInfo = new Map();
@@ -447,7 +295,7 @@ function nodeResolve(opts = {}) {
447
295
  const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
448
296
  const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
449
297
  const rootDir = options.rootDir || process.cwd();
450
- let dedupe = options.dedupe;
298
+ let { dedupe } = options;
451
299
  let rollupOptions;
452
300
 
453
301
  if (options.only) {
@@ -456,40 +304,32 @@ function nodeResolve(opts = {}) {
456
304
  }
457
305
 
458
306
  if (typeof dedupe !== 'function') {
459
- dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
307
+ dedupe = (importee) =>
308
+ options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
460
309
  }
461
310
 
462
- const resolveOnly = options.resolveOnly.map(pattern => {
311
+ const resolveOnly = options.resolveOnly.map((pattern) => {
463
312
  if (pattern instanceof RegExp) {
464
313
  return pattern;
465
314
  }
466
-
467
315
  const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
468
316
  return new RegExp(`^${normalized}$`);
469
317
  });
318
+
470
319
  const browserMapCache = new Map();
471
320
  let preserveSymlinks;
321
+
472
322
  return {
473
323
  name: 'node-resolve',
474
324
 
475
325
  buildStart(options) {
476
326
  rollupOptions = options;
477
327
 
478
- var _iterator = _createForOfIteratorHelper(warnings),
479
- _step;
480
-
481
- try {
482
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
483
- const warning = _step.value;
484
- this.warn(warning);
485
- }
486
- } catch (err) {
487
- _iterator.e(err);
488
- } finally {
489
- _iterator.f();
328
+ for (const warning of warnings) {
329
+ this.warn(warning);
490
330
  }
491
331
 
492
- preserveSymlinks = options.preserveSymlinks;
332
+ ({ preserveSymlinks } = options);
493
333
  },
494
334
 
495
335
  generateBundle() {
@@ -498,217 +338,200 @@ function nodeResolve(opts = {}) {
498
338
  isDirCached.clear();
499
339
  },
500
340
 
501
- resolveId(importee, importer) {
502
- var _this = this;
503
-
504
- return _asyncToGenerator(function* () {
505
- if (importee === ES6_BROWSER_EMPTY) {
506
- return importee;
507
- } // ignore IDs with null character, these belong to other plugins
508
-
509
-
510
- if (/\0/.test(importee)) return null; // strip hash and query params from import
511
-
512
- const _importee$split = importee.split('#'),
513
- _importee$split2 = _slicedToArray(_importee$split, 2),
514
- withoutHash = _importee$split2[0],
515
- hash = _importee$split2[1];
516
-
517
- const _withoutHash$split = withoutHash.split('?'),
518
- _withoutHash$split2 = _slicedToArray(_withoutHash$split, 2),
519
- importPath = _withoutHash$split2[0],
520
- params = _withoutHash$split2[1];
521
-
522
- const importSuffix = `${params ? `?${params}` : ''}${hash ? `#${hash}` : ''}`;
523
- importee = importPath;
524
- const basedir = !importer || dedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec
525
-
526
- const browser = browserMapCache.get(importer);
527
-
528
- if (useBrowserOverrides && browser) {
529
- const resolvedImportee = path.resolve(basedir, importee);
530
-
531
- if (browser[importee] === false || browser[resolvedImportee] === false) {
532
- return ES6_BROWSER_EMPTY;
533
- }
534
-
535
- const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
536
-
537
- if (browserImportee) {
538
- importee = browserImportee;
539
- }
341
+ async resolveId(importee, importer) {
342
+ if (importee === ES6_BROWSER_EMPTY) {
343
+ return importee;
344
+ }
345
+ // ignore IDs with null character, these belong to other plugins
346
+ if (/\0/.test(importee)) return null;
347
+
348
+ // strip hash and query params from import
349
+ const [withoutHash, hash] = importee.split('#');
350
+ const [importPath, params] = withoutHash.split('?');
351
+ const importSuffix = `${params ? `?${params}` : ''}${hash ? `#${hash}` : ''}`;
352
+ importee = importPath;
353
+
354
+ const basedir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
355
+
356
+ // https://github.com/defunctzombie/package-browser-field-spec
357
+ const browser = browserMapCache.get(importer);
358
+ if (useBrowserOverrides && browser) {
359
+ const resolvedImportee = path.resolve(basedir, importee);
360
+ if (browser[importee] === false || browser[resolvedImportee] === false) {
361
+ return ES6_BROWSER_EMPTY;
540
362
  }
541
-
542
- const parts = importee.split(/[/\\]/);
543
- let id = parts.shift();
544
- let isRelativeImport = false;
545
-
546
- if (id[0] === '@' && parts.length > 0) {
547
- // scoped packages
548
- id += `/${parts.shift()}`;
549
- } else if (id[0] === '.') {
550
- // an import relative to the parent dir of the importer
551
- id = path.resolve(basedir, importee);
552
- isRelativeImport = true;
363
+ const browserImportee =
364
+ browser[importee] ||
365
+ browser[resolvedImportee] ||
366
+ browser[`${resolvedImportee}.js`] ||
367
+ browser[`${resolvedImportee}.json`];
368
+ if (browserImportee) {
369
+ importee = browserImportee;
553
370
  }
371
+ }
554
372
 
555
- if (!isRelativeImport && resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) {
556
- if (normalizeInput(rollupOptions.input).includes(importee)) {
557
- return null;
558
- }
373
+ const parts = importee.split(/[/\\]/);
374
+ let id = parts.shift();
375
+ let isRelativeImport = false;
376
+
377
+ if (id[0] === '@' && parts.length > 0) {
378
+ // scoped packages
379
+ id += `/${parts.shift()}`;
380
+ } else if (id[0] === '.') {
381
+ // an import relative to the parent dir of the importer
382
+ id = path.resolve(basedir, importee);
383
+ isRelativeImport = true;
384
+ }
559
385
 
560
- return false;
386
+ if (
387
+ !isRelativeImport &&
388
+ resolveOnly.length &&
389
+ !resolveOnly.some((pattern) => pattern.test(id))
390
+ ) {
391
+ if (normalizeInput(rollupOptions.input).includes(importee)) {
392
+ return null;
561
393
  }
394
+ return false;
395
+ }
562
396
 
563
- let hasModuleSideEffects = nullFn;
564
- let hasPackageEntry = true;
565
- let packageBrowserField = false;
566
- let packageInfo;
567
-
568
- const filter = (pkg, pkgPath) => {
569
- const info = getPackageInfo({
570
- cache: packageInfoCache,
571
- extensions,
572
- pkg,
573
- pkgPath,
574
- mainFields,
575
- preserveSymlinks,
576
- useBrowserOverrides
577
- });
578
- packageInfo = info.packageInfo;
579
- hasModuleSideEffects = info.hasModuleSideEffects;
580
- hasPackageEntry = info.hasPackageEntry;
581
- packageBrowserField = info.packageBrowserField;
582
- return info.cachedPkg;
583
- };
584
-
585
- let resolveOptions = {
586
- basedir,
587
- packageFilter: filter,
588
- readFile: readCachedFile,
589
- isFile: isFileCached,
590
- isDirectory: isDirCached,
591
- extensions
592
- };
593
-
594
- if (preserveSymlinks !== undefined) {
595
- resolveOptions.preserveSymlinks = preserveSymlinks;
596
- }
397
+ let hasModuleSideEffects = nullFn;
398
+ let hasPackageEntry = true;
399
+ let packageBrowserField = false;
400
+ let packageInfo;
401
+
402
+ const filter = (pkg, pkgPath) => {
403
+ const info = getPackageInfo({
404
+ cache: packageInfoCache,
405
+ extensions,
406
+ pkg,
407
+ pkgPath,
408
+ mainFields,
409
+ preserveSymlinks,
410
+ useBrowserOverrides
411
+ });
412
+
413
+ ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
414
+
415
+ return info.cachedPkg;
416
+ };
597
417
 
598
- const importSpecifierList = [];
418
+ let resolveOptions = {
419
+ basedir,
420
+ packageFilter: filter,
421
+ readFile: readCachedFile,
422
+ isFile: isFileCached,
423
+ isDirectory: isDirCached,
424
+ extensions
425
+ };
599
426
 
600
- if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
601
- // For module graph roots (i.e. when importer is undefined), we
602
- // need to handle 'path fragments` like `foo/bar` that are commonly
603
- // found in rollup config files. If importee doesn't look like a
604
- // relative or absolute path, we make it relative and attempt to
605
- // resolve it. If we don't find anything, we try resolving it as we
606
- // got it.
607
- importSpecifierList.push(`./${importee}`);
608
- }
427
+ if (preserveSymlinks !== undefined) {
428
+ resolveOptions.preserveSymlinks = preserveSymlinks;
429
+ }
609
430
 
610
- const importeeIsBuiltin = builtins.has(importee);
431
+ const importSpecifierList = [];
611
432
 
612
- if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
613
- // The `resolve` library will not resolve packages with the same
614
- // name as a node built-in module. If we're resolving something
615
- // that's a builtin, and we don't prefer to find built-ins, we
616
- // first try to look up a local module with that name. If we don't
617
- // find anything, we resolve the builtin which just returns back
618
- // the built-in's name.
619
- importSpecifierList.push(`${importee}/`);
620
- } // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
433
+ if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
434
+ // For module graph roots (i.e. when importer is undefined), we
435
+ // need to handle 'path fragments` like `foo/bar` that are commonly
436
+ // found in rollup config files. If importee doesn't look like a
437
+ // relative or absolute path, we make it relative and attempt to
438
+ // resolve it. If we don't find anything, we try resolving it as we
439
+ // got it.
440
+ importSpecifierList.push(`./${importee}`);
441
+ }
621
442
 
443
+ const importeeIsBuiltin = builtins.has(importee);
622
444
 
623
- if (importer && importee.endsWith('.js')) {
624
- for (var _i = 0, _arr = ['.ts', '.tsx']; _i < _arr.length; _i++) {
625
- const ext = _arr[_i];
445
+ if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
446
+ // The `resolve` library will not resolve packages with the same
447
+ // name as a node built-in module. If we're resolving something
448
+ // that's a builtin, and we don't prefer to find built-ins, we
449
+ // first try to look up a local module with that name. If we don't
450
+ // find anything, we resolve the builtin which just returns back
451
+ // the built-in's name.
452
+ importSpecifierList.push(`${importee}/`);
453
+ }
626
454
 
627
- if (importer.endsWith(ext) && extensions.includes(ext)) {
628
- importSpecifierList.push(importee.replace(/.js$/, ext));
629
- }
455
+ // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
456
+ if (importer && importee.endsWith('.js')) {
457
+ for (const ext of ['.ts', '.tsx']) {
458
+ if (importer.endsWith(ext) && extensions.includes(ext)) {
459
+ importSpecifierList.push(importee.replace(/.js$/, ext));
630
460
  }
631
461
  }
462
+ }
632
463
 
633
- importSpecifierList.push(importee);
634
- resolveOptions = Object.assign(resolveOptions, customResolveOptions);
635
-
636
- try {
637
- let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions);
638
-
639
- if (resolved && packageBrowserField) {
640
- if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
641
- if (!packageBrowserField[resolved]) {
642
- browserMapCache.set(resolved, packageBrowserField);
643
- return ES6_BROWSER_EMPTY;
644
- }
645
-
646
- resolved = packageBrowserField[resolved];
647
- }
648
-
649
- browserMapCache.set(resolved, packageBrowserField);
650
- }
464
+ importSpecifierList.push(importee);
465
+ resolveOptions = Object.assign(resolveOptions, customResolveOptions);
651
466
 
652
- if (hasPackageEntry && !preserveSymlinks && resolved) {
653
- const fileExists = yield exists(resolved);
467
+ try {
468
+ let resolved = await resolveImportSpecifiers(importSpecifierList, resolveOptions);
654
469
 
655
- if (fileExists) {
656
- resolved = yield realpath(resolved);
470
+ if (resolved && packageBrowserField) {
471
+ if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
472
+ if (!packageBrowserField[resolved]) {
473
+ browserMapCache.set(resolved, packageBrowserField);
474
+ return ES6_BROWSER_EMPTY;
657
475
  }
476
+ resolved = packageBrowserField[resolved];
658
477
  }
478
+ browserMapCache.set(resolved, packageBrowserField);
479
+ }
659
480
 
660
- idToPackageInfo.set(resolved, packageInfo);
661
-
662
- if (hasPackageEntry) {
663
- if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
664
- return null;
665
- } else if (importeeIsBuiltin && preferBuiltins) {
666
- if (!isPreferBuiltinsSet) {
667
- _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`);
668
- }
669
-
670
- return null;
671
- } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
672
- return null;
673
- }
481
+ if (hasPackageEntry && !preserveSymlinks && resolved) {
482
+ const fileExists = await exists(resolved);
483
+ if (fileExists) {
484
+ resolved = await realpath(resolved);
674
485
  }
486
+ }
675
487
 
676
- if (resolved && options.modulesOnly) {
677
- const code = yield readFile(resolved, 'utf-8');
488
+ idToPackageInfo.set(resolved, packageInfo);
678
489
 
679
- if (isModule(code)) {
680
- return {
681
- id: `${resolved}${importSuffix}`,
682
- moduleSideEffects: hasModuleSideEffects(resolved)
683
- };
490
+ if (hasPackageEntry) {
491
+ if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
492
+ return null;
493
+ } else if (importeeIsBuiltin && preferBuiltins) {
494
+ if (!isPreferBuiltinsSet) {
495
+ this.warn(
496
+ `preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
497
+ );
684
498
  }
685
-
499
+ return null;
500
+ } else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
686
501
  return null;
687
502
  }
503
+ }
688
504
 
689
- const result = {
690
- id: `${resolved}${importSuffix}`,
691
- moduleSideEffects: hasModuleSideEffects(resolved)
692
- };
693
- return result;
694
- } catch (error) {
505
+ if (resolved && options.modulesOnly) {
506
+ const code = await readFile(resolved, 'utf-8');
507
+ if (isModule(code)) {
508
+ return {
509
+ id: `${resolved}${importSuffix}`,
510
+ moduleSideEffects: hasModuleSideEffects(resolved)
511
+ };
512
+ }
695
513
  return null;
696
514
  }
697
- })();
515
+ const result = {
516
+ id: `${resolved}${importSuffix}`,
517
+ moduleSideEffects: hasModuleSideEffects(resolved)
518
+ };
519
+ return result;
520
+ } catch (error) {
521
+ return null;
522
+ }
698
523
  },
699
524
 
700
525
  load(importee) {
701
526
  if (importee === ES6_BROWSER_EMPTY) {
702
527
  return 'export default {};';
703
528
  }
704
-
705
529
  return null;
706
530
  },
707
531
 
708
532
  getPackageInfoForId(id) {
709
533
  return idToPackageInfo.get(id);
710
534
  }
711
-
712
535
  };
713
536
  }
714
537