Skip to content

Commit

Permalink
Merge pull request #64 from iambumblehead/resolve-full-path-from-targ…
Browse files Browse the repository at this point in the history
…et-index-top

return full path
  • Loading branch information
iambumblehead authored Oct 20, 2023
2 parents 0fa3bf3 + 1e9212d commit f22a56d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resolvewithplus",
"version": "2.1.2",
"version": "2.1.3",
"description": "resolvewith with extra power",
"readmeFilename": "README.md",
"license": "ISC",
Expand Down
14 changes: 12 additions & 2 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,17 @@ 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)

if (opts.isspectype !== false)
// 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 && gettargetindextopmain(index, opts, dir)
}

// if priorty list includes 'import', return packagejson.module
if (!index && (opts.priority || []).includes(spectypemoduleimport)
Expand Down Expand Up @@ -567,3 +573,7 @@ export default Object.assign(resolvewith, {
esmparse,
cache
})

export {
gettargetindextop
}
40 changes: 39 additions & 1 deletion tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
})

0 comments on commit f22a56d

Please sign in to comment.