@rollup/plugin-node-resolve 11.0.1 → 11.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.md +16 -3
- package/dist/cjs/index.js +392 -158
- package/dist/es/index.js +392 -158
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
@@ -8,6 +8,7 @@ var deepMerge = require('deepmerge');
|
|
8
8
|
var isModule = require('is-module');
|
9
9
|
var fs = require('fs');
|
10
10
|
var util = require('util');
|
11
|
+
var url = require('url');
|
11
12
|
var resolve = require('resolve');
|
12
13
|
var pluginutils = require('@rollup/pluginutils');
|
13
14
|
|
@@ -20,10 +21,18 @@ var isModule__default = /*#__PURE__*/_interopDefaultLegacy(isModule);
|
|
20
21
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
21
22
|
var resolve__default = /*#__PURE__*/_interopDefaultLegacy(resolve);
|
22
23
|
|
23
|
-
const
|
24
|
+
const access = util.promisify(fs__default['default'].access);
|
24
25
|
const readFile = util.promisify(fs__default['default'].readFile);
|
25
26
|
const realpath = util.promisify(fs__default['default'].realpath);
|
26
27
|
const stat = util.promisify(fs__default['default'].stat);
|
28
|
+
async function exists(filePath) {
|
29
|
+
try {
|
30
|
+
await access(filePath);
|
31
|
+
return true;
|
32
|
+
} catch {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|
27
36
|
|
28
37
|
const onError = (error) => {
|
29
38
|
if (error.code === 'ENOENT') {
|
@@ -115,7 +124,16 @@ function getMainFields(options) {
|
|
115
124
|
}
|
116
125
|
|
117
126
|
function getPackageInfo(options) {
|
118
|
-
const {
|
127
|
+
const {
|
128
|
+
cache,
|
129
|
+
extensions,
|
130
|
+
pkg,
|
131
|
+
mainFields,
|
132
|
+
preserveSymlinks,
|
133
|
+
useBrowserOverrides,
|
134
|
+
rootDir,
|
135
|
+
ignoreSideEffectsForRoot
|
136
|
+
} = options;
|
119
137
|
let { pkgPath } = options;
|
120
138
|
|
121
139
|
if (cache.has(pkgPath)) {
|
@@ -205,13 +223,15 @@ function getPackageInfo(options) {
|
|
205
223
|
packageInfo.browserMappedMain = false;
|
206
224
|
}
|
207
225
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
226
|
+
if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
|
227
|
+
const packageSideEffects = pkg.sideEffects;
|
228
|
+
if (typeof packageSideEffects === 'boolean') {
|
229
|
+
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
|
230
|
+
} else if (Array.isArray(packageSideEffects)) {
|
231
|
+
internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
|
232
|
+
resolve: pkgRoot
|
233
|
+
});
|
234
|
+
}
|
215
235
|
}
|
216
236
|
|
217
237
|
cache.set(pkgPath, internalPackageInfo);
|
@@ -229,168 +249,332 @@ function normalizeInput(input) {
|
|
229
249
|
return [input];
|
230
250
|
}
|
231
251
|
|
232
|
-
|
233
|
-
|
252
|
+
/* eslint-disable no-await-in-loop */
|
253
|
+
|
254
|
+
const fileExists = util.promisify(fs__default['default'].exists);
|
255
|
+
|
256
|
+
function isModuleDir(current, moduleDirs) {
|
257
|
+
return moduleDirs.some((dir) => current.endsWith(dir));
|
258
|
+
}
|
259
|
+
|
260
|
+
async function findPackageJson(base, moduleDirs) {
|
261
|
+
const { root } = path__default['default'].parse(base);
|
262
|
+
let current = base;
|
234
263
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
264
|
+
while (current !== root && !isModuleDir(current, moduleDirs)) {
|
265
|
+
const pkgJsonPath = path__default['default'].join(current, 'package.json');
|
266
|
+
if (await fileExists(pkgJsonPath)) {
|
267
|
+
const pkgJsonString = fs__default['default'].readFileSync(pkgJsonPath, 'utf-8');
|
268
|
+
return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
|
269
|
+
}
|
270
|
+
current = path__default['default'].resolve(current, '..');
|
271
|
+
}
|
272
|
+
return null;
|
273
|
+
}
|
240
274
|
|
241
|
-
function
|
242
|
-
|
243
|
-
return
|
275
|
+
function isUrl(str) {
|
276
|
+
try {
|
277
|
+
return !!new URL(str);
|
278
|
+
} catch (_) {
|
279
|
+
return false;
|
244
280
|
}
|
281
|
+
}
|
245
282
|
|
246
|
-
|
247
|
-
|
248
|
-
|
283
|
+
function isConditions(exports) {
|
284
|
+
return typeof exports === 'object' && Object.keys(exports).every((k) => !k.startsWith('.'));
|
285
|
+
}
|
286
|
+
|
287
|
+
function isMappings(exports) {
|
288
|
+
return typeof exports === 'object' && !isConditions(exports);
|
289
|
+
}
|
290
|
+
|
291
|
+
function isMixedExports(exports) {
|
292
|
+
const keys = Object.keys(exports);
|
293
|
+
return keys.some((k) => k.startsWith('.')) && keys.some((k) => !k.startsWith('.'));
|
294
|
+
}
|
295
|
+
|
296
|
+
function createBaseErrorMsg(importSpecifier, importer) {
|
297
|
+
return `Could not resolve import "${importSpecifier}" in ${importer}`;
|
298
|
+
}
|
299
|
+
|
300
|
+
function createErrorMsg(context, reason, internal) {
|
301
|
+
const { importSpecifier, importer, pkgJsonPath } = context;
|
302
|
+
const base = createBaseErrorMsg(importSpecifier, importer);
|
303
|
+
const field = internal ? 'imports' : 'exports';
|
304
|
+
return `${base} using ${field} defined in ${pkgJsonPath}.${reason ? ` ${reason}` : ''}`;
|
305
|
+
}
|
306
|
+
|
307
|
+
class ResolveError extends Error {}
|
308
|
+
|
309
|
+
class InvalidConfigurationError extends ResolveError {
|
310
|
+
constructor(context, reason) {
|
311
|
+
super(createErrorMsg(context, `Invalid "exports" field. ${reason}`));
|
312
|
+
}
|
313
|
+
}
|
314
|
+
|
315
|
+
class InvalidModuleSpecifierError extends ResolveError {
|
316
|
+
constructor(context, internal) {
|
317
|
+
super(createErrorMsg(context, internal));
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
class InvalidPackageTargetError extends ResolveError {
|
322
|
+
constructor(context, reason) {
|
323
|
+
super(createErrorMsg(context, reason));
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
/* eslint-disable no-await-in-loop, no-undefined */
|
249
328
|
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
329
|
+
function includesInvalidSegments(pathSegments, moduleDirs) {
|
330
|
+
return pathSegments
|
331
|
+
.split('/')
|
332
|
+
.slice(1)
|
333
|
+
.some((t) => ['.', '..', ...moduleDirs].includes(t));
|
334
|
+
}
|
335
|
+
|
336
|
+
async function resolvePackageTarget(context, { target, subpath, pattern, internal }) {
|
337
|
+
if (typeof target === 'string') {
|
338
|
+
if (!pattern && subpath.length > 0 && !target.endsWith('/')) {
|
339
|
+
throw new InvalidModuleSpecifierError(context);
|
340
|
+
}
|
341
|
+
|
342
|
+
if (!target.startsWith('./')) {
|
343
|
+
if (internal && !['/', '../'].some((p) => target.startsWith(p)) && !isUrl(target)) {
|
344
|
+
// this is a bare package import, remap it and resolve it using regular node resolve
|
345
|
+
if (pattern) {
|
346
|
+
const result = await context.resolveId(
|
347
|
+
target.replace(/\*/g, subpath),
|
348
|
+
context.pkgURL.href
|
349
|
+
);
|
350
|
+
return result ? url.pathToFileURL(result.location) : null;
|
351
|
+
}
|
352
|
+
|
353
|
+
const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
|
354
|
+
return result ? url.pathToFileURL(result.location) : null;
|
256
355
|
}
|
356
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
357
|
+
}
|
358
|
+
|
359
|
+
if (includesInvalidSegments(target, context.moduleDirs)) {
|
360
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
257
361
|
}
|
258
362
|
|
259
|
-
|
260
|
-
|
261
|
-
|
363
|
+
const resolvedTarget = new URL(target, context.pkgURL);
|
364
|
+
if (!resolvedTarget.href.startsWith(context.pkgURL.href)) {
|
365
|
+
throw new InvalidPackageTargetError(
|
366
|
+
context,
|
367
|
+
`Resolved to ${resolvedTarget.href} which is outside package ${context.pkgURL.href}`
|
368
|
+
);
|
369
|
+
}
|
370
|
+
|
371
|
+
if (includesInvalidSegments(subpath, context.moduleDirs)) {
|
372
|
+
throw new InvalidModuleSpecifierError(context);
|
262
373
|
}
|
263
374
|
|
264
|
-
if (
|
265
|
-
|
266
|
-
return key;
|
375
|
+
if (pattern) {
|
376
|
+
return resolvedTarget.href.replace(/\*/g, subpath);
|
267
377
|
}
|
378
|
+
return new URL(subpath, resolvedTarget).href;
|
268
379
|
}
|
269
|
-
return null;
|
270
|
-
}
|
271
380
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
381
|
+
if (Array.isArray(target)) {
|
382
|
+
let lastError;
|
383
|
+
for (const item of target) {
|
384
|
+
try {
|
385
|
+
const resolved = await resolvePackageTarget(context, {
|
386
|
+
target: item,
|
387
|
+
subpath,
|
388
|
+
pattern,
|
389
|
+
internal
|
390
|
+
});
|
391
|
+
|
392
|
+
// return if defined or null, but not undefined
|
393
|
+
if (resolved !== undefined) {
|
394
|
+
return resolved;
|
395
|
+
}
|
396
|
+
} catch (error) {
|
397
|
+
if (!(error instanceof InvalidPackageTargetError)) {
|
398
|
+
throw error;
|
399
|
+
} else {
|
400
|
+
lastError = error;
|
401
|
+
}
|
402
|
+
}
|
279
403
|
}
|
280
404
|
|
281
|
-
if (
|
282
|
-
|
283
|
-
return `${value}${subPath.substring(key.length)}`;
|
405
|
+
if (lastError) {
|
406
|
+
throw lastError;
|
284
407
|
}
|
408
|
+
return null;
|
409
|
+
}
|
410
|
+
|
411
|
+
if (target && typeof target === 'object') {
|
412
|
+
for (const [key, value] of Object.entries(target)) {
|
413
|
+
if (key === 'default' || context.conditions.includes(key)) {
|
414
|
+
const resolved = await resolvePackageTarget(context, {
|
415
|
+
target: value,
|
416
|
+
subpath,
|
417
|
+
pattern,
|
418
|
+
internal
|
419
|
+
});
|
285
420
|
|
286
|
-
|
287
|
-
|
421
|
+
// return if defined or null, but not undefined
|
422
|
+
if (resolved !== undefined) {
|
423
|
+
return resolved;
|
424
|
+
}
|
425
|
+
}
|
426
|
+
}
|
427
|
+
return undefined;
|
288
428
|
}
|
289
429
|
|
290
|
-
if (
|
291
|
-
|
292
|
-
return value.find((v) => v.startsWith('./'));
|
430
|
+
if (target === null) {
|
431
|
+
return null;
|
293
432
|
}
|
294
433
|
|
295
|
-
throw
|
434
|
+
throw new InvalidPackageTargetError(context, `Invalid exports field.`);
|
296
435
|
}
|
297
436
|
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
key
|
306
|
-
}) {
|
307
|
-
if (typeof exportMap !== 'object') {
|
308
|
-
return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value: exportMap });
|
437
|
+
/* eslint-disable no-await-in-loop */
|
438
|
+
|
439
|
+
async function resolvePackageImportsExports(context, { matchKey, matchObj, internal }) {
|
440
|
+
if (!matchKey.endsWith('*') && matchKey in matchObj) {
|
441
|
+
const target = matchObj[matchKey];
|
442
|
+
const resolved = await resolvePackageTarget(context, { target, subpath: '', internal });
|
443
|
+
return resolved;
|
309
444
|
}
|
310
445
|
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
446
|
+
const expansionKeys = Object.keys(matchObj)
|
447
|
+
.filter((k) => k.endsWith('/') || k.endsWith('*'))
|
448
|
+
.sort((a, b) => b.length - a.length);
|
449
|
+
|
450
|
+
for (const expansionKey of expansionKeys) {
|
451
|
+
const prefix = expansionKey.substring(0, expansionKey.length - 1);
|
452
|
+
|
453
|
+
if (expansionKey.endsWith('*') && matchKey.startsWith(prefix)) {
|
454
|
+
const target = matchObj[expansionKey];
|
455
|
+
const subpath = matchKey.substring(expansionKey.length - 1);
|
456
|
+
const resolved = await resolvePackageTarget(context, {
|
457
|
+
target,
|
458
|
+
subpath,
|
459
|
+
pattern: true,
|
460
|
+
internal
|
322
461
|
});
|
323
|
-
|
324
|
-
return mappedSubPath;
|
325
|
-
}
|
462
|
+
return resolved;
|
326
463
|
}
|
327
|
-
}
|
328
|
-
throw pathNotFoundError(importer, subPath, pkgJsonPath);
|
329
|
-
}
|
330
464
|
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
conditions
|
338
|
-
}) {
|
339
|
-
if (typeof exportMap !== 'object') {
|
340
|
-
// the export map shorthand, for example { exports: "./index.js" }
|
341
|
-
if (subPath !== '.') {
|
342
|
-
// shorthand only supports a main entrypoint
|
343
|
-
throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
|
465
|
+
if (matchKey.startsWith(expansionKey)) {
|
466
|
+
const target = matchObj[expansionKey];
|
467
|
+
const subpath = matchKey.substring(expansionKey.length);
|
468
|
+
|
469
|
+
const resolved = await resolvePackageTarget(context, { target, subpath, internal });
|
470
|
+
return resolved;
|
344
471
|
}
|
345
|
-
return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key: null, value: exportMap });
|
346
472
|
}
|
347
473
|
|
348
|
-
|
349
|
-
|
350
|
-
const isConditions = keys.every((k) => !k.startsWith('.'));
|
351
|
-
const isMappings = keys.every((k) => k.startsWith('.'));
|
474
|
+
throw new InvalidModuleSpecifierError(context, internal);
|
475
|
+
}
|
352
476
|
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
477
|
+
async function resolvePackageExports(context, subpath, exports) {
|
478
|
+
if (isMixedExports(exports)) {
|
479
|
+
throw new InvalidConfigurationError(
|
480
|
+
context,
|
481
|
+
'All keys must either start with ./, or without one.'
|
358
482
|
);
|
359
483
|
}
|
360
484
|
|
361
|
-
|
362
|
-
|
485
|
+
if (subpath === '.') {
|
486
|
+
let mainExport;
|
487
|
+
// If exports is a String or Array, or an Object containing no keys starting with ".", then
|
488
|
+
if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) {
|
489
|
+
mainExport = exports;
|
490
|
+
} else if (isMappings(exports)) {
|
491
|
+
mainExport = exports['.'];
|
492
|
+
}
|
363
493
|
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
494
|
+
if (mainExport) {
|
495
|
+
const resolved = await resolvePackageTarget(context, { target: mainExport, subpath: '' });
|
496
|
+
if (resolved) {
|
497
|
+
return resolved;
|
498
|
+
}
|
369
499
|
}
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
500
|
+
} else if (isMappings(exports)) {
|
501
|
+
const resolvedMatch = await resolvePackageImportsExports(context, {
|
502
|
+
matchKey: subpath,
|
503
|
+
matchObj: exports
|
504
|
+
});
|
505
|
+
|
506
|
+
if (resolvedMatch) {
|
507
|
+
return resolvedMatch;
|
376
508
|
}
|
377
|
-
exportMapForSubPath = exportMap[key];
|
378
509
|
}
|
379
510
|
|
380
|
-
|
381
|
-
|
511
|
+
throw new InvalidModuleSpecifierError(context);
|
512
|
+
}
|
513
|
+
|
514
|
+
async function resolvePackageImports({
|
515
|
+
importSpecifier,
|
516
|
+
importer,
|
517
|
+
moduleDirs,
|
518
|
+
conditions,
|
519
|
+
resolveId
|
520
|
+
}) {
|
521
|
+
const result = await findPackageJson(importer, moduleDirs);
|
522
|
+
if (!result) {
|
523
|
+
throw new Error(createBaseErrorMsg('. Could not find a parent package.json.'));
|
524
|
+
}
|
525
|
+
|
526
|
+
const { pkgPath, pkgJsonPath, pkgJson } = result;
|
527
|
+
const pkgURL = url.pathToFileURL(`${pkgPath}/`);
|
528
|
+
const context = {
|
382
529
|
importer,
|
530
|
+
importSpecifier,
|
531
|
+
moduleDirs,
|
532
|
+
pkgURL,
|
383
533
|
pkgJsonPath,
|
384
|
-
subPath,
|
385
|
-
exportMap: exportMapForSubPath,
|
386
534
|
conditions,
|
387
|
-
|
535
|
+
resolveId
|
536
|
+
};
|
537
|
+
|
538
|
+
const { imports } = pkgJson;
|
539
|
+
if (!imports) {
|
540
|
+
throw new InvalidModuleSpecifierError(context, true);
|
541
|
+
}
|
542
|
+
|
543
|
+
if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
|
544
|
+
throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
|
545
|
+
}
|
546
|
+
|
547
|
+
return resolvePackageImportsExports(context, {
|
548
|
+
matchKey: importSpecifier,
|
549
|
+
matchObj: imports,
|
550
|
+
internal: true
|
388
551
|
});
|
389
552
|
}
|
390
553
|
|
554
|
+
const resolveImportPath = util.promisify(resolve__default['default']);
|
555
|
+
const readFile$1 = util.promisify(fs__default['default'].readFile);
|
556
|
+
|
557
|
+
async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
|
558
|
+
if (importer) {
|
559
|
+
const selfPackageJsonResult = await findPackageJson(importer, moduleDirectories);
|
560
|
+
if (selfPackageJsonResult && selfPackageJsonResult.pkgJson.name === pkgName) {
|
561
|
+
// the referenced package name is the current package
|
562
|
+
return selfPackageJsonResult;
|
563
|
+
}
|
564
|
+
}
|
565
|
+
|
566
|
+
try {
|
567
|
+
const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
|
568
|
+
const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
|
569
|
+
return { pkgJsonPath, pkgJson };
|
570
|
+
} catch (_) {
|
571
|
+
return null;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
|
391
575
|
async function resolveId({
|
392
576
|
importer,
|
393
|
-
|
577
|
+
importSpecifier,
|
394
578
|
exportConditions,
|
395
579
|
warn,
|
396
580
|
packageInfoCache,
|
@@ -399,7 +583,9 @@ async function resolveId({
|
|
399
583
|
preserveSymlinks,
|
400
584
|
useBrowserOverrides,
|
401
585
|
baseDir,
|
402
|
-
moduleDirectories
|
586
|
+
moduleDirectories,
|
587
|
+
rootDir,
|
588
|
+
ignoreSideEffectsForRoot
|
403
589
|
}) {
|
404
590
|
let hasModuleSideEffects = () => null;
|
405
591
|
let hasPackageEntry = true;
|
@@ -414,7 +600,9 @@ async function resolveId({
|
|
414
600
|
pkgPath,
|
415
601
|
mainFields,
|
416
602
|
preserveSymlinks,
|
417
|
-
useBrowserOverrides
|
603
|
+
useBrowserOverrides,
|
604
|
+
rootDir,
|
605
|
+
ignoreSideEffectsForRoot
|
418
606
|
});
|
419
607
|
|
420
608
|
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
|
@@ -436,41 +624,70 @@ async function resolveId({
|
|
436
624
|
|
437
625
|
let location;
|
438
626
|
|
439
|
-
const pkgName = getPackageName(
|
440
|
-
if (
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
627
|
+
const pkgName = getPackageName(importSpecifier);
|
628
|
+
if (importSpecifier.startsWith('#')) {
|
629
|
+
// this is a package internal import, resolve using package imports field
|
630
|
+
const resolveResult = await resolvePackageImports({
|
631
|
+
importSpecifier,
|
632
|
+
importer,
|
633
|
+
moduleDirs: moduleDirectories,
|
634
|
+
conditions: exportConditions,
|
635
|
+
resolveId(id, parent) {
|
636
|
+
return resolveId({
|
637
|
+
importSpecifier: id,
|
638
|
+
importer: parent,
|
639
|
+
exportConditions,
|
640
|
+
warn,
|
641
|
+
packageInfoCache,
|
642
|
+
extensions,
|
643
|
+
mainFields,
|
644
|
+
preserveSymlinks,
|
645
|
+
useBrowserOverrides,
|
646
|
+
baseDir,
|
647
|
+
moduleDirectories
|
648
|
+
});
|
649
|
+
}
|
650
|
+
});
|
651
|
+
location = url.fileURLToPath(resolveResult);
|
652
|
+
} else if (pkgName) {
|
653
|
+
// it's a bare import, find the package.json and resolve using package exports if available
|
654
|
+
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
|
449
655
|
|
450
|
-
if (
|
656
|
+
if (result && result.pkgJson.exports) {
|
657
|
+
const { pkgJson, pkgJsonPath } = result;
|
451
658
|
try {
|
452
|
-
const
|
453
|
-
pkgName ===
|
454
|
-
const
|
659
|
+
const subpath =
|
660
|
+
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
|
661
|
+
const pkgDr = pkgJsonPath.replace('package.json', '');
|
662
|
+
const pkgURL = url.pathToFileURL(pkgDr);
|
663
|
+
|
664
|
+
const context = {
|
455
665
|
importer,
|
456
|
-
|
666
|
+
importSpecifier,
|
667
|
+
moduleDirs: moduleDirectories,
|
668
|
+
pkgURL,
|
457
669
|
pkgJsonPath,
|
458
|
-
subPath: packageSubPath,
|
459
|
-
exportMap: pkgJson.exports,
|
460
670
|
conditions: exportConditions
|
461
|
-
}
|
462
|
-
const
|
463
|
-
|
671
|
+
};
|
672
|
+
const resolvedPackageExport = await resolvePackageExports(
|
673
|
+
context,
|
674
|
+
subpath,
|
675
|
+
pkgJson.exports
|
676
|
+
);
|
677
|
+
location = url.fileURLToPath(resolvedPackageExport);
|
464
678
|
} catch (error) {
|
465
|
-
|
466
|
-
|
679
|
+
if (error instanceof ResolveError) {
|
680
|
+
return error;
|
681
|
+
}
|
682
|
+
throw error;
|
467
683
|
}
|
468
684
|
}
|
469
685
|
}
|
470
686
|
|
471
687
|
if (!location) {
|
688
|
+
// package has no imports or exports, use classic node resolve
|
472
689
|
try {
|
473
|
-
location = await resolveImportPath(
|
690
|
+
location = await resolveImportPath(importSpecifier, resolveOptions);
|
474
691
|
} catch (error) {
|
475
692
|
if (error.code !== 'MODULE_NOT_FOUND') {
|
476
693
|
throw error;
|
@@ -507,13 +724,17 @@ async function resolveImportSpecifiers({
|
|
507
724
|
preserveSymlinks,
|
508
725
|
useBrowserOverrides,
|
509
726
|
baseDir,
|
510
|
-
moduleDirectories
|
727
|
+
moduleDirectories,
|
728
|
+
rootDir,
|
729
|
+
ignoreSideEffectsForRoot
|
511
730
|
}) {
|
731
|
+
let lastResolveError;
|
732
|
+
|
512
733
|
for (let i = 0; i < importSpecifierList.length; i++) {
|
513
734
|
// eslint-disable-next-line no-await-in-loop
|
514
|
-
const
|
735
|
+
const result = await resolveId({
|
515
736
|
importer,
|
516
|
-
|
737
|
+
importSpecifier: importSpecifierList[i],
|
517
738
|
exportConditions,
|
518
739
|
warn,
|
519
740
|
packageInfoCache,
|
@@ -522,12 +743,22 @@ async function resolveImportSpecifiers({
|
|
522
743
|
preserveSymlinks,
|
523
744
|
useBrowserOverrides,
|
524
745
|
baseDir,
|
525
|
-
moduleDirectories
|
746
|
+
moduleDirectories,
|
747
|
+
rootDir,
|
748
|
+
ignoreSideEffectsForRoot
|
526
749
|
});
|
527
|
-
|
528
|
-
|
750
|
+
|
751
|
+
if (result instanceof ResolveError) {
|
752
|
+
lastResolveError = result;
|
753
|
+
} else if (result) {
|
754
|
+
return result;
|
529
755
|
}
|
530
756
|
}
|
757
|
+
|
758
|
+
if (lastResolveError) {
|
759
|
+
// only log the last failed resolve error
|
760
|
+
warn(lastResolveError);
|
761
|
+
}
|
531
762
|
return null;
|
532
763
|
}
|
533
764
|
|
@@ -603,7 +834,8 @@ const defaults = {
|
|
603
834
|
// which deploy both ESM .mjs and CommonJS .js files as ESM.
|
604
835
|
extensions: ['.mjs', '.js', '.json', '.node'],
|
605
836
|
resolveOnly: [],
|
606
|
-
moduleDirectories: ['node_modules']
|
837
|
+
moduleDirectories: ['node_modules'],
|
838
|
+
ignoreSideEffectsForRoot: false
|
607
839
|
};
|
608
840
|
const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
|
609
841
|
|
@@ -611,7 +843,7 @@ function nodeResolve(opts = {}) {
|
|
611
843
|
const { warnings } = handleDeprecatedOptions(opts);
|
612
844
|
|
613
845
|
const options = { ...defaults, ...opts };
|
614
|
-
const { extensions, jail, moduleDirectories } = options;
|
846
|
+
const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
|
615
847
|
const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
|
616
848
|
const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
|
617
849
|
const packageInfoCache = new Map();
|
@@ -620,7 +852,7 @@ function nodeResolve(opts = {}) {
|
|
620
852
|
const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
|
621
853
|
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
|
622
854
|
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
|
623
|
-
const rootDir = options.rootDir || process.cwd();
|
855
|
+
const rootDir = path.resolve(options.rootDir || process.cwd());
|
624
856
|
let { dedupe } = options;
|
625
857
|
let rollupOptions;
|
626
858
|
|
@@ -769,7 +1001,9 @@ function nodeResolve(opts = {}) {
|
|
769
1001
|
preserveSymlinks,
|
770
1002
|
useBrowserOverrides,
|
771
1003
|
baseDir,
|
772
|
-
moduleDirectories
|
1004
|
+
moduleDirectories,
|
1005
|
+
rootDir,
|
1006
|
+
ignoreSideEffectsForRoot
|
773
1007
|
});
|
774
1008
|
|
775
1009
|
const resolved =
|