@rollup/plugin-node-resolve 11.0.0 → 11.0.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # @rollup/plugin-node-resolve ChangeLog
2
2
 
3
+ ## v11.0.1
4
+
5
+ _2020-12-14_
6
+
7
+ ### Bugfixes
8
+
9
+ - fix: export map specificity (#675)
10
+ - fix: add missing type import (#668)
11
+
12
+ ### Updates
13
+
14
+ - docs: corrected word "yse" to "use" (#723)
15
+
3
16
  ## v11.0.0
4
17
 
5
18
  _2020-11-30_
package/README.md CHANGED
@@ -181,7 +181,7 @@ By default this plugin will prefer built-ins over local modules, marking them as
181
181
 
182
182
  See [`preferBuiltins`](#preferbuiltins).
183
183
 
184
- To provide stubbed versions of Node built-ins, yse a plugin like [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) or use [`builtin-modules`](https://github.com/sindresorhus/builtin-modules) with `external`, and set `preferBuiltins` to `false`. e.g.
184
+ To provide stubbed versions of Node built-ins, use a plugin like [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) or use [`builtin-modules`](https://github.com/sindresorhus/builtin-modules) with `external`, and set `preferBuiltins` to `false`. e.g.
185
185
 
186
186
  ```js
187
187
  import { nodeResolve } from '@rollup/plugin-node-resolve';
package/dist/cjs/index.js CHANGED
@@ -232,11 +232,22 @@ function normalizeInput(input) {
232
232
  const resolveImportPath = util.promisify(resolve__default['default']);
233
233
  const readFile$1 = util.promisify(fs__default['default'].readFile);
234
234
 
235
- const pathNotFoundError = (subPath, pkgPath) =>
236
- new Error(`Package subpath '${subPath}' is not defined by "exports" in ${pkgPath}`);
235
+ const pathNotFoundError = (importPath, importer, subPath, pkgPath) =>
236
+ new Error(
237
+ `Could not resolve import "${importPath}" in "${importer}".` +
238
+ ` Package subpath "${subPath}" is not defined by "exports" in ${pkgPath}`
239
+ );
237
240
 
238
241
  function findExportKeyMatch(exportMap, subPath) {
239
- for (const key of Object.keys(exportMap)) {
242
+ if (subPath in exportMap) {
243
+ return subPath;
244
+ }
245
+
246
+ const matchKeys = Object.keys(exportMap)
247
+ .filter((key) => key.endsWith('/') || key.endsWith('*'))
248
+ .sort((a, b) => b.length - a.length);
249
+
250
+ for (const key of matchKeys) {
240
251
  if (key.endsWith('*')) {
241
252
  // star match: "./foo/*": "./foo/*.js"
242
253
  const keyWithoutStar = key.substring(0, key.length - 1);
@@ -258,7 +269,7 @@ function findExportKeyMatch(exportMap, subPath) {
258
269
  return null;
259
270
  }
260
271
 
261
- function mapSubPath(pkgJsonPath, subPath, key, value) {
272
+ function mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value }) {
262
273
  if (typeof value === 'string') {
263
274
  if (typeof key === 'string' && key.endsWith('*')) {
264
275
  // star match: "./foo/*": "./foo/*.js"
@@ -281,34 +292,57 @@ function mapSubPath(pkgJsonPath, subPath, key, value) {
281
292
  return value.find((v) => v.startsWith('./'));
282
293
  }
283
294
 
284
- throw pathNotFoundError(subPath, pkgJsonPath);
295
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
285
296
  }
286
297
 
287
- function findEntrypoint(pkgJsonPath, subPath, exportMap, conditions, key) {
298
+ function findEntrypoint({
299
+ importPath,
300
+ importer,
301
+ pkgJsonPath,
302
+ subPath,
303
+ exportMap,
304
+ conditions,
305
+ key
306
+ }) {
288
307
  if (typeof exportMap !== 'object') {
289
- return mapSubPath(pkgJsonPath, subPath, key, exportMap);
308
+ return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value: exportMap });
290
309
  }
291
310
 
292
311
  // iterate conditions recursively, find the first that matches all conditions
293
312
  for (const [condition, subExportMap] of Object.entries(exportMap)) {
294
313
  if (conditions.includes(condition)) {
295
- const mappedSubPath = findEntrypoint(pkgJsonPath, subPath, subExportMap, conditions, key);
314
+ const mappedSubPath = findEntrypoint({
315
+ importPath,
316
+ importer,
317
+ pkgJsonPath,
318
+ subPath,
319
+ exportMap: subExportMap,
320
+ conditions,
321
+ key
322
+ });
296
323
  if (mappedSubPath) {
297
324
  return mappedSubPath;
298
325
  }
299
326
  }
300
327
  }
301
- throw pathNotFoundError(subPath, pkgJsonPath);
328
+ throw pathNotFoundError(importer, subPath, pkgJsonPath);
302
329
  }
303
330
 
304
- function findEntrypointTopLevel(pkgJsonPath, subPath, exportMap, conditions) {
331
+ function findEntrypointTopLevel({
332
+ importPath,
333
+ importer,
334
+ pkgJsonPath,
335
+ subPath,
336
+ exportMap,
337
+ conditions
338
+ }) {
305
339
  if (typeof exportMap !== 'object') {
306
340
  // the export map shorthand, for example { exports: "./index.js" }
307
341
  if (subPath !== '.') {
308
342
  // shorthand only supports a main entrypoint
309
- throw pathNotFoundError(subPath, pkgJsonPath);
343
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
310
344
  }
311
- return mapSubPath(pkgJsonPath, subPath, null, exportMap);
345
+ return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key: null, value: exportMap });
312
346
  }
313
347
 
314
348
  // export map is an object, the top level can be either conditions or sub path mappings
@@ -331,22 +365,31 @@ function findEntrypointTopLevel(pkgJsonPath, subPath, exportMap, conditions) {
331
365
  // top level is conditions, for example { "import": ..., "require": ..., "module": ... }
332
366
  if (subPath !== '.') {
333
367
  // package with top level conditions means it only supports a main entrypoint
334
- throw pathNotFoundError(subPath, pkgJsonPath);
368
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
335
369
  }
336
370
  exportMapForSubPath = exportMap;
337
371
  } else {
338
372
  // top level is sub path mappings, for example { ".": ..., "./foo": ..., "./bar": ... }
339
373
  key = findExportKeyMatch(exportMap, subPath);
340
374
  if (!key) {
341
- throw pathNotFoundError(subPath, pkgJsonPath);
375
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
342
376
  }
343
377
  exportMapForSubPath = exportMap[key];
344
378
  }
345
379
 
346
- return findEntrypoint(pkgJsonPath, subPath, exportMapForSubPath, conditions, key);
380
+ return findEntrypoint({
381
+ importPath,
382
+ importer,
383
+ pkgJsonPath,
384
+ subPath,
385
+ exportMap: exportMapForSubPath,
386
+ conditions,
387
+ key
388
+ });
347
389
  }
348
390
 
349
391
  async function resolveId({
392
+ importer,
350
393
  importPath,
351
394
  exportConditions,
352
395
  warn,
@@ -408,12 +451,14 @@ async function resolveId({
408
451
  try {
409
452
  const packageSubPath =
410
453
  pkgName === importPath ? '.' : `.${importPath.substring(pkgName.length)}`;
411
- const mappedSubPath = findEntrypointTopLevel(
454
+ const mappedSubPath = findEntrypointTopLevel({
455
+ importer,
456
+ importPath,
412
457
  pkgJsonPath,
413
- packageSubPath,
414
- pkgJson.exports,
415
- exportConditions
416
- );
458
+ subPath: packageSubPath,
459
+ exportMap: pkgJson.exports,
460
+ conditions: exportConditions
461
+ });
417
462
  const pkgDir = path__default['default'].dirname(pkgJsonPath);
418
463
  location = path__default['default'].join(pkgDir, mappedSubPath);
419
464
  } catch (error) {
@@ -452,6 +497,7 @@ async function resolveId({
452
497
  // Resolve module specifiers in order. Promise resolves to the first module that resolves
453
498
  // successfully, or the error that resulted from the last attempted module resolution.
454
499
  async function resolveImportSpecifiers({
500
+ importer,
455
501
  importSpecifierList,
456
502
  exportConditions,
457
503
  warn,
@@ -466,6 +512,7 @@ async function resolveImportSpecifiers({
466
512
  for (let i = 0; i < importSpecifierList.length; i++) {
467
513
  // eslint-disable-next-line no-await-in-loop
468
514
  const resolved = await resolveId({
515
+ importer,
469
516
  importPath: importSpecifierList[i],
470
517
  exportConditions,
471
518
  warn,
@@ -712,6 +759,7 @@ function nodeResolve(opts = {}) {
712
759
  const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
713
760
 
714
761
  const resolvedWithoutBuiltins = await resolveImportSpecifiers({
762
+ importer,
715
763
  importSpecifierList,
716
764
  exportConditions,
717
765
  warn,
package/dist/es/index.js CHANGED
@@ -219,11 +219,22 @@ function normalizeInput(input) {
219
219
  const resolveImportPath = promisify(resolve$1);
220
220
  const readFile$1 = promisify(fs.readFile);
221
221
 
222
- const pathNotFoundError = (subPath, pkgPath) =>
223
- new Error(`Package subpath '${subPath}' is not defined by "exports" in ${pkgPath}`);
222
+ const pathNotFoundError = (importPath, importer, subPath, pkgPath) =>
223
+ new Error(
224
+ `Could not resolve import "${importPath}" in "${importer}".` +
225
+ ` Package subpath "${subPath}" is not defined by "exports" in ${pkgPath}`
226
+ );
224
227
 
225
228
  function findExportKeyMatch(exportMap, subPath) {
226
- for (const key of Object.keys(exportMap)) {
229
+ if (subPath in exportMap) {
230
+ return subPath;
231
+ }
232
+
233
+ const matchKeys = Object.keys(exportMap)
234
+ .filter((key) => key.endsWith('/') || key.endsWith('*'))
235
+ .sort((a, b) => b.length - a.length);
236
+
237
+ for (const key of matchKeys) {
227
238
  if (key.endsWith('*')) {
228
239
  // star match: "./foo/*": "./foo/*.js"
229
240
  const keyWithoutStar = key.substring(0, key.length - 1);
@@ -245,7 +256,7 @@ function findExportKeyMatch(exportMap, subPath) {
245
256
  return null;
246
257
  }
247
258
 
248
- function mapSubPath(pkgJsonPath, subPath, key, value) {
259
+ function mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value }) {
249
260
  if (typeof value === 'string') {
250
261
  if (typeof key === 'string' && key.endsWith('*')) {
251
262
  // star match: "./foo/*": "./foo/*.js"
@@ -268,34 +279,57 @@ function mapSubPath(pkgJsonPath, subPath, key, value) {
268
279
  return value.find((v) => v.startsWith('./'));
269
280
  }
270
281
 
271
- throw pathNotFoundError(subPath, pkgJsonPath);
282
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
272
283
  }
273
284
 
274
- function findEntrypoint(pkgJsonPath, subPath, exportMap, conditions, key) {
285
+ function findEntrypoint({
286
+ importPath,
287
+ importer,
288
+ pkgJsonPath,
289
+ subPath,
290
+ exportMap,
291
+ conditions,
292
+ key
293
+ }) {
275
294
  if (typeof exportMap !== 'object') {
276
- return mapSubPath(pkgJsonPath, subPath, key, exportMap);
295
+ return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key, value: exportMap });
277
296
  }
278
297
 
279
298
  // iterate conditions recursively, find the first that matches all conditions
280
299
  for (const [condition, subExportMap] of Object.entries(exportMap)) {
281
300
  if (conditions.includes(condition)) {
282
- const mappedSubPath = findEntrypoint(pkgJsonPath, subPath, subExportMap, conditions, key);
301
+ const mappedSubPath = findEntrypoint({
302
+ importPath,
303
+ importer,
304
+ pkgJsonPath,
305
+ subPath,
306
+ exportMap: subExportMap,
307
+ conditions,
308
+ key
309
+ });
283
310
  if (mappedSubPath) {
284
311
  return mappedSubPath;
285
312
  }
286
313
  }
287
314
  }
288
- throw pathNotFoundError(subPath, pkgJsonPath);
315
+ throw pathNotFoundError(importer, subPath, pkgJsonPath);
289
316
  }
290
317
 
291
- function findEntrypointTopLevel(pkgJsonPath, subPath, exportMap, conditions) {
318
+ function findEntrypointTopLevel({
319
+ importPath,
320
+ importer,
321
+ pkgJsonPath,
322
+ subPath,
323
+ exportMap,
324
+ conditions
325
+ }) {
292
326
  if (typeof exportMap !== 'object') {
293
327
  // the export map shorthand, for example { exports: "./index.js" }
294
328
  if (subPath !== '.') {
295
329
  // shorthand only supports a main entrypoint
296
- throw pathNotFoundError(subPath, pkgJsonPath);
330
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
297
331
  }
298
- return mapSubPath(pkgJsonPath, subPath, null, exportMap);
332
+ return mapSubPath({ importPath, importer, pkgJsonPath, subPath, key: null, value: exportMap });
299
333
  }
300
334
 
301
335
  // export map is an object, the top level can be either conditions or sub path mappings
@@ -318,22 +352,31 @@ function findEntrypointTopLevel(pkgJsonPath, subPath, exportMap, conditions) {
318
352
  // top level is conditions, for example { "import": ..., "require": ..., "module": ... }
319
353
  if (subPath !== '.') {
320
354
  // package with top level conditions means it only supports a main entrypoint
321
- throw pathNotFoundError(subPath, pkgJsonPath);
355
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
322
356
  }
323
357
  exportMapForSubPath = exportMap;
324
358
  } else {
325
359
  // top level is sub path mappings, for example { ".": ..., "./foo": ..., "./bar": ... }
326
360
  key = findExportKeyMatch(exportMap, subPath);
327
361
  if (!key) {
328
- throw pathNotFoundError(subPath, pkgJsonPath);
362
+ throw pathNotFoundError(importPath, importer, subPath, pkgJsonPath);
329
363
  }
330
364
  exportMapForSubPath = exportMap[key];
331
365
  }
332
366
 
333
- return findEntrypoint(pkgJsonPath, subPath, exportMapForSubPath, conditions, key);
367
+ return findEntrypoint({
368
+ importPath,
369
+ importer,
370
+ pkgJsonPath,
371
+ subPath,
372
+ exportMap: exportMapForSubPath,
373
+ conditions,
374
+ key
375
+ });
334
376
  }
335
377
 
336
378
  async function resolveId({
379
+ importer,
337
380
  importPath,
338
381
  exportConditions,
339
382
  warn,
@@ -395,12 +438,14 @@ async function resolveId({
395
438
  try {
396
439
  const packageSubPath =
397
440
  pkgName === importPath ? '.' : `.${importPath.substring(pkgName.length)}`;
398
- const mappedSubPath = findEntrypointTopLevel(
441
+ const mappedSubPath = findEntrypointTopLevel({
442
+ importer,
443
+ importPath,
399
444
  pkgJsonPath,
400
- packageSubPath,
401
- pkgJson.exports,
402
- exportConditions
403
- );
445
+ subPath: packageSubPath,
446
+ exportMap: pkgJson.exports,
447
+ conditions: exportConditions
448
+ });
404
449
  const pkgDir = path.dirname(pkgJsonPath);
405
450
  location = path.join(pkgDir, mappedSubPath);
406
451
  } catch (error) {
@@ -439,6 +484,7 @@ async function resolveId({
439
484
  // Resolve module specifiers in order. Promise resolves to the first module that resolves
440
485
  // successfully, or the error that resulted from the last attempted module resolution.
441
486
  async function resolveImportSpecifiers({
487
+ importer,
442
488
  importSpecifierList,
443
489
  exportConditions,
444
490
  warn,
@@ -453,6 +499,7 @@ async function resolveImportSpecifiers({
453
499
  for (let i = 0; i < importSpecifierList.length; i++) {
454
500
  // eslint-disable-next-line no-await-in-loop
455
501
  const resolved = await resolveId({
502
+ importer,
456
503
  importPath: importSpecifierList[i],
457
504
  exportConditions,
458
505
  warn,
@@ -699,6 +746,7 @@ function nodeResolve(opts = {}) {
699
746
  const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
700
747
 
701
748
  const resolvedWithoutBuiltins = await resolveImportSpecifiers({
749
+ importer,
702
750
  importSpecifierList,
703
751
  exportConditions,
704
752
  warn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rollup/plugin-node-resolve",
3
- "version": "11.0.0",
3
+ "version": "11.0.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/types/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Plugin } from 'rollup';
2
+
1
3
  export const DEFAULTS: {
2
4
  customResolveOptions: {};
3
5
  dedupe: [];