@rollup/plugin-node-resolve 9.0.0 → 11.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +47 -21
- package/dist/cjs/index.js +642 -132
- package/dist/es/index.js +625 -121
- package/package.json +9 -9
- package/types/index.d.ts +15 -9
package/dist/cjs/index.js
CHANGED
@@ -2,22 +2,29 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6
|
-
|
7
5
|
var path = require('path');
|
8
|
-
var builtinList =
|
9
|
-
var deepMerge =
|
10
|
-
var isModule =
|
6
|
+
var builtinList = require('builtin-modules');
|
7
|
+
var deepMerge = require('deepmerge');
|
8
|
+
var isModule = require('is-module');
|
11
9
|
var fs = require('fs');
|
12
|
-
var fs__default = _interopDefault(fs);
|
13
10
|
var util = require('util');
|
11
|
+
var url = require('url');
|
12
|
+
var resolve = require('resolve');
|
14
13
|
var pluginutils = require('@rollup/pluginutils');
|
15
|
-
var resolveModule = _interopDefault(require('resolve'));
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
16
|
+
|
17
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
18
|
+
var builtinList__default = /*#__PURE__*/_interopDefaultLegacy(builtinList);
|
19
|
+
var deepMerge__default = /*#__PURE__*/_interopDefaultLegacy(deepMerge);
|
20
|
+
var isModule__default = /*#__PURE__*/_interopDefaultLegacy(isModule);
|
21
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
22
|
+
var resolve__default = /*#__PURE__*/_interopDefaultLegacy(resolve);
|
23
|
+
|
24
|
+
const exists = util.promisify(fs__default['default'].exists);
|
25
|
+
const readFile = util.promisify(fs__default['default'].readFile);
|
26
|
+
const realpath = util.promisify(fs__default['default'].realpath);
|
27
|
+
const stat = util.promisify(fs__default['default'].stat);
|
21
28
|
|
22
29
|
const onError = (error) => {
|
23
30
|
if (error.code === 'ENOENT') {
|
@@ -73,8 +80,6 @@ const isFileCached = makeCache(async (file) => {
|
|
73
80
|
|
74
81
|
const readCachedFile = makeCache(readFile);
|
75
82
|
|
76
|
-
const resolveId = util.promisify(resolveModule);
|
77
|
-
|
78
83
|
// returns the imported package name for bare module imports
|
79
84
|
function getPackageName(id) {
|
80
85
|
if (id.startsWith('.') || id.startsWith('/')) {
|
@@ -127,7 +132,7 @@ function getPackageInfo(options) {
|
|
127
132
|
|
128
133
|
const packageInfo = {
|
129
134
|
// copy as we are about to munge the `main` field of `pkg`.
|
130
|
-
packageJson:
|
135
|
+
packageJson: { ...pkg },
|
131
136
|
|
132
137
|
// path to package.json file
|
133
138
|
packageJsonPath: pkgPath,
|
@@ -225,44 +230,553 @@ function normalizeInput(input) {
|
|
225
230
|
return [input];
|
226
231
|
}
|
227
232
|
|
228
|
-
|
229
|
-
// successfully, or the error that resulted from the last attempted module resolution.
|
230
|
-
function resolveImportSpecifiers(importSpecifierList, resolveOptions) {
|
231
|
-
let promise = Promise.resolve();
|
233
|
+
/* eslint-disable no-await-in-loop */
|
232
234
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
235
|
+
const fileExists = util.promisify(fs__default['default'].exists);
|
236
|
+
|
237
|
+
function isModuleDir(current, moduleDirs) {
|
238
|
+
return moduleDirs.some((dir) => current.endsWith(dir));
|
239
|
+
}
|
240
|
+
|
241
|
+
async function findPackageJson(base, moduleDirs) {
|
242
|
+
const { root } = path__default['default'].parse(base);
|
243
|
+
let current = base;
|
244
|
+
|
245
|
+
while (current !== root && !isModuleDir(current, moduleDirs)) {
|
246
|
+
const pkgJsonPath = path__default['default'].join(current, 'package.json');
|
247
|
+
if (await fileExists(pkgJsonPath)) {
|
248
|
+
const pkgJsonString = fs__default['default'].readFileSync(pkgJsonPath, 'utf-8');
|
249
|
+
return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
|
250
|
+
}
|
251
|
+
current = path__default['default'].resolve(current, '..');
|
252
|
+
}
|
253
|
+
return null;
|
254
|
+
}
|
255
|
+
|
256
|
+
function isUrl(str) {
|
257
|
+
try {
|
258
|
+
return !!new URL(str);
|
259
|
+
} catch (_) {
|
260
|
+
return false;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
|
264
|
+
function isConditions(exports) {
|
265
|
+
return typeof exports === 'object' && Object.keys(exports).every((k) => !k.startsWith('.'));
|
266
|
+
}
|
267
|
+
|
268
|
+
function isMappings(exports) {
|
269
|
+
return typeof exports === 'object' && !isConditions(exports);
|
270
|
+
}
|
271
|
+
|
272
|
+
function isMixedExports(exports) {
|
273
|
+
const keys = Object.keys(exports);
|
274
|
+
return keys.some((k) => k.startsWith('.')) && keys.some((k) => !k.startsWith('.'));
|
275
|
+
}
|
276
|
+
|
277
|
+
function createBaseErrorMsg(importSpecifier, importer) {
|
278
|
+
return `Could not resolve import "${importSpecifier}" in ${importer}`;
|
279
|
+
}
|
280
|
+
|
281
|
+
function createErrorMsg(context, reason, internal) {
|
282
|
+
const { importSpecifier, importer, pkgJsonPath } = context;
|
283
|
+
const base = createBaseErrorMsg(importSpecifier, importer);
|
284
|
+
const field = internal ? 'imports' : 'exports';
|
285
|
+
return `${base} using ${field} defined in ${pkgJsonPath}.${reason ? ` ${reason}` : ''}`;
|
286
|
+
}
|
287
|
+
|
288
|
+
class ResolveError extends Error {}
|
289
|
+
|
290
|
+
class InvalidConfigurationError extends ResolveError {
|
291
|
+
constructor(context, reason) {
|
292
|
+
super(createErrorMsg(context, `Invalid "exports" field. ${reason}`));
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
class InvalidModuleSpecifierError extends ResolveError {
|
297
|
+
constructor(context, internal) {
|
298
|
+
super(createErrorMsg(context, internal));
|
299
|
+
}
|
300
|
+
}
|
301
|
+
|
302
|
+
class InvalidPackageTargetError extends ResolveError {
|
303
|
+
constructor(context, reason) {
|
304
|
+
super(createErrorMsg(context, reason));
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
/* eslint-disable no-await-in-loop, no-undefined */
|
309
|
+
|
310
|
+
function includesInvalidSegments(pathSegments, moduleDirs) {
|
311
|
+
return pathSegments
|
312
|
+
.split('/')
|
313
|
+
.slice(1)
|
314
|
+
.some((t) => ['.', '..', ...moduleDirs].includes(t));
|
315
|
+
}
|
316
|
+
|
317
|
+
async function resolvePackageTarget(context, { target, subpath, pattern, internal }) {
|
318
|
+
if (typeof target === 'string') {
|
319
|
+
if (!pattern && subpath.length > 0 && !target.endsWith('/')) {
|
320
|
+
throw new InvalidModuleSpecifierError(context);
|
321
|
+
}
|
322
|
+
|
323
|
+
if (!target.startsWith('./')) {
|
324
|
+
if (internal && !['/', '../'].some((p) => target.startsWith(p)) && !isUrl(target)) {
|
325
|
+
// this is a bare package import, remap it and resolve it using regular node resolve
|
326
|
+
if (pattern) {
|
327
|
+
const result = await context.resolveId(
|
328
|
+
target.replace(/\*/g, subpath),
|
329
|
+
context.pkgURL.href
|
330
|
+
);
|
331
|
+
return result ? url.pathToFileURL(result.location) : null;
|
332
|
+
}
|
333
|
+
|
334
|
+
const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
|
335
|
+
return result ? url.pathToFileURL(result.location) : null;
|
238
336
|
}
|
337
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
338
|
+
}
|
339
|
+
|
340
|
+
if (includesInvalidSegments(target, context.moduleDirs)) {
|
341
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
342
|
+
}
|
343
|
+
|
344
|
+
const resolvedTarget = new URL(target, context.pkgURL);
|
345
|
+
if (!resolvedTarget.href.startsWith(context.pkgURL.href)) {
|
346
|
+
throw new InvalidPackageTargetError(
|
347
|
+
context,
|
348
|
+
`Resolved to ${resolvedTarget.href} which is outside package ${context.pkgURL.href}`
|
349
|
+
);
|
350
|
+
}
|
351
|
+
|
352
|
+
if (includesInvalidSegments(subpath, context.moduleDirs)) {
|
353
|
+
throw new InvalidModuleSpecifierError(context);
|
354
|
+
}
|
239
355
|
|
240
|
-
|
241
|
-
|
242
|
-
|
356
|
+
if (pattern) {
|
357
|
+
return resolvedTarget.href.replace(/\*/g, subpath);
|
358
|
+
}
|
359
|
+
return new URL(subpath, resolvedTarget).href;
|
360
|
+
}
|
361
|
+
|
362
|
+
if (Array.isArray(target)) {
|
363
|
+
let lastError;
|
364
|
+
for (const item of target) {
|
365
|
+
try {
|
366
|
+
const resolved = await resolvePackageTarget(context, {
|
367
|
+
target: item,
|
368
|
+
subpath,
|
369
|
+
pattern,
|
370
|
+
internal
|
371
|
+
});
|
372
|
+
|
373
|
+
// return if defined or null, but not undefined
|
374
|
+
if (resolved !== undefined) {
|
375
|
+
return resolved;
|
376
|
+
}
|
377
|
+
} catch (error) {
|
378
|
+
if (!(error instanceof InvalidPackageTargetError)) {
|
379
|
+
throw error;
|
380
|
+
} else {
|
381
|
+
lastError = error;
|
243
382
|
}
|
244
|
-
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
if (lastError) {
|
387
|
+
throw lastError;
|
388
|
+
}
|
389
|
+
return null;
|
390
|
+
}
|
391
|
+
|
392
|
+
if (target && typeof target === 'object') {
|
393
|
+
for (const [key, value] of Object.entries(target)) {
|
394
|
+
if (key === 'default' || context.conditions.includes(key)) {
|
395
|
+
const resolved = await resolvePackageTarget(context, {
|
396
|
+
target: value,
|
397
|
+
subpath,
|
398
|
+
pattern,
|
399
|
+
internal
|
400
|
+
});
|
401
|
+
|
402
|
+
// return if defined or null, but not undefined
|
403
|
+
if (resolved !== undefined) {
|
404
|
+
return resolved;
|
405
|
+
}
|
406
|
+
}
|
407
|
+
}
|
408
|
+
return undefined;
|
409
|
+
}
|
410
|
+
|
411
|
+
if (target === null) {
|
412
|
+
return null;
|
413
|
+
}
|
414
|
+
|
415
|
+
throw new InvalidPackageTargetError(context, `Invalid exports field.`);
|
416
|
+
}
|
417
|
+
|
418
|
+
/* eslint-disable no-await-in-loop */
|
419
|
+
|
420
|
+
async function resolvePackageImportsExports(context, { matchKey, matchObj, internal }) {
|
421
|
+
if (!matchKey.endsWith('*') && matchKey in matchObj) {
|
422
|
+
const target = matchObj[matchKey];
|
423
|
+
const resolved = await resolvePackageTarget(context, { target, subpath: '', internal });
|
424
|
+
return resolved;
|
425
|
+
}
|
426
|
+
|
427
|
+
const expansionKeys = Object.keys(matchObj)
|
428
|
+
.filter((k) => k.endsWith('/') || k.endsWith('*'))
|
429
|
+
.sort((a, b) => b.length - a.length);
|
430
|
+
|
431
|
+
for (const expansionKey of expansionKeys) {
|
432
|
+
const prefix = expansionKey.substring(0, expansionKey.length - 1);
|
433
|
+
|
434
|
+
if (expansionKey.endsWith('*') && matchKey.startsWith(prefix)) {
|
435
|
+
const target = matchObj[expansionKey];
|
436
|
+
const subpath = matchKey.substring(expansionKey.length - 1);
|
437
|
+
const resolved = await resolvePackageTarget(context, {
|
438
|
+
target,
|
439
|
+
subpath,
|
440
|
+
pattern: true,
|
441
|
+
internal
|
245
442
|
});
|
443
|
+
return resolved;
|
444
|
+
}
|
445
|
+
|
446
|
+
if (matchKey.startsWith(expansionKey)) {
|
447
|
+
const target = matchObj[expansionKey];
|
448
|
+
const subpath = matchKey.substring(expansionKey.length);
|
449
|
+
|
450
|
+
const resolved = await resolvePackageTarget(context, { target, subpath, internal });
|
451
|
+
return resolved;
|
452
|
+
}
|
453
|
+
}
|
454
|
+
|
455
|
+
throw new InvalidModuleSpecifierError(context, internal);
|
456
|
+
}
|
457
|
+
|
458
|
+
async function resolvePackageExports(context, subpath, exports) {
|
459
|
+
if (isMixedExports(exports)) {
|
460
|
+
throw new InvalidConfigurationError(
|
461
|
+
context,
|
462
|
+
'All keys must either start with ./, or without one.'
|
463
|
+
);
|
464
|
+
}
|
465
|
+
|
466
|
+
if (subpath === '.') {
|
467
|
+
let mainExport;
|
468
|
+
// If exports is a String or Array, or an Object containing no keys starting with ".", then
|
469
|
+
if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) {
|
470
|
+
mainExport = exports;
|
471
|
+
} else if (isMappings(exports)) {
|
472
|
+
mainExport = exports['.'];
|
473
|
+
}
|
474
|
+
|
475
|
+
if (mainExport) {
|
476
|
+
const resolved = await resolvePackageTarget(context, { target: mainExport, subpath: '' });
|
477
|
+
if (resolved) {
|
478
|
+
return resolved;
|
479
|
+
}
|
480
|
+
}
|
481
|
+
} else if (isMappings(exports)) {
|
482
|
+
const resolvedMatch = await resolvePackageImportsExports(context, {
|
483
|
+
matchKey: subpath,
|
484
|
+
matchObj: exports
|
485
|
+
});
|
486
|
+
|
487
|
+
if (resolvedMatch) {
|
488
|
+
return resolvedMatch;
|
489
|
+
}
|
490
|
+
}
|
491
|
+
|
492
|
+
throw new InvalidModuleSpecifierError(context);
|
493
|
+
}
|
494
|
+
|
495
|
+
async function resolvePackageImports({
|
496
|
+
importSpecifier,
|
497
|
+
importer,
|
498
|
+
moduleDirs,
|
499
|
+
conditions,
|
500
|
+
resolveId
|
501
|
+
}) {
|
502
|
+
const result = await findPackageJson(importer, moduleDirs);
|
503
|
+
if (!result) {
|
504
|
+
throw new Error(createBaseErrorMsg('. Could not find a parent package.json.'));
|
505
|
+
}
|
506
|
+
|
507
|
+
const { pkgPath, pkgJsonPath, pkgJson } = result;
|
508
|
+
const pkgURL = url.pathToFileURL(`${pkgPath}/`);
|
509
|
+
const context = {
|
510
|
+
importer,
|
511
|
+
importSpecifier,
|
512
|
+
moduleDirs,
|
513
|
+
pkgURL,
|
514
|
+
pkgJsonPath,
|
515
|
+
conditions,
|
516
|
+
resolveId
|
517
|
+
};
|
518
|
+
|
519
|
+
const { imports } = pkgJson;
|
520
|
+
if (!imports) {
|
521
|
+
throw new InvalidModuleSpecifierError(context, true);
|
522
|
+
}
|
523
|
+
|
524
|
+
if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
|
525
|
+
throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
|
526
|
+
}
|
527
|
+
|
528
|
+
return resolvePackageImportsExports(context, {
|
529
|
+
matchKey: importSpecifier,
|
530
|
+
matchObj: imports,
|
531
|
+
internal: true
|
532
|
+
});
|
533
|
+
}
|
534
|
+
|
535
|
+
const resolveImportPath = util.promisify(resolve__default['default']);
|
536
|
+
const readFile$1 = util.promisify(fs__default['default'].readFile);
|
537
|
+
|
538
|
+
async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
|
539
|
+
if (importer) {
|
540
|
+
const selfPackageJsonResult = await findPackageJson(importer, moduleDirectories);
|
541
|
+
if (selfPackageJsonResult && selfPackageJsonResult.pkgJson.name === pkgName) {
|
542
|
+
// the referenced package name is the current package
|
543
|
+
return selfPackageJsonResult;
|
544
|
+
}
|
545
|
+
}
|
546
|
+
|
547
|
+
try {
|
548
|
+
const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
|
549
|
+
const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
|
550
|
+
return { pkgJsonPath, pkgJson };
|
551
|
+
} catch (_) {
|
552
|
+
return null;
|
553
|
+
}
|
554
|
+
}
|
555
|
+
|
556
|
+
async function resolveId({
|
557
|
+
importer,
|
558
|
+
importSpecifier,
|
559
|
+
exportConditions,
|
560
|
+
warn,
|
561
|
+
packageInfoCache,
|
562
|
+
extensions,
|
563
|
+
mainFields,
|
564
|
+
preserveSymlinks,
|
565
|
+
useBrowserOverrides,
|
566
|
+
baseDir,
|
567
|
+
moduleDirectories
|
568
|
+
}) {
|
569
|
+
let hasModuleSideEffects = () => null;
|
570
|
+
let hasPackageEntry = true;
|
571
|
+
let packageBrowserField = false;
|
572
|
+
let packageInfo;
|
573
|
+
|
574
|
+
const filter = (pkg, pkgPath) => {
|
575
|
+
const info = getPackageInfo({
|
576
|
+
cache: packageInfoCache,
|
577
|
+
extensions,
|
578
|
+
pkg,
|
579
|
+
pkgPath,
|
580
|
+
mainFields,
|
581
|
+
preserveSymlinks,
|
582
|
+
useBrowserOverrides
|
246
583
|
});
|
247
584
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
585
|
+
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
|
586
|
+
|
587
|
+
return info.cachedPkg;
|
588
|
+
};
|
589
|
+
|
590
|
+
const resolveOptions = {
|
591
|
+
basedir: baseDir,
|
592
|
+
readFile: readCachedFile,
|
593
|
+
isFile: isFileCached,
|
594
|
+
isDirectory: isDirCached,
|
595
|
+
extensions,
|
596
|
+
includeCoreModules: false,
|
597
|
+
moduleDirectory: moduleDirectories,
|
598
|
+
preserveSymlinks,
|
599
|
+
packageFilter: filter
|
600
|
+
};
|
601
|
+
|
602
|
+
let location;
|
603
|
+
|
604
|
+
const pkgName = getPackageName(importSpecifier);
|
605
|
+
if (importSpecifier.startsWith('#')) {
|
606
|
+
// this is a package internal import, resolve using package imports field
|
607
|
+
const resolveResult = await resolvePackageImports({
|
608
|
+
importSpecifier,
|
609
|
+
importer,
|
610
|
+
moduleDirs: moduleDirectories,
|
611
|
+
conditions: exportConditions,
|
612
|
+
resolveId(id, parent) {
|
613
|
+
return resolveId({
|
614
|
+
importSpecifier: id,
|
615
|
+
importer: parent,
|
616
|
+
exportConditions,
|
617
|
+
warn,
|
618
|
+
packageInfoCache,
|
619
|
+
extensions,
|
620
|
+
mainFields,
|
621
|
+
preserveSymlinks,
|
622
|
+
useBrowserOverrides,
|
623
|
+
baseDir,
|
624
|
+
moduleDirectories
|
625
|
+
});
|
626
|
+
}
|
627
|
+
});
|
628
|
+
location = url.fileURLToPath(resolveResult);
|
629
|
+
} else if (pkgName) {
|
630
|
+
// it's a bare import, find the package.json and resolve using package exports if available
|
631
|
+
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
|
632
|
+
|
633
|
+
if (result && result.pkgJson.exports) {
|
634
|
+
const { pkgJson, pkgJsonPath } = result;
|
635
|
+
try {
|
636
|
+
const subpath =
|
637
|
+
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
|
638
|
+
const pkgDr = pkgJsonPath.replace('package.json', '');
|
639
|
+
const pkgURL = url.pathToFileURL(pkgDr);
|
640
|
+
|
641
|
+
const context = {
|
642
|
+
importer,
|
643
|
+
importSpecifier,
|
644
|
+
moduleDirs: moduleDirectories,
|
645
|
+
pkgURL,
|
646
|
+
pkgJsonPath,
|
647
|
+
conditions: exportConditions
|
648
|
+
};
|
649
|
+
const resolvedPackageExport = await resolvePackageExports(
|
650
|
+
context,
|
651
|
+
subpath,
|
652
|
+
pkgJson.exports
|
653
|
+
);
|
654
|
+
location = url.fileURLToPath(resolvedPackageExport);
|
655
|
+
} catch (error) {
|
656
|
+
if (!(error instanceof ResolveError)) {
|
252
657
|
throw error;
|
253
658
|
}
|
254
|
-
|
659
|
+
warn(error);
|
660
|
+
return null;
|
661
|
+
}
|
255
662
|
}
|
256
663
|
}
|
257
664
|
|
258
|
-
|
665
|
+
if (!location) {
|
666
|
+
// package has no imports or exports, use classic node resolve
|
667
|
+
try {
|
668
|
+
location = await resolveImportPath(importSpecifier, resolveOptions);
|
669
|
+
} catch (error) {
|
670
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
671
|
+
throw error;
|
672
|
+
}
|
673
|
+
return null;
|
674
|
+
}
|
675
|
+
}
|
676
|
+
|
677
|
+
if (!preserveSymlinks) {
|
678
|
+
if (await exists(location)) {
|
679
|
+
location = await realpath(location);
|
680
|
+
}
|
681
|
+
}
|
682
|
+
|
683
|
+
return {
|
684
|
+
location,
|
685
|
+
hasModuleSideEffects,
|
686
|
+
hasPackageEntry,
|
687
|
+
packageBrowserField,
|
688
|
+
packageInfo
|
689
|
+
};
|
690
|
+
}
|
691
|
+
|
692
|
+
// Resolve module specifiers in order. Promise resolves to the first module that resolves
|
693
|
+
// successfully, or the error that resulted from the last attempted module resolution.
|
694
|
+
async function resolveImportSpecifiers({
|
695
|
+
importer,
|
696
|
+
importSpecifierList,
|
697
|
+
exportConditions,
|
698
|
+
warn,
|
699
|
+
packageInfoCache,
|
700
|
+
extensions,
|
701
|
+
mainFields,
|
702
|
+
preserveSymlinks,
|
703
|
+
useBrowserOverrides,
|
704
|
+
baseDir,
|
705
|
+
moduleDirectories
|
706
|
+
}) {
|
707
|
+
for (let i = 0; i < importSpecifierList.length; i++) {
|
708
|
+
// eslint-disable-next-line no-await-in-loop
|
709
|
+
const resolved = await resolveId({
|
710
|
+
importer,
|
711
|
+
importSpecifier: importSpecifierList[i],
|
712
|
+
exportConditions,
|
713
|
+
warn,
|
714
|
+
packageInfoCache,
|
715
|
+
extensions,
|
716
|
+
mainFields,
|
717
|
+
preserveSymlinks,
|
718
|
+
useBrowserOverrides,
|
719
|
+
baseDir,
|
720
|
+
moduleDirectories
|
721
|
+
});
|
722
|
+
if (resolved) {
|
723
|
+
return resolved;
|
724
|
+
}
|
725
|
+
}
|
726
|
+
return null;
|
727
|
+
}
|
728
|
+
|
729
|
+
function handleDeprecatedOptions(opts) {
|
730
|
+
const warnings = [];
|
731
|
+
|
732
|
+
if (opts.customResolveOptions) {
|
733
|
+
const { customResolveOptions } = opts;
|
734
|
+
if (customResolveOptions.moduleDirectory) {
|
735
|
+
// eslint-disable-next-line no-param-reassign
|
736
|
+
opts.moduleDirectories = Array.isArray(customResolveOptions.moduleDirectory)
|
737
|
+
? customResolveOptions.moduleDirectory
|
738
|
+
: [customResolveOptions.moduleDirectory];
|
739
|
+
|
740
|
+
warnings.push(
|
741
|
+
'node-resolve: The `customResolveOptions.moduleDirectory` option has been deprecated. Use `moduleDirectories`, which must be an array.'
|
742
|
+
);
|
743
|
+
}
|
744
|
+
|
745
|
+
if (customResolveOptions.preserveSymlinks) {
|
746
|
+
throw new Error(
|
747
|
+
'node-resolve: `customResolveOptions.preserveSymlinks` is no longer an option. We now always use the rollup `preserveSymlinks` option.'
|
748
|
+
);
|
749
|
+
}
|
750
|
+
|
751
|
+
[
|
752
|
+
'basedir',
|
753
|
+
'package',
|
754
|
+
'extensions',
|
755
|
+
'includeCoreModules',
|
756
|
+
'readFile',
|
757
|
+
'isFile',
|
758
|
+
'isDirectory',
|
759
|
+
'realpath',
|
760
|
+
'packageFilter',
|
761
|
+
'pathFilter',
|
762
|
+
'paths',
|
763
|
+
'packageIterator'
|
764
|
+
].forEach((resolveOption) => {
|
765
|
+
if (customResolveOptions[resolveOption]) {
|
766
|
+
throw new Error(
|
767
|
+
`node-resolve: \`customResolveOptions.${resolveOption}\` is no longer an option. If you need this, please open an issue.`
|
768
|
+
);
|
769
|
+
}
|
770
|
+
});
|
771
|
+
}
|
772
|
+
|
773
|
+
return { warnings };
|
259
774
|
}
|
260
775
|
|
261
776
|
/* eslint-disable no-param-reassign, no-shadow, no-undefined */
|
262
777
|
|
263
|
-
const builtins = new Set(
|
778
|
+
const builtins = new Set(builtinList__default['default']);
|
264
779
|
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
|
265
|
-
const nullFn = () => null;
|
266
780
|
const deepFreeze = (object) => {
|
267
781
|
Object.freeze(object);
|
268
782
|
|
@@ -274,20 +788,27 @@ const deepFreeze = (object) => {
|
|
274
788
|
|
275
789
|
return object;
|
276
790
|
};
|
791
|
+
|
792
|
+
const baseConditions = ['default', 'module'];
|
793
|
+
const baseConditionsEsm = [...baseConditions, 'import'];
|
794
|
+
const baseConditionsCjs = [...baseConditions, 'require'];
|
277
795
|
const defaults = {
|
278
|
-
customResolveOptions: {},
|
279
796
|
dedupe: [],
|
280
797
|
// It's important that .mjs is listed before .js so that Rollup will interpret npm modules
|
281
798
|
// which deploy both ESM .mjs and CommonJS .js files as ESM.
|
282
799
|
extensions: ['.mjs', '.js', '.json', '.node'],
|
283
|
-
resolveOnly: []
|
800
|
+
resolveOnly: [],
|
801
|
+
moduleDirectories: ['node_modules']
|
284
802
|
};
|
285
|
-
const DEFAULTS = deepFreeze(
|
803
|
+
const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
|
286
804
|
|
287
805
|
function nodeResolve(opts = {}) {
|
288
|
-
const
|
289
|
-
|
290
|
-
const
|
806
|
+
const { warnings } = handleDeprecatedOptions(opts);
|
807
|
+
|
808
|
+
const options = { ...defaults, ...opts };
|
809
|
+
const { extensions, jail, moduleDirectories } = options;
|
810
|
+
const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
|
811
|
+
const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
|
291
812
|
const packageInfoCache = new Map();
|
292
813
|
const idToPackageInfo = new Map();
|
293
814
|
const mainFields = getMainFields(options);
|
@@ -298,11 +819,6 @@ function nodeResolve(opts = {}) {
|
|
298
819
|
let { dedupe } = options;
|
299
820
|
let rollupOptions;
|
300
821
|
|
301
|
-
if (options.only) {
|
302
|
-
warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`');
|
303
|
-
options.resolveOnly = options.only;
|
304
|
-
}
|
305
|
-
|
306
822
|
if (typeof dedupe !== 'function') {
|
307
823
|
dedupe = (importee) =>
|
308
824
|
options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
|
@@ -338,25 +854,28 @@ function nodeResolve(opts = {}) {
|
|
338
854
|
isDirCached.clear();
|
339
855
|
},
|
340
856
|
|
341
|
-
async resolveId(importee, importer) {
|
857
|
+
async resolveId(importee, importer, opts) {
|
342
858
|
if (importee === ES6_BROWSER_EMPTY) {
|
343
859
|
return importee;
|
344
860
|
}
|
345
861
|
// ignore IDs with null character, these belong to other plugins
|
346
862
|
if (/\0/.test(importee)) return null;
|
347
863
|
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
864
|
+
if (/\0/.test(importer)) {
|
865
|
+
importer = undefined;
|
866
|
+
}
|
867
|
+
|
868
|
+
// strip query params from import
|
869
|
+
const [importPath, params] = importee.split('?');
|
870
|
+
const importSuffix = `${params ? `?${params}` : ''}`;
|
352
871
|
importee = importPath;
|
353
872
|
|
354
|
-
const
|
873
|
+
const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
|
355
874
|
|
356
875
|
// https://github.com/defunctzombie/package-browser-field-spec
|
357
876
|
const browser = browserMapCache.get(importer);
|
358
877
|
if (useBrowserOverrides && browser) {
|
359
|
-
const resolvedImportee = path.resolve(
|
878
|
+
const resolvedImportee = path.resolve(baseDir, importee);
|
360
879
|
if (browser[importee] === false || browser[resolvedImportee] === false) {
|
361
880
|
return ES6_BROWSER_EMPTY;
|
362
881
|
}
|
@@ -379,7 +898,7 @@ function nodeResolve(opts = {}) {
|
|
379
898
|
id += `/${parts.shift()}`;
|
380
899
|
} else if (id[0] === '.') {
|
381
900
|
// an import relative to the parent dir of the importer
|
382
|
-
id = path.resolve(
|
901
|
+
id = path.resolve(baseDir, importee);
|
383
902
|
isRelativeImport = true;
|
384
903
|
}
|
385
904
|
|
@@ -394,40 +913,6 @@ function nodeResolve(opts = {}) {
|
|
394
913
|
return false;
|
395
914
|
}
|
396
915
|
|
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
|
-
};
|
417
|
-
|
418
|
-
let resolveOptions = {
|
419
|
-
basedir,
|
420
|
-
packageFilter: filter,
|
421
|
-
readFile: readCachedFile,
|
422
|
-
isFile: isFileCached,
|
423
|
-
isDirectory: isDirCached,
|
424
|
-
extensions
|
425
|
-
};
|
426
|
-
|
427
|
-
if (preserveSymlinks !== undefined) {
|
428
|
-
resolveOptions.preserveSymlinks = preserveSymlinks;
|
429
|
-
}
|
430
|
-
|
431
916
|
const importSpecifierList = [];
|
432
917
|
|
433
918
|
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
|
@@ -442,7 +927,7 @@ function nodeResolve(opts = {}) {
|
|
442
927
|
|
443
928
|
const importeeIsBuiltin = builtins.has(importee);
|
444
929
|
|
445
|
-
if (importeeIsBuiltin
|
930
|
+
if (importeeIsBuiltin) {
|
446
931
|
// The `resolve` library will not resolve packages with the same
|
447
932
|
// name as a node built-in module. If we're resolving something
|
448
933
|
// that's a builtin, and we don't prefer to find built-ins, we
|
@@ -462,64 +947,89 @@ function nodeResolve(opts = {}) {
|
|
462
947
|
}
|
463
948
|
|
464
949
|
importSpecifierList.push(importee);
|
465
|
-
resolveOptions = Object.assign(resolveOptions, customResolveOptions);
|
466
950
|
|
467
|
-
|
468
|
-
|
951
|
+
const warn = (...args) => this.warn(...args);
|
952
|
+
const isRequire =
|
953
|
+
opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
|
954
|
+
const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
|
955
|
+
|
956
|
+
const resolvedWithoutBuiltins = await resolveImportSpecifiers({
|
957
|
+
importer,
|
958
|
+
importSpecifierList,
|
959
|
+
exportConditions,
|
960
|
+
warn,
|
961
|
+
packageInfoCache,
|
962
|
+
extensions,
|
963
|
+
mainFields,
|
964
|
+
preserveSymlinks,
|
965
|
+
useBrowserOverrides,
|
966
|
+
baseDir,
|
967
|
+
moduleDirectories
|
968
|
+
});
|
469
969
|
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
970
|
+
const resolved =
|
971
|
+
importeeIsBuiltin && preferBuiltins
|
972
|
+
? {
|
973
|
+
packageInfo: undefined,
|
974
|
+
hasModuleSideEffects: () => null,
|
975
|
+
hasPackageEntry: true,
|
976
|
+
packageBrowserField: false
|
475
977
|
}
|
476
|
-
|
978
|
+
: resolvedWithoutBuiltins;
|
979
|
+
if (!resolved) {
|
980
|
+
return null;
|
981
|
+
}
|
982
|
+
|
983
|
+
const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
|
984
|
+
let { location } = resolved;
|
985
|
+
if (packageBrowserField) {
|
986
|
+
if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
|
987
|
+
if (!packageBrowserField[location]) {
|
988
|
+
browserMapCache.set(location, packageBrowserField);
|
989
|
+
return ES6_BROWSER_EMPTY;
|
477
990
|
}
|
478
|
-
|
991
|
+
location = packageBrowserField[location];
|
479
992
|
}
|
993
|
+
browserMapCache.set(location, packageBrowserField);
|
994
|
+
}
|
480
995
|
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
}
|
996
|
+
if (hasPackageEntry && !preserveSymlinks) {
|
997
|
+
const fileExists = await exists(location);
|
998
|
+
if (fileExists) {
|
999
|
+
location = await realpath(location);
|
486
1000
|
}
|
1001
|
+
}
|
487
1002
|
|
488
|
-
|
1003
|
+
idToPackageInfo.set(location, packageInfo);
|
489
1004
|
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
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
|
-
);
|
498
|
-
}
|
499
|
-
return null;
|
500
|
-
} else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
|
501
|
-
return null;
|
1005
|
+
if (hasPackageEntry) {
|
1006
|
+
if (importeeIsBuiltin && preferBuiltins) {
|
1007
|
+
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
|
1008
|
+
this.warn(
|
1009
|
+
`preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
|
1010
|
+
);
|
502
1011
|
}
|
1012
|
+
return false;
|
1013
|
+
} else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
|
1014
|
+
return null;
|
503
1015
|
}
|
1016
|
+
}
|
504
1017
|
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
}
|
513
|
-
return null;
|
1018
|
+
if (options.modulesOnly && (await exists(location))) {
|
1019
|
+
const code = await readFile(location, 'utf-8');
|
1020
|
+
if (isModule__default['default'](code)) {
|
1021
|
+
return {
|
1022
|
+
id: `${location}${importSuffix}`,
|
1023
|
+
moduleSideEffects: hasModuleSideEffects(location)
|
1024
|
+
};
|
514
1025
|
}
|
515
|
-
const result = {
|
516
|
-
id: `${resolved}${importSuffix}`,
|
517
|
-
moduleSideEffects: hasModuleSideEffects(resolved)
|
518
|
-
};
|
519
|
-
return result;
|
520
|
-
} catch (error) {
|
521
1026
|
return null;
|
522
1027
|
}
|
1028
|
+
const result = {
|
1029
|
+
id: `${location}${importSuffix}`,
|
1030
|
+
moduleSideEffects: hasModuleSideEffects(location)
|
1031
|
+
};
|
1032
|
+
return result;
|
523
1033
|
},
|
524
1034
|
|
525
1035
|
load(importee) {
|