From e33a096ed9d03c6ce3f07498526dc2f075e65b3c Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 8 Jul 2020 22:08:18 +0800 Subject: [PATCH 1/6] esm: improve packageResolve --- lib/internal/modules/esm/resolve.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 987a139c6aae57..a836836329fb30 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -133,8 +133,9 @@ function getPackageScopeConfig(resolved, base) { const packageJSONPath = packageJSONUrl.pathname; if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) break; - const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base); - if (packageConfig.exists) return packageConfig; + const filePath = fileURLToPath(packageJSONUrl); + const packageConfig = getPackageConfig(filePath, base); + if (packageConfig.exists) return { path: filePath, packageConfig }; const lastPackageJSONUrl = packageJSONUrl; packageJSONUrl = new URL('../package.json', packageJSONUrl); @@ -150,8 +151,9 @@ function getPackageScopeConfig(resolved, base) { type: 'none', exports: undefined }; - packageJSONCache.set(fileURLToPath(packageJSONUrl), packageConfig); - return packageConfig; + const filePath = fileURLToPath(packageJSONUrl); + packageJSONCache.set(filePath, packageConfig); + return { path: filePath, packageConfig }; } /* @@ -488,7 +490,7 @@ function packageExportsResolve( } function getPackageType(url) { - const packageConfig = getPackageScopeConfig(url, url); + const { packageConfig } = getPackageScopeConfig(url, url); return packageConfig.type; } @@ -533,17 +535,9 @@ function packageResolve(specifier, base, conditions) { '' : '.' + StringPrototypeSlice(specifier, separatorIndex); // ResolveSelf - const packageConfig = getPackageScopeConfig(base, base); + const { path: _path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { - // TODO(jkrems): Find a way to forward the pair/iterator already generated - // while executing GetPackageScopeConfig - let packageJSONUrl; - for (const [ filename, packageConfigCandidate ] of packageJSONCache) { - if (packageConfig === packageConfigCandidate) { - packageJSONUrl = pathToFileURL(filename); - break; - } - } + const packageJSONUrl = _path; if (packageJSONUrl !== undefined && packageConfig.name === packageName && packageConfig.exports !== undefined) { From 1aeb4d1e2ace2bee167eb941cfe2c2d62b0c71e6 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 8 Jul 2020 22:12:24 +0800 Subject: [PATCH 2/6] fixup!: rename variable --- lib/internal/modules/esm/resolve.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a836836329fb30..19b713f0a44221 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -535,9 +535,9 @@ function packageResolve(specifier, base, conditions) { '' : '.' + StringPrototypeSlice(specifier, separatorIndex); // ResolveSelf - const { path: _path, packageConfig } = getPackageScopeConfig(base, base); + const { path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { - const packageJSONUrl = _path; + const packageJSONUrl = path; if (packageJSONUrl !== undefined && packageConfig.name === packageName && packageConfig.exports !== undefined) { From b0bc610deae70222cff84fc0b5674cd953c87b15 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 8 Jul 2020 22:16:19 +0800 Subject: [PATCH 3/6] fixup!: add a comment --- lib/internal/modules/esm/resolve.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 19b713f0a44221..70df2d12fa1362 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -153,7 +153,7 @@ function getPackageScopeConfig(resolved, base) { }; const filePath = fileURLToPath(packageJSONUrl); packageJSONCache.set(filePath, packageConfig); - return { path: filePath, packageConfig }; + return { packageConfig }; } /* @@ -537,6 +537,7 @@ function packageResolve(specifier, base, conditions) { // ResolveSelf const { path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { + // If path not exist, then packageConfig not exists too const packageJSONUrl = path; if (packageJSONUrl !== undefined && packageConfig.name === packageName && From 4e3df4fabdefce1d399c49daf32a7dc04fd771bc Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 8 Jul 2020 22:16:47 +0800 Subject: [PATCH 4/6] fixup!: fix a comment --- lib/internal/modules/esm/resolve.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 70df2d12fa1362..b077219886045f 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -537,7 +537,7 @@ function packageResolve(specifier, base, conditions) { // ResolveSelf const { path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { - // If path not exist, then packageConfig not exists too + // If path not exist, then packageConfig.exists is false too const packageJSONUrl = path; if (packageJSONUrl !== undefined && packageConfig.name === packageName && From 873917cd81d35571e8be7bf81dbdd898df08b423 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 8 Jul 2020 22:25:01 +0800 Subject: [PATCH 5/6] fixup!: remove unnecessary code --- lib/internal/modules/esm/resolve.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index b077219886045f..fef773af4bdb3a 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -151,8 +151,7 @@ function getPackageScopeConfig(resolved, base) { type: 'none', exports: undefined }; - const filePath = fileURLToPath(packageJSONUrl); - packageJSONCache.set(filePath, packageConfig); + packageJSONCache.set(fileURLToPath(packageJSONUrl), packageConfig); return { packageConfig }; } From 8449e6b6246ea6b189e74de49bc8a5ea7124b173 Mon Sep 17 00:00:00 2001 From: himself65 Date: Thu, 9 Jul 2020 09:53:14 +0800 Subject: [PATCH 6/6] fixup!: fix a bug --- lib/internal/modules/esm/resolve.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index fef773af4bdb3a..f93dd44e1f2147 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -537,7 +537,7 @@ function packageResolve(specifier, base, conditions) { const { path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { // If path not exist, then packageConfig.exists is false too - const packageJSONUrl = path; + const packageJSONUrl = pathToFileURL(path); if (packageJSONUrl !== undefined && packageConfig.name === packageName && packageConfig.exports !== undefined) {