From 9555024add77565a5e94068d6cf85f79a28186b0 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 19 Aug 2022 08:01:31 -0700 Subject: [PATCH 1/2] resolve pg main: ./lib definition --- CHANGELOG.md | 2 ++ package.json | 2 +- resolvewithplus.js | 3 ++- tests/package.json | 1 + tests/tests-basic/tests-basic.test.js | 12 ++++++++++-- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e1e8f0..66febda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 0.8.9 _Aug.19.2022_ + * resolve pg packages cjs ["main": "./lib"](https://github.com/iambumblehead/resolvewithplus/pull/32) * 0.8.8 _Aug.16.2022_ * support win32 [drive-style module-path](https://github.com/iambumblehead/resolvewithplus/pull/31) * 0.8.7 _Aug.15.2022_ diff --git a/package.json b/package.json index 44d757b..c29544e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "0.8.8", + "version": "0.8.9", "engines" : { "node" : ">=12.16.0" }, diff --git a/resolvewithplus.js b/resolvewithplus.js index 690a480..36878ba 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -183,7 +183,8 @@ export default (o => { let json = path.join(d, packagejson); if ((relpath = o.getpackagepath(json, opts))) { - filepath = o.getasfilesync(path.join(d, relpath)); + filepath = o.getasfilesync(path.join(d, relpath)) + || o.getasfilesync(path.join(d, path.join(relpath, 'index'))); } else { supportedExtensions.some(f => ( (f = path.join(d, `index${f}`)) && o.isfilesync(f) && (filepath = f))); diff --git a/tests/package.json b/tests/package.json index 7511937..0bdd276 100644 --- a/tests/package.json +++ b/tests/package.json @@ -9,6 +9,7 @@ "main": "package.json.resolvewithplus.export.js", "dependencies": { "npm-run-all": "^4.1.5", + "pg": "8.7.3", "got": "12.0.1", "eslint": "^8.0.1", "koa": "^2.13.4", diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 8e97fbc..ff878a3 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -2,7 +2,7 @@ // Timestamp: 2017.04.23-23:31:33 (last modified) // Author(s): bumblehead -import url, { fileURLToPath } from 'url'; +import url from 'url'; import os from 'os'; import path from 'path'; import test from 'node:test' @@ -18,7 +18,7 @@ test('should convert win32 path to node-friendly posix path', () => { }) test('should pass windows and posix system-specific module path', () => { - const modulePath = fileURLToPath( + const modulePath = url.fileURLToPath( new URL('../testfiles/testscript.js', import.meta.url)) const calleePath = import.meta.url; const returnPath = resolvewithplus(modulePath, calleePath) @@ -150,6 +150,14 @@ test('should handle package.json stringy "exports" field (got)', () => { path.resolve('../node_modules/got/dist/source/index.js')); }); +test('should handle package.json "main": "./lib" field (pg)', () => { + const fullpath = path.resolve('../testfiles/'); + + assert.strictEqual( + resolvewithplus('pg', fullpath), + path.resolve('../node_modules/pg/lib/index.js')); +}); + test('should return values from cache', () => { resolvewithplus.cache['filepathkey'] = 'filepathvalue'; From ab99e2dd2a1c094a22c43dfa0a24969a7fd17caf Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 19 Aug 2022 08:14:57 -0700 Subject: [PATCH 2/2] only check for index file when no export pattern is defined, package is cjs --- resolvewithplus.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 36878ba..1789321 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -143,10 +143,6 @@ export default (o => { return indexval; }; - o.getpackagepath = (jsonfile, opts) => ( - o.isfilesync(jsonfile) && (jsonfile = require(jsonfile)) && - (o.gettargetindex(jsonfile, opts) || jsonfile.main)); - // https://nodejs.org/api/modules.html#modules_module_require_id // // LOAD_AS_FILE(X) @@ -181,8 +177,10 @@ export default (o => { let filepath = null; let relpath; let json = path.join(d, packagejson); - - if ((relpath = o.getpackagepath(json, opts))) { + let jsonobj = o.isfilesync(json) && require(json); + if ((relpath = o.gettargetindex(jsonobj, opts))) { + filepath = o.getasfilesync(path.join(d, relpath)) + } else if ((relpath = jsonobj.main)) { filepath = o.getasfilesync(path.join(d, relpath)) || o.getasfilesync(path.join(d, path.join(relpath, 'index'))); } else {