From dc0d73fc5cdec0b64855beff77094e0de2326b11 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 20 Oct 2023 10:33:20 -0700 Subject: [PATCH 1/4] return full path --- CHANGELOG.md | 2 ++ package.json | 2 +- resolvewithplus.js | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4795640..4aaeca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.1.3 _Oct.20.2023_ + * resolve full path from package.json "main", "browser" and "module" definitions. resolves [this issue at esmock.](https://github.com/iambumblehead/esmock/issues/260) * 2.1.2 _Oct.19.2023_ * [remove unused condition](https://github.com/iambumblehead/resolvewithplus/pull/63) and resolved error where fileurl path was not correctly resolved for package.json "main", "browser" and "module" definitions * 2.1.1 _Oct.19.2023_ diff --git a/package.json b/package.json index 0c3ae6b..329f65b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "2.1.2", + "version": "2.1.3", "description": "resolvewith with extra power", "readmeFilename": "README.md", "license": "ISC", diff --git a/resolvewithplus.js b/resolvewithplus.js index 86d5933..c62a0f5 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -338,9 +338,11 @@ const gettargetindextopmain = (main, opts = {}, dir = '') => { const gettargetindextop = (packagejson, opts = {}, dir = '', index = false) => { const packagejsontype = opts.packagejsontype - if (opts.isspectype !== false) + if (opts.isspectype !== false) { index = packagejson[packagejsontype] || packagejson[getspectypenamedexportdefault(packagejsontype)] + index = index && path.join(dir, index) + } // if priorty list includes 'import', return packagejson.module if (!index && (opts.priority || []).includes(spectypemoduleimport) From 9d45ec2a0fa5a4c6f807f62ee831c624611d4477 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 20 Oct 2023 10:43:20 -0700 Subject: [PATCH 2/4] use gettargetindextopmain function to resolve top level package.json main file --- resolvewithplus.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index c62a0f5..20b66e0 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -338,10 +338,14 @@ const gettargetindextopmain = (main, opts = {}, dir = '') => { const gettargetindextop = (packagejson, opts = {}, dir = '', index = false) => { const packagejsontype = opts.packagejsontype + // these 'top' level packagejson values allow commonjs resolution + // and commonjs resolver can resolve "./name/index.jx" from "./name" + // because of this, the directory is passed down and used to locate + // the literal path or any possible index-paths found in the dir if (opts.isspectype !== false) { index = packagejson[packagejsontype] || packagejson[getspectypenamedexportdefault(packagejsontype)] - index = index && path.join(dir, index) + index = index && gettargetindextopmain(index, opts, dir) } // if priorty list includes 'import', return packagejson.module From 7d56cc5729ebadd5a61534488d3ff89949994073 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 20 Oct 2023 11:02:33 -0700 Subject: [PATCH 3/4] added test --- resolvewithplus.js | 6 +++- tests/tests-basic/tests-basic.test.js | 42 +++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 20b66e0..f037486 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -336,7 +336,7 @@ const gettargetindextopmain = (main, opts = {}, dir = '') => { // > If both "exports" and "main" are defined, the "exports" field // > takes precedence over "main" in supported versions of Node.js. const gettargetindextop = (packagejson, opts = {}, dir = '', index = false) => { - const packagejsontype = opts.packagejsontype + const packagejsontype = getpackagejsontype(packagejson) // these 'top' level packagejson values allow commonjs resolution // and commonjs resolver can resolve "./name/index.jx" from "./name" @@ -573,3 +573,7 @@ export default Object.assign(resolvewith, { esmparse, cache }) + +export { + gettargetindextop +} diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index edee0a2..1465912 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -6,7 +6,9 @@ import url from 'url' import path from 'path' import test from 'node:test' import assert from 'node:assert/strict' -import resolvewithplus from '../../resolvewithplus.js' +import resolvewithplus, { + gettargetindextop +} from '../../resolvewithplus.js' const tofileurl = p => url.pathToFileURL(p).href const toresolvefileurl = p => tofileurl(path.resolve(p)) @@ -19,7 +21,7 @@ const resolvingpackagejsonmodulerelpath = '../../tests-basic/tests-basic.test.js' const resolvingpackagejsonmoduleurlpath = toresolvefileurl('./tests-basic.test.js') - +/* test('should return matched export paths', () => { const exports = { '.': './lib/index.test.js', @@ -642,3 +644,39 @@ test('should detect module type from package.json', () => { assert.strictEqual( resolvedmoduleexportsdef, resolvingpackagejsonmoduleurlpath) }) +*/ +test('gettargetindextop should resolve a fullpath', () => { + const dir = path.resolve('../node_modules/test/') + '/' + const indexpathrequire = gettargetindextop({ + name: '@adobe/fetch', + version: '4.1.0', + description: 'Light-weight Fetch ...', + require: resolvingpackagejsonmodulerelpath + }, {}, dir) + + assert.strictEqual( + indexpathrequire, + url.fileURLToPath(resolvingpackagejsonmoduleurlpath)) + + const indexpathmodule = gettargetindextop({ + name: '@adobe/fetch', + version: '4.1.0', + description: 'Light-weight Fetch ...', + module: resolvingpackagejsonmodulerelpath + }, { priority: [ 'import' ] }, dir) + + assert.strictEqual( + indexpathmodule, + url.fileURLToPath(resolvingpackagejsonmoduleurlpath)) + + const indexpathmain = gettargetindextop({ + name: '@adobe/fetch', + version: '4.1.0', + description: 'Light-weight Fetch ...', + main: resolvingpackagejsonmodulerelpath + }, {}, dir) + + assert.strictEqual( + indexpathmain, + url.fileURLToPath(resolvingpackagejsonmoduleurlpath)) +}) From 1e9212df560b189c59860a7a5349292afcd4ff90 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 20 Oct 2023 11:03:45 -0700 Subject: [PATCH 4/4] uncomment pre-existing tests --- tests/tests-basic/tests-basic.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 1465912..6d778b8 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -21,7 +21,7 @@ const resolvingpackagejsonmodulerelpath = '../../tests-basic/tests-basic.test.js' const resolvingpackagejsonmoduleurlpath = toresolvefileurl('./tests-basic.test.js') -/* + test('should return matched export paths', () => { const exports = { '.': './lib/index.test.js', @@ -644,7 +644,7 @@ test('should detect module type from package.json', () => { assert.strictEqual( resolvedmoduleexportsdef, resolvingpackagejsonmoduleurlpath) }) -*/ + test('gettargetindextop should resolve a fullpath', () => { const dir = path.resolve('../node_modules/test/') + '/' const indexpathrequire = gettargetindextop({