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