@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/es/index.js
CHANGED
@@ -4,13 +4,22 @@ import deepMerge from 'deepmerge';
|
|
4
4
|
import isModule from 'is-module';
|
5
5
|
import fs, { realpathSync } from 'fs';
|
6
6
|
import { promisify } from 'util';
|
7
|
+
import { pathToFileURL, fileURLToPath } from 'url';
|
7
8
|
import resolve$1 from 'resolve';
|
8
9
|
import { createFilter } from '@rollup/pluginutils';
|
9
10
|
|
10
|
-
const
|
11
|
+
const access = promisify(fs.access);
|
11
12
|
const readFile = promisify(fs.readFile);
|
12
13
|
const realpath = promisify(fs.realpath);
|
13
14
|
const stat = promisify(fs.stat);
|
15
|
+
async function exists(filePath) {
|
16
|
+
try {
|
17
|
+
await access(filePath);
|
18
|
+
return true;
|
19
|
+
} catch {
|
20
|
+
return false;
|
21
|
+
}
|
22
|
+
}
|
14
23
|
|
15
24
|
const onError = (error) => {
|
16
25
|
if (error.code === 'ENOENT') {
|
@@ -102,7 +111,16 @@ function getMainFields(options) {
|
|
102
111
|
}
|
103
112
|
|
104
113
|
function getPackageInfo(options) {
|
105
|
-
const {
|
114
|
+
const {
|
115
|
+
cache,
|
116
|
+
extensions,
|
117
|
+
pkg,
|
118
|
+
mainFields,
|
119
|
+
preserveSymlinks,
|
120
|
+
useBrowserOverrides,
|
121
|
+
rootDir,
|
122
|
+
ignoreSideEffectsForRoot
|
123
|
+
} = options;
|
106
124
|
let { pkgPath } = options;
|
107
125
|
|
108
126
|
if (cache.has(pkgPath)) {
|
@@ -192,13 +210,15 @@ function getPackageInfo(options) {
|
|
192
210
|
packageInfo.browserMappedMain = false;
|
193
211
|
}
|
194
212
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
213
|
+
if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
|
214
|
+
const packageSideEffects = pkg.sideEffects;
|
215
|
+
if (typeof packageSideEffects === 'boolean') {
|
216
|
+
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
|
217
|
+
} else if (Array.isArray(packageSideEffects)) {
|
218
|
+
internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
|
219
|
+
resolve: pkgRoot
|
220
|
+
});
|
221
|
+
}
|
202
222
|
}
|
203
223
|
|
204
224
|
cache.set(pkgPath, internalPackageInfo);
|
@@ -216,168 +236,332 @@ function normalizeInput(input) {
|
|
216
236
|
return [input];
|
217
237
|
}
|
218
238
|
|
219
|
-
|
220
|
-
|
239
|
+
/* eslint-disable no-await-in-loop */
|
240
|
+
|
241
|
+
const fileExists = promisify(fs.exists);
|
242
|
+
|
243
|
+
function isModuleDir(current, moduleDirs) {
|
244
|
+
return moduleDirs.some((dir) => current.endsWith(dir));
|
245
|
+
}
|
246
|
+
|
247
|
+
async function findPackageJson(base, moduleDirs) {
|
248
|
+
const { root } = path.parse(base);
|
249
|
+
let current = base;
|
221
250
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
251
|
+
while (current !== root && !isModuleDir(current, moduleDirs)) {
|
252
|
+
const pkgJsonPath = path.join(current, 'package.json');
|
253
|
+
if (await fileExists(pkgJsonPath)) {
|
254
|
+
const pkgJsonString = fs.readFileSync(pkgJsonPath, 'utf-8');
|
255
|
+
return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
|
256
|
+
}
|
257
|
+
current = path.resolve(current, '..');
|
258
|
+
}
|
259
|
+
return null;
|
260
|
+
}
|
227
261
|
|
228
|
-
function
|
229
|
-
|
230
|
-
return
|
262
|
+
function isUrl(str) {
|
263
|
+
try {
|
264
|
+
return !!new URL(str);
|
265
|
+
} catch (_) {
|
266
|
+
return false;
|
231
267
|
}
|
268
|
+
}
|
232
269
|
|
233
|
-
|
234
|
-
|
235
|
-
|
270
|
+
function isConditions(exports) {
|
271
|
+
return typeof exports === 'object' && Object.keys(exports).every((k) => !k.startsWith('.'));
|
272
|
+
}
|
273
|
+
|
274
|
+
function isMappings(exports) {
|
275
|
+
return typeof exports === 'object' && !isConditions(exports);
|
276
|
+
}
|
277
|
+
|
278
|
+
function isMixedExports(exports) {
|
279
|
+
const keys = Object.keys(exports);
|
280
|
+
return keys.some((k) => k.startsWith('.')) && keys.some((k) => !k.startsWith('.'));
|
281
|
+
}
|
282
|
+
|
283
|
+
function createBaseErrorMsg(importSpecifier, importer) {
|
284
|
+
return `Could not resolve import "${importSpecifier}" in ${importer}`;
|
285
|
+
}
|
286
|
+
|
287
|
+
function createErrorMsg(context, reason, internal) {
|
288
|
+
const { importSpecifier, importer, pkgJsonPath } = context;
|
289
|
+
const base = createBaseErrorMsg(importSpecifier, importer);
|
290
|
+
const field = internal ? 'imports' : 'exports';
|
291
|
+
return `${base} using ${field} defined in ${pkgJsonPath}.${reason ? ` ${reason}` : ''}`;
|
292
|
+
}
|
293
|
+
|
294
|
+
class ResolveError extends Error {}
|
295
|
+
|
296
|
+
class InvalidConfigurationError extends ResolveError {
|
297
|
+
constructor(context, reason) {
|
298
|
+
super(createErrorMsg(context, `Invalid "exports" field. ${reason}`));
|
299
|
+
}
|
300
|
+
}
|
301
|
+
|
302
|
+
class InvalidModuleSpecifierError extends ResolveError {
|
303
|
+
constructor(context, internal) {
|
304
|
+
super(createErrorMsg(context, internal));
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
class InvalidPackageTargetError extends ResolveError {
|
309
|
+
constructor(context, reason) {
|
310
|
+
super(createErrorMsg(context, reason));
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
/* eslint-disable no-await-in-loop, no-undefined */
|
236
315
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
316
|
+
function includesInvalidSegments(pathSegments, moduleDirs) {
|
317
|
+
return pathSegments
|
318
|
+
.split('/')
|
319
|
+
.slice(1)
|
320
|
+
.some((t) => ['.', '..', ...moduleDirs].includes(t));
|
321
|
+
}
|
322
|
+
|
323
|
+
async function resolvePackageTarget(context, { target, subpath, pattern, internal }) {
|
324
|
+
if (typeof target === 'string') {
|
325
|
+
if (!pattern && subpath.length > 0 && !target.endsWith('/')) {
|
326
|
+
throw new InvalidModuleSpecifierError(context);
|
327
|
+
}
|
328
|
+
|
329
|
+
if (!target.startsWith('./')) {
|
330
|
+
if (internal && !['/', '../'].some((p) => target.startsWith(p)) && !isUrl(target)) {
|
331
|
+
// this is a bare package import, remap it and resolve it using regular node resolve
|
332
|
+
if (pattern) {
|
333
|
+
const result = await context.resolveId(
|
334
|
+
target.replace(/\*/g, subpath),
|
335
|
+
context.pkgURL.href
|
336
|
+
);
|
337
|
+
return result ? pathToFileURL(result.location) : null;
|
338
|
+
}
|
339
|
+
|
340
|
+
const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
|
341
|
+
return result ? pathToFileURL(result.location) : null;
|
243
342
|
}
|
343
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
344
|
+
}
|
345
|
+
|
346
|
+
if (includesInvalidSegments(target, context.moduleDirs)) {
|
347
|
+
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
244
348
|
}
|
245
349
|
|
246
|
-
|
247
|
-
|
248
|
-
|
350
|
+
const resolvedTarget = new URL(target, context.pkgURL);
|
351
|
+
if (!resolvedTarget.href.startsWith(context.pkgURL.href)) {
|
352
|
+
throw new InvalidPackageTargetError(
|
353
|
+
context,
|
354
|
+
`Resolved to ${resolvedTarget.href} which is outside package ${context.pkgURL.href}`
|
355
|
+
);
|
356
|
+
}
|
357
|
+
|
358
|
+
if (includesInvalidSegments(subpath, context.moduleDirs)) {
|
359
|
+
throw new InvalidModuleSpecifierError(context);
|
249
360
|
}
|
250
361
|
|
251
|
-
if (
|
252
|
-
|
253
|
-
return key;
|
362
|
+
if (pattern) {
|
363
|
+
return resolvedTarget.href.replace(/\*/g, subpath);
|
254
364
|
}
|
365
|
+
return new URL(subpath, resolvedTarget).href;
|
255
366
|
}
|
256
|
-
return null;
|
257
|
-
}
|
258
367
|
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
368
|
+
if (Array.isArray(target)) {
|
369
|
+
let lastError;
|
370
|
+
for (const item of target) {
|
371
|
+
try {
|
372
|
+
const resolved = await resolvePackageTarget(context, {
|
373
|
+
target: item,
|
374
|
+
subpath,
|
375
|
+
pattern,
|
376
|
+
internal
|
377
|
+
});
|
378
|
+
|
379
|
+
// return if defined or null, but not undefined
|
380
|
+
if (resolved !== undefined) {
|
381
|
+
return resolved;
|
382
|
+
}
|
383
|
+
} catch (error) {
|
384
|
+
if (!(error instanceof InvalidPackageTargetError)) {
|
385
|
+
throw error;
|
386
|
+
} else {
|
387
|
+
lastError = error;
|
388
|
+
}
|
389
|
+
}
|
266
390
|
}
|
267
391
|
|
268
|
-
if (
|
269
|
-
|
270
|
-
return `${value}${subPath.substring(key.length)}`;
|
392
|
+
if (lastError) {
|
393
|
+
throw lastError;
|
271
394
|
}
|
395
|
+
return null;
|
396
|
+
}
|
397
|
+
|
398
|
+
if (target && typeof target === 'object') {
|
399
|
+
for (const [key, value] of Object.entries(target)) {
|
400
|
+
if (key === 'default' || context.conditions.includes(key)) {
|
401
|
+
const resolved = await resolvePackageTarget(context, {
|
402
|
+
target: value,
|
403
|
+
subpath,
|
404
|
+
pattern,
|
405
|
+
internal
|
406
|
+
});
|
272
407
|
|
273
|
-
|
274
|
-
|
408
|
+
// return if defined or null, but not undefined
|
409
|
+
if (resolved !== undefined) {
|
410
|
+
return resolved;
|
411
|
+
}
|
412
|
+
}
|
413
|
+
}
|
414
|
+
return undefined;
|
275
415
|
}
|
276
416
|
|
277
|
-
if (
|
278
|
-
|
279
|
-
return value.find((v) => v.startsWith('./'));
|
417
|
+
if (target === null) {
|
418
|
+
return null;
|
280
419
|
}
|
281
420
|
|
282
|
-
throw
|
421
|
+
throw new InvalidPackageTargetError(context, `Invalid exports field.`);
|
283
422
|
}
|
284
423
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
key
|
293
|
-
}) {
|
294
|
-
if (typeof exportMap !== 'object') {
|
295
|
-
return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value: exportMap });
|
424
|
+
/* eslint-disable no-await-in-loop */
|
425
|
+
|
426
|
+
async function resolvePackageImportsExports(context, { matchKey, matchObj, internal }) {
|
427
|
+
if (!matchKey.endsWith('*') && matchKey in matchObj) {
|
428
|
+
const target = matchObj[matchKey];
|
429
|
+
const resolved = await resolvePackageTarget(context, { target, subpath: '', internal });
|
430
|
+
return resolved;
|
296
431
|
}
|
297
432
|
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
433
|
+
const expansionKeys = Object.keys(matchObj)
|
434
|
+
.filter((k) => k.endsWith('/') || k.endsWith('*'))
|
435
|
+
.sort((a, b) => b.length - a.length);
|
436
|
+
|
437
|
+
for (const expansionKey of expansionKeys) {
|
438
|
+
const prefix = expansionKey.substring(0, expansionKey.length - 1);
|
439
|
+
|
440
|
+
if (expansionKey.endsWith('*') && matchKey.startsWith(prefix)) {
|
441
|
+
const target = matchObj[expansionKey];
|
442
|
+
const subpath = matchKey.substring(expansionKey.length - 1);
|
443
|
+
const resolved = await resolvePackageTarget(context, {
|
444
|
+
target,
|
445
|
+
subpath,
|
446
|
+
pattern: true,
|
447
|
+
internal
|
309
448
|
});
|
310
|
-
|
311
|
-
return mappedSubPath;
|
312
|
-
}
|
449
|
+
return resolved;
|
313
450
|
}
|
314
|
-
}
|
315
|
-
throw pathNotFoundError(importer, subPath, pkgJsonPath);
|
316
|
-
}
|
317
451
|
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
conditions
|
325
|
-
}) {
|
326
|
-
if (typeof exportMap !== 'object') {
|
327
|
-
// the export map shorthand, for example { exports: "./index.js" }
|
328
|
-
if (subPath !== '.') {
|
329
|
-
// shorthand only supports a main entrypoint
|
330
|
-
throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
|
452
|
+
if (matchKey.startsWith(expansionKey)) {
|
453
|
+
const target = matchObj[expansionKey];
|
454
|
+
const subpath = matchKey.substring(expansionKey.length);
|
455
|
+
|
456
|
+
const resolved = await resolvePackageTarget(context, { target, subpath, internal });
|
457
|
+
return resolved;
|
331
458
|
}
|
332
|
-
return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key: null, value: exportMap });
|
333
459
|
}
|
334
460
|
|
335
|
-
|
336
|
-
|
337
|
-
const isConditions = keys.every((k) => !k.startsWith('.'));
|
338
|
-
const isMappings = keys.every((k) => k.startsWith('.'));
|
461
|
+
throw new InvalidModuleSpecifierError(context, internal);
|
462
|
+
}
|
339
463
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
464
|
+
async function resolvePackageExports(context, subpath, exports) {
|
465
|
+
if (isMixedExports(exports)) {
|
466
|
+
throw new InvalidConfigurationError(
|
467
|
+
context,
|
468
|
+
'All keys must either start with ./, or without one.'
|
345
469
|
);
|
346
470
|
}
|
347
471
|
|
348
|
-
|
349
|
-
|
472
|
+
if (subpath === '.') {
|
473
|
+
let mainExport;
|
474
|
+
// If exports is a String or Array, or an Object containing no keys starting with ".", then
|
475
|
+
if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) {
|
476
|
+
mainExport = exports;
|
477
|
+
} else if (isMappings(exports)) {
|
478
|
+
mainExport = exports['.'];
|
479
|
+
}
|
350
480
|
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
481
|
+
if (mainExport) {
|
482
|
+
const resolved = await resolvePackageTarget(context, { target: mainExport, subpath: '' });
|
483
|
+
if (resolved) {
|
484
|
+
return resolved;
|
485
|
+
}
|
356
486
|
}
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
487
|
+
} else if (isMappings(exports)) {
|
488
|
+
const resolvedMatch = await resolvePackageImportsExports(context, {
|
489
|
+
matchKey: subpath,
|
490
|
+
matchObj: exports
|
491
|
+
});
|
492
|
+
|
493
|
+
if (resolvedMatch) {
|
494
|
+
return resolvedMatch;
|
363
495
|
}
|
364
|
-
exportMapForSubPath = exportMap[key];
|
365
496
|
}
|
366
497
|
|
367
|
-
|
368
|
-
|
498
|
+
throw new InvalidModuleSpecifierError(context);
|
499
|
+
}
|
500
|
+
|
501
|
+
async function resolvePackageImports({
|
502
|
+
importSpecifier,
|
503
|
+
importer,
|
504
|
+
moduleDirs,
|
505
|
+
conditions,
|
506
|
+
resolveId
|
507
|
+
}) {
|
508
|
+
const result = await findPackageJson(importer, moduleDirs);
|
509
|
+
if (!result) {
|
510
|
+
throw new Error(createBaseErrorMsg('. Could not find a parent package.json.'));
|
511
|
+
}
|
512
|
+
|
513
|
+
const { pkgPath, pkgJsonPath, pkgJson } = result;
|
514
|
+
const pkgURL = pathToFileURL(`${pkgPath}/`);
|
515
|
+
const context = {
|
369
516
|
importer,
|
517
|
+
importSpecifier,
|
518
|
+
moduleDirs,
|
519
|
+
pkgURL,
|
370
520
|
pkgJsonPath,
|
371
|
-
subPath,
|
372
|
-
exportMap: exportMapForSubPath,
|
373
521
|
conditions,
|
374
|
-
|
522
|
+
resolveId
|
523
|
+
};
|
524
|
+
|
525
|
+
const { imports } = pkgJson;
|
526
|
+
if (!imports) {
|
527
|
+
throw new InvalidModuleSpecifierError(context, true);
|
528
|
+
}
|
529
|
+
|
530
|
+
if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
|
531
|
+
throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
|
532
|
+
}
|
533
|
+
|
534
|
+
return resolvePackageImportsExports(context, {
|
535
|
+
matchKey: importSpecifier,
|
536
|
+
matchObj: imports,
|
537
|
+
internal: true
|
375
538
|
});
|
376
539
|
}
|
377
540
|
|
541
|
+
const resolveImportPath = promisify(resolve$1);
|
542
|
+
const readFile$1 = promisify(fs.readFile);
|
543
|
+
|
544
|
+
async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
|
545
|
+
if (importer) {
|
546
|
+
const selfPackageJsonResult = await findPackageJson(importer, moduleDirectories);
|
547
|
+
if (selfPackageJsonResult && selfPackageJsonResult.pkgJson.name === pkgName) {
|
548
|
+
// the referenced package name is the current package
|
549
|
+
return selfPackageJsonResult;
|
550
|
+
}
|
551
|
+
}
|
552
|
+
|
553
|
+
try {
|
554
|
+
const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
|
555
|
+
const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
|
556
|
+
return { pkgJsonPath, pkgJson };
|
557
|
+
} catch (_) {
|
558
|
+
return null;
|
559
|
+
}
|
560
|
+
}
|
561
|
+
|
378
562
|
async function resolveId({
|
379
563
|
importer,
|
380
|
-
|
564
|
+
importSpecifier,
|
381
565
|
exportConditions,
|
382
566
|
warn,
|
383
567
|
packageInfoCache,
|
@@ -386,7 +570,9 @@ async function resolveId({
|
|
386
570
|
preserveSymlinks,
|
387
571
|
useBrowserOverrides,
|
388
572
|
baseDir,
|
389
|
-
moduleDirectories
|
573
|
+
moduleDirectories,
|
574
|
+
rootDir,
|
575
|
+
ignoreSideEffectsForRoot
|
390
576
|
}) {
|
391
577
|
let hasModuleSideEffects = () => null;
|
392
578
|
let hasPackageEntry = true;
|
@@ -401,7 +587,9 @@ async function resolveId({
|
|
401
587
|
pkgPath,
|
402
588
|
mainFields,
|
403
589
|
preserveSymlinks,
|
404
|
-
useBrowserOverrides
|
590
|
+
useBrowserOverrides,
|
591
|
+
rootDir,
|
592
|
+
ignoreSideEffectsForRoot
|
405
593
|
});
|
406
594
|
|
407
595
|
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
|
@@ -423,41 +611,70 @@ async function resolveId({
|
|
423
611
|
|
424
612
|
let location;
|
425
613
|
|
426
|
-
const pkgName = getPackageName(
|
427
|
-
if (
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
614
|
+
const pkgName = getPackageName(importSpecifier);
|
615
|
+
if (importSpecifier.startsWith('#')) {
|
616
|
+
// this is a package internal import, resolve using package imports field
|
617
|
+
const resolveResult = await resolvePackageImports({
|
618
|
+
importSpecifier,
|
619
|
+
importer,
|
620
|
+
moduleDirs: moduleDirectories,
|
621
|
+
conditions: exportConditions,
|
622
|
+
resolveId(id, parent) {
|
623
|
+
return resolveId({
|
624
|
+
importSpecifier: id,
|
625
|
+
importer: parent,
|
626
|
+
exportConditions,
|
627
|
+
warn,
|
628
|
+
packageInfoCache,
|
629
|
+
extensions,
|
630
|
+
mainFields,
|
631
|
+
preserveSymlinks,
|
632
|
+
useBrowserOverrides,
|
633
|
+
baseDir,
|
634
|
+
moduleDirectories
|
635
|
+
});
|
636
|
+
}
|
637
|
+
});
|
638
|
+
location = fileURLToPath(resolveResult);
|
639
|
+
} else if (pkgName) {
|
640
|
+
// it's a bare import, find the package.json and resolve using package exports if available
|
641
|
+
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
|
436
642
|
|
437
|
-
if (
|
643
|
+
if (result && result.pkgJson.exports) {
|
644
|
+
const { pkgJson, pkgJsonPath } = result;
|
438
645
|
try {
|
439
|
-
const
|
440
|
-
pkgName ===
|
441
|
-
const
|
646
|
+
const subpath =
|
647
|
+
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
|
648
|
+
const pkgDr = pkgJsonPath.replace('package.json', '');
|
649
|
+
const pkgURL = pathToFileURL(pkgDr);
|
650
|
+
|
651
|
+
const context = {
|
442
652
|
importer,
|
443
|
-
|
653
|
+
importSpecifier,
|
654
|
+
moduleDirs: moduleDirectories,
|
655
|
+
pkgURL,
|
444
656
|
pkgJsonPath,
|
445
|
-
subPath: packageSubPath,
|
446
|
-
exportMap: pkgJson.exports,
|
447
657
|
conditions: exportConditions
|
448
|
-
}
|
449
|
-
const
|
450
|
-
|
658
|
+
};
|
659
|
+
const resolvedPackageExport = await resolvePackageExports(
|
660
|
+
context,
|
661
|
+
subpath,
|
662
|
+
pkgJson.exports
|
663
|
+
);
|
664
|
+
location = fileURLToPath(resolvedPackageExport);
|
451
665
|
} catch (error) {
|
452
|
-
|
453
|
-
|
666
|
+
if (error instanceof ResolveError) {
|
667
|
+
return error;
|
668
|
+
}
|
669
|
+
throw error;
|
454
670
|
}
|
455
671
|
}
|
456
672
|
}
|
457
673
|
|
458
674
|
if (!location) {
|
675
|
+
// package has no imports or exports, use classic node resolve
|
459
676
|
try {
|
460
|
-
location = await resolveImportPath(
|
677
|
+
location = await resolveImportPath(importSpecifier, resolveOptions);
|
461
678
|
} catch (error) {
|
462
679
|
if (error.code !== 'MODULE_NOT_FOUND') {
|
463
680
|
throw error;
|
@@ -494,13 +711,17 @@ async function resolveImportSpecifiers({
|
|
494
711
|
preserveSymlinks,
|
495
712
|
useBrowserOverrides,
|
496
713
|
baseDir,
|
497
|
-
moduleDirectories
|
714
|
+
moduleDirectories,
|
715
|
+
rootDir,
|
716
|
+
ignoreSideEffectsForRoot
|
498
717
|
}) {
|
718
|
+
let lastResolveError;
|
719
|
+
|
499
720
|
for (let i = 0; i < importSpecifierList.length; i++) {
|
500
721
|
// eslint-disable-next-line no-await-in-loop
|
501
|
-
const
|
722
|
+
const result = await resolveId({
|
502
723
|
importer,
|
503
|
-
|
724
|
+
importSpecifier: importSpecifierList[i],
|
504
725
|
exportConditions,
|
505
726
|
warn,
|
506
727
|
packageInfoCache,
|
@@ -509,12 +730,22 @@ async function resolveImportSpecifiers({
|
|
509
730
|
preserveSymlinks,
|
510
731
|
useBrowserOverrides,
|
511
732
|
baseDir,
|
512
|
-
moduleDirectories
|
733
|
+
moduleDirectories,
|
734
|
+
rootDir,
|
735
|
+
ignoreSideEffectsForRoot
|
513
736
|
});
|
514
|
-
|
515
|
-
|
737
|
+
|
738
|
+
if (result instanceof ResolveError) {
|
739
|
+
lastResolveError = result;
|
740
|
+
} else if (result) {
|
741
|
+
return result;
|
516
742
|
}
|
517
743
|
}
|
744
|
+
|
745
|
+
if (lastResolveError) {
|
746
|
+
// only log the last failed resolve error
|
747
|
+
warn(lastResolveError);
|
748
|
+
}
|
518
749
|
return null;
|
519
750
|
}
|
520
751
|
|
@@ -590,7 +821,8 @@ const defaults = {
|
|
590
821
|
// which deploy both ESM .mjs and CommonJS .js files as ESM.
|
591
822
|
extensions: ['.mjs', '.js', '.json', '.node'],
|
592
823
|
resolveOnly: [],
|
593
|
-
moduleDirectories: ['node_modules']
|
824
|
+
moduleDirectories: ['node_modules'],
|
825
|
+
ignoreSideEffectsForRoot: false
|
594
826
|
};
|
595
827
|
const DEFAULTS = deepFreeze(deepMerge({}, defaults));
|
596
828
|
|
@@ -598,7 +830,7 @@ function nodeResolve(opts = {}) {
|
|
598
830
|
const { warnings } = handleDeprecatedOptions(opts);
|
599
831
|
|
600
832
|
const options = { ...defaults, ...opts };
|
601
|
-
const { extensions, jail, moduleDirectories } = options;
|
833
|
+
const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
|
602
834
|
const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
|
603
835
|
const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
|
604
836
|
const packageInfoCache = new Map();
|
@@ -607,7 +839,7 @@ function nodeResolve(opts = {}) {
|
|
607
839
|
const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
|
608
840
|
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
|
609
841
|
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
|
610
|
-
const rootDir = options.rootDir || process.cwd();
|
842
|
+
const rootDir = resolve(options.rootDir || process.cwd());
|
611
843
|
let { dedupe } = options;
|
612
844
|
let rollupOptions;
|
613
845
|
|
@@ -756,7 +988,9 @@ function nodeResolve(opts = {}) {
|
|
756
988
|
preserveSymlinks,
|
757
989
|
useBrowserOverrides,
|
758
990
|
baseDir,
|
759
|
-
moduleDirectories
|
991
|
+
moduleDirectories,
|
992
|
+
rootDir,
|
993
|
+
ignoreSideEffectsForRoot
|
760
994
|
});
|
761
995
|
|
762
996
|
const resolved =
|