| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 76abcf3 commit 6d8eb6a
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -457,7 +457,7 @@ function trySelf(parentPath, request) { | |||
| 457 | 457 | try { | |
| 458 | 458 | return finalizeEsmResolution(packageExportsResolve( | |
| 459 | 459 | pathToFileURL(pkgPath + '/package.json'), expansion, pkg, | |
| 460 | - pathToFileURL(parentPath), cjsConditions), parentPath, pkgPath); | ||
| 460 | + pathToFileURL(parentPath), cjsConditions).resolved, parentPath, pkgPath); | ||
| 461 | 461 | } catch (e) { | |
| 462 | 462 | if (e.code === 'ERR_MODULE_NOT_FOUND') | |
| 463 | 463 | throw createEsmNotFoundErr(request, pkgPath + '/package.json'); | |
@@ -481,7 +481,7 @@ function resolveExports(nmPath, request) { | |||
| 481 | 481 | try { | |
| 482 | 482 | return finalizeEsmResolution(packageExportsResolve( | |
| 483 | 483 | pathToFileURL(pkgPath + '/package.json'), '.' + expansion, pkg, null, | |
| 484 | - cjsConditions), null, pkgPath); | ||
| 484 | + cjsConditions).resolved, null, pkgPath); | ||
| 485 | 485 | } catch (e) { | |
| 486 | 486 | if (e.code === 'ERR_MODULE_NOT_FOUND') | |
| 487 | 487 | throw createEsmNotFoundErr(request, pkgPath + '/package.json'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -463,6 +463,23 @@ const patternRegEx = /\*/g; | |||
| 463 | 463 | ||
| 464 | 464 | function resolvePackageTargetString( | |
| 465 | 465 | target, subpath, match, packageJSONUrl, base, pattern, internal, conditions) { | |
| 466 | + | ||
| 467 | + const composeResult = (resolved) => { | ||
| 468 | + let format; | ||
| 469 | + try { | ||
| 470 | + format = getPackageType(resolved); | ||
| 471 | + } catch (err) { | ||
| 472 | + if (err.code === 'ERR_INVALID_FILE_URL_PATH') { | ||
| 473 | + const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER( | ||
| 474 | + resolved, 'must not include encoded "/" or "\\" characters', base); | ||
| 475 | + invalidModuleErr.cause = err; | ||
| 476 | + throw invalidModuleErr; | ||
| 477 | + } | ||
| 478 | + throw err; | ||
| 479 | + } | ||
| 480 | + return { resolved, ...(format !== 'none') && { format } }; | ||
| 481 | + }; | ||
| 482 | + | ||
| 466 | 483 | if (subpath !== '' && !pattern && target[target.length - 1] !== '/') | |
| 467 | 484 | throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); | |
| 468 | 485 | ||
@@ -478,7 +495,8 @@ function resolvePackageTargetString( | |||
| 478 | 495 | const exportTarget = pattern ? | |
| 479 | 496 | RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) : | |
| 480 | 497 | target + subpath; | |
| 481 | - return packageResolve(exportTarget, packageJSONUrl, conditions); | ||
| 498 | + return packageResolve( | ||
| 499 | + exportTarget, packageJSONUrl, conditions); | ||
| 482 | 500 | } | |
| 483 | 501 | } | |
| 484 | 502 | throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); | |
@@ -494,18 +512,21 @@ function resolvePackageTargetString( | |||
| 494 | 512 | if (!StringPrototypeStartsWith(resolvedPath, packagePath)) | |
| 495 | 513 | throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); | |
| 496 | 514 | ||
| 497 | - if (subpath === '') return resolved; | ||
| 515 | + if (subpath === '') return composeResult(resolved); | ||
| 498 | 516 | ||
| 499 | 517 | if (RegExpPrototypeTest(invalidSegmentRegEx, subpath)) { | |
| 500 | 518 | const request = pattern ? | |
| 501 | 519 | StringPrototypeReplace(match, '*', () => subpath) : match + subpath; | |
| 502 | 520 | throwInvalidSubpath(request, packageJSONUrl, internal, base); | |
| 503 | 521 | } | |
| 504 | 522 | ||
| 505 | - if (pattern) | ||
| 506 | - return new URL(RegExpPrototypeSymbolReplace(patternRegEx, resolved.href, | ||
| 507 | - () => subpath)); | ||
| 508 | - return new URL(subpath, resolved); | ||
| 523 | + if (pattern) { | ||
| 524 | + return composeResult(new URL(RegExpPrototypeSymbolReplace(patternRegEx, | ||
| 525 | + resolved.href, | ||
| 526 | + () => subpath))); | ||
| 527 | + } | ||
| 528 | + | ||
| 529 | + return composeResult(new URL(subpath, resolved)); | ||
| 509 | 530 | } | |
| 510 | 531 | ||
| 511 | 532 | /** | |
@@ -531,9 +552,9 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, | |||
| 531 | 552 | let lastException; | |
| 532 | 553 | for (let i = 0; i < target.length; i++) { | |
| 533 | 554 | const targetItem = target[i]; | |
| 534 | - let resolved; | ||
| 555 | + let resolveResult; | ||
| 535 | 556 | try { | |
| 536 | - resolved = resolvePackageTarget( | ||
| 557 | + resolveResult = resolvePackageTarget( | ||
| 537 | 558 | packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern, | |
| 538 | 559 | internal, conditions); | |
| 539 | 560 | } catch (e) { | |
@@ -542,13 +563,13 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, | |||
| 542 | 563 | continue; | |
| 543 | 564 | throw e; | |
| 544 | 565 | } | |
| 545 | - if (resolved === undefined) | ||
| 566 | + if (resolveResult === undefined) | ||
| 546 | 567 | continue; | |
| 547 | - if (resolved === null) { | ||
| 568 | + if (resolveResult === null) { | ||
| 548 | 569 | lastException = null; | |
| 549 | 570 | continue; | |
| 550 | 571 | } | |
| 551 | - return resolved; | ||
| 572 | + return resolveResult; | ||
| 552 | 573 | } | |
| 553 | 574 | if (lastException === undefined || lastException === null) | |
| 554 | 575 | return lastException; | |
@@ -567,12 +588,12 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, | |||
| 567 | 588 | const key = keys[i]; | |
| 568 | 589 | if (key === 'default' || conditions.has(key)) { | |
| 569 | 590 | const conditionalTarget = target[key]; | |
| 570 | - const resolved = resolvePackageTarget( | ||
| 591 | + const resolveResult = resolvePackageTarget( | ||
| 571 | 592 | packageJSONUrl, conditionalTarget, subpath, packageSubpath, base, | |
| 572 | 593 | pattern, internal, conditions); | |
| 573 | - if (resolved === undefined) | ||
| 594 | + if (resolveResult === undefined) | ||
| 574 | 595 | continue; | |
| 575 | - return resolved; | ||
| 596 | + return resolveResult; | ||
| 576 | 597 | } | |
| 577 | 598 | } | |
| 578 | 599 | return undefined; | |
@@ -631,12 +652,15 @@ function packageExportsResolve( | |||
| 631 | 652 | !StringPrototypeIncludes(packageSubpath, '*') && | |
| 632 | 653 | !StringPrototypeEndsWith(packageSubpath, '/')) { | |
| 633 | 654 | const target = exports[packageSubpath]; | |
| 634 | - const resolved = resolvePackageTarget( | ||
| 655 | + const resolveResult = resolvePackageTarget( | ||
| 635 | 656 | packageJSONUrl, target, '', packageSubpath, base, false, false, conditions | |
| 636 | 657 | ); | |
| 637 | - if (resolved === null || resolved === undefined) | ||
| 658 | + | ||
| 659 | + if (resolveResult == null) { | ||
| 638 | 660 | throwExportsNotFound(packageSubpath, packageJSONUrl, base); | |
| 639 | - return resolved; | ||
| 661 | + } | ||
| 662 | + | ||
| 663 | + return resolveResult; | ||
| 640 | 664 | } | |
| 641 | 665 | ||
| 642 | 666 | let bestMatch = ''; | |
@@ -672,12 +696,20 @@ function packageExportsResolve( | |||
| 672 | 696 | ||
| 673 | 697 | if (bestMatch) { | |
| 674 | 698 | const target = exports[bestMatch]; | |
| 675 | - const resolved = resolvePackageTarget(packageJSONUrl, target, | ||
| 676 | - bestMatchSubpath, bestMatch, base, | ||
| 677 | - true, false, conditions); | ||
| 678 | - if (resolved === null || resolved === undefined) | ||
| 699 | + const resolveResult = resolvePackageTarget( | ||
| 700 | + packageJSONUrl, | ||
| 701 | + target, | ||
| 702 | + bestMatchSubpath, | ||
| 703 | + bestMatch, | ||
| 704 | + base, | ||
| 705 | + true, | ||
| 706 | + false, | ||
| 707 | + conditions); | ||
| 708 | + | ||
| 709 | + if (resolveResult == null) { | ||
| 679 | 710 | throwExportsNotFound(packageSubpath, packageJSONUrl, base); | |
| 680 | - return resolved; | ||
| 711 | + } | ||
| 712 | + return resolveResult; | ||
| 681 | 713 | } | |
| 682 | 714 | ||
| 683 | 715 | throwExportsNotFound(packageSubpath, packageJSONUrl, base); | |
@@ -717,11 +749,12 @@ function packageImportsResolve(name, base, conditions) { | |||
| 717 | 749 | if (imports) { | |
| 718 | 750 | if (ObjectPrototypeHasOwnProperty(imports, name) && | |
| 719 | 751 | !StringPrototypeIncludes(name, '*')) { | |
| 720 | - const resolved = resolvePackageTarget( | ||
| 752 | + const resolveResult = resolvePackageTarget( | ||
| 721 | 753 | packageJSONUrl, imports[name], '', name, base, false, true, conditions | |
| 722 | 754 | ); | |
| 723 | - if (resolved !== null && resolved !== undefined) | ||
| 724 | - return resolved; | ||
| 755 | + if (resolveResult != null) { | ||
| 756 | + return resolveResult.resolved; | ||
| 757 | + } | ||
| 725 | 758 | } else { | |
| 726 | 759 | let bestMatch = ''; | |
| 727 | 760 | let bestMatchSubpath; | |
@@ -747,11 +780,13 @@ function packageImportsResolve(name, base, conditions) { | |||
| 747 | 780 | ||
| 748 | 781 | if (bestMatch) { | |
| 749 | 782 | const target = imports[bestMatch]; | |
| 750 | - const resolved = resolvePackageTarget(packageJSONUrl, target, | ||
| 751 | - bestMatchSubpath, bestMatch, | ||
| 752 | - base, true, true, conditions); | ||
| 753 | - if (resolved !== null && resolved !== undefined) | ||
| 754 | - return resolved; | ||
| 783 | + const resolveResult = resolvePackageTarget(packageJSONUrl, target, | ||
| 784 | + bestMatchSubpath, | ||
| 785 | + bestMatch, base, true, | ||
| 786 | + true, conditions); | ||
| 787 | + if (resolveResult != null) { | ||
| 788 | + return resolveResult.resolved; | ||
| 789 | + } | ||
| 755 | 790 | } | |
| 756 | 791 | } | |
| 757 | 792 | } | |
@@ -810,11 +845,11 @@ function parsePackageName(specifier, base) { | |||
| 810 | 845 | * @param {string} specifier | |
| 811 | 846 | * @param {string | URL | undefined} base | |
| 812 | 847 | * @param {Set<string>} conditions | |
| 813 | - * @returns {URL} | ||
| 848 | + * @returns {resolved: URL, format? : string} | ||
| 814 | 849 | */ | |
| 815 | 850 | function packageResolve(specifier, base, conditions) { | |
| 816 | 851 | if (NativeModule.canBeRequiredByUsers(specifier)) | |
| 817 | - return new URL('node:' + specifier); | ||
| 852 | + return { resolved: new URL('node:' + specifier) }; | ||
| 818 | 853 | ||
| 819 | 854 | const { packageName, packageSubpath, isScoped } = | |
| 820 | 855 | parsePackageName(specifier, base); | |
@@ -826,8 +861,7 @@ function packageResolve(specifier, base, conditions) { | |||
| 826 | 861 | if (packageConfig.name === packageName && | |
| 827 | 862 | packageConfig.exports !== undefined && packageConfig.exports !== null) { | |
| 828 | 863 | return packageExportsResolve( | |
| 829 | - packageJSONUrl, packageSubpath, packageConfig, base, conditions | ||
| 830 | - ); | ||
| 864 | + packageJSONUrl, packageSubpath, packageConfig, base, conditions); | ||
| 831 | 865 | } | |
| 832 | 866 | } | |
| 833 | 867 | ||
@@ -849,13 +883,24 @@ function packageResolve(specifier, base, conditions) { | |||
| 849 | 883 | ||
| 850 | 884 | // Package match. | |
| 851 | 885 | const packageConfig = getPackageConfig(packageJSONPath, specifier, base); | |
| 852 | - if (packageConfig.exports !== undefined && packageConfig.exports !== null) | ||
| 886 | + if (packageConfig.exports !== undefined && packageConfig.exports !== null) { | ||
| 853 | 887 | return packageExportsResolve( | |
| 854 | - packageJSONUrl, packageSubpath, packageConfig, base, conditions | ||
| 855 | - ); | ||
| 856 | - if (packageSubpath === '.') | ||
| 857 | - return legacyMainResolve(packageJSONUrl, packageConfig, base); | ||
| 858 | - return new URL(packageSubpath, packageJSONUrl); | ||
| 888 | + packageJSONUrl, packageSubpath, packageConfig, base, conditions); | ||
| 889 | + } | ||
| 890 | + if (packageSubpath === '.') { | ||
| 891 | + return { | ||
| 892 | + resolved: legacyMainResolve( | ||
| 893 | + packageJSONUrl, | ||
| 894 | + packageConfig, | ||
| 895 | + base), | ||
| 896 | + ...(packageConfig.type !== 'none') && { format: packageConfig.type } | ||
| 897 | + }; | ||
| 898 | + } | ||
| 899 | + | ||
| 900 | + return { | ||
| 901 | + resolved: new URL(packageSubpath, packageJSONUrl), | ||
| 902 | + ...(packageConfig.type !== 'none') && { format: packageConfig.type } | ||
| 903 | + }; | ||
| 859 | 904 | // Cross-platform root check. | |
| 860 | 905 | } while (packageJSONPath.length !== lastPath.length); | |
| 861 | 906 | ||
@@ -893,12 +938,13 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |||
| 893 | 938 | * @param {string | URL | undefined} base | |
| 894 | 939 | * @param {Set<string>} conditions | |
| 895 | 940 | * @param {boolean} preserveSymlinks | |
| 896 | - * @returns {URL} | ||
| 941 | + * @returns {url: URL, format?: string} | ||
| 897 | 942 | */ | |
| 898 | 943 | function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |
| 899 | 944 | // Order swapped from spec for minor perf gain. | |
| 900 | 945 | // Ok since relative URLs cannot parse as URLs. | |
| 901 | 946 | let resolved; | |
| 947 | + let format; | ||
| 902 | 948 | if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | |
| 903 | 949 | resolved = new URL(specifier, base); | |
| 904 | 950 | } else if (specifier[0] === '#') { | |
@@ -907,12 +953,19 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |||
| 907 | 953 | try { | |
| 908 | 954 | resolved = new URL(specifier); | |
| 909 | 955 | } catch { | |
| 910 | - resolved = packageResolve(specifier, base, conditions); | ||
| 956 | + ({ resolved, format } = packageResolve(specifier, base, conditions)); | ||
| 911 | 957 | } | |
| 912 | 958 | } | |
| 913 | - if (resolved.protocol !== 'file:') | ||
| 914 | - return resolved; | ||
| 915 | - return finalizeResolution(resolved, base, preserveSymlinks); | ||
| 959 | + if (resolved.protocol !== 'file:') { | ||
| 960 | + return { | ||
| 961 | + url: resolved | ||
| 962 | + }; | ||
| 963 | + } | ||
| 964 | + | ||
| 965 | + return { | ||
| 966 | + url: finalizeResolution(resolved, base, preserveSymlinks), | ||
| 967 | + ...(format != null) && { format } | ||
| 968 | + }; | ||
| 916 | 969 | } | |
| 917 | 970 | ||
| 918 | 971 | /** | |
@@ -1001,9 +1054,15 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) { | |||
| 1001 | 1054 | ||
| 1002 | 1055 | conditions = getConditionsSet(conditions); | |
| 1003 | 1056 | let url; | |
| 1057 | + let format; | ||
| 1004 | 1058 | try { | |
| 1005 | - url = moduleResolve(specifier, parentURL, conditions, | ||
| 1006 | - isMain ? preserveSymlinksMain : preserveSymlinks); | ||
| 1059 | + ({ url, format } = | ||
| 1060 | + moduleResolve( | ||
| 1061 | + specifier, | ||
| 1062 | + parentURL, | ||
| 1063 | + conditions, | ||
| 1064 | + isMain ? preserveSymlinksMain : preserveSymlinks | ||
| 1065 | + )); | ||
| 1007 | 1066 | } catch (error) { | |
| 1008 | 1067 | // Try to give the user a hint of what would have been the | |
| 1009 | 1068 | // resolved CommonJS module | |
@@ -1031,7 +1090,10 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) { | |||
| 1031 | 1090 | url.protocol !== 'node:') | |
| 1032 | 1091 | throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url); | |
| 1033 | 1092 | ||
| 1034 | - return { url: `${url}` }; | ||
| 1093 | + return { | ||
| 1094 | + url: `${url}`, | ||
| 1095 | + ...(format != null) && { format } | ||
| 1096 | + }; | ||
| 1035 | 1097 | } | |
| 1036 | 1098 | ||
| 1037 | 1099 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + // Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs | ||
| 2 | + import { allowGlobals } from '../common/index.mjs'; | ||
| 3 | + import * as fixtures from '../common/fixtures.mjs'; | ||
| 4 | + import { strict as assert } from 'assert'; | ||
| 5 | + import * as fs from 'fs'; | ||
| 6 | + | ||
| 7 | + allowGlobals(global.getModuleTypeStats); | ||
| 8 | + | ||
| 9 | + const basePath = | ||
| 10 | + new URL('./node_modules/', import.meta.url); | ||
| 11 | + | ||
| 12 | + const rel = (file) => new URL(file, basePath); | ||
| 13 | + const createDir = (path) => { | ||
| 14 | + if (!fs.existsSync(path)) { | ||
| 15 | + fs.mkdirSync(path); | ||
| 16 | + } | ||
| 17 | + }; | ||
| 18 | + | ||
| 19 | + const moduleName = 'module-counter-by-type'; | ||
| 20 | + | ||
| 21 | + const moduleDir = rel(`${moduleName}`); | ||
| 22 | + createDir(basePath); | ||
| 23 | + createDir(moduleDir); | ||
| 24 | + fs.cpSync( | ||
| 25 | + fixtures.path('es-modules', moduleName), | ||
| 26 | + moduleDir, | ||
| 27 | + { recursive: true } | ||
| 28 | + ); | ||
| 29 | + | ||
| 30 | + const { importedESM: importedESMBefore, | ||
| 31 | + importedCJS: importedCJSBefore } = global.getModuleTypeStats(); | ||
| 32 | + | ||
| 33 | + import(`${moduleName}`).finally(() => { | ||
| 34 | + fs.rmSync(basePath, { recursive: true, force: true }); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + const { importedESM: importedESMAfter, | ||
| 38 | + importedCJS: importedCJSAfter } = global.getModuleTypeStats(); | ||
| 39 | + | ||
| 40 | + assert.strictEqual(importedESMBefore + 1, importedESMAfter); | ||
| 41 | + assert.strictEqual(importedCJSBefore, importedCJSAfter); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments