Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exlicitly resolve browser and import priorities #55

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# changelog

* 2.0.6 _Oct.10.2023_
* [explicitly prioritize "browser"](https://github.com/iambumblehead/resolvewithplus/pull/54) then "import" then "default", when browser and import both true
* [update README image link](https://github.com/iambumblehead/resolvewithplus/pull/52) to use "main" repo path
* [replace reducer function](https://github.com/iambumblehead/resolvewithplus/pull/53) w/ simple recursion
* 2.0.5 _Sep.13.2023_
Expand Down
50 changes: 32 additions & 18 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const node_modules = 'node_modules'
const packagejson = 'package.json'
const specruntime = 'node'
const specdefault = 'default'
const specbrowser = 'browser'
const specimport = 'import'
const specdot = '.'
const isobj = o => o && typeof o === 'object'
Expand Down Expand Up @@ -195,7 +196,7 @@ const esmparselist = (list, spec, specifier, key = list[0]) => {
|| esmparselist(list.slice(1), spec, specifier)
}

const esmparse = (spec, specifier) => {
const esmparse = (spec, specifier, opts = {}) => {
let indexval = false

if (typeof spec === 'string')
Expand All @@ -209,7 +210,7 @@ const esmparse = (spec, specifier) => {
// }, "./index.cjs" ]
// }
indexval = spec
.reduce((p, elem) => p || esmparse(elem, specifier), null)
.reduce((p, elem) => p || esmparse(elem, specifier, opts), null)
}

if (!indexval && isobj(spec)) {
Expand All @@ -226,21 +227,23 @@ const esmparse = (spec, specifier) => {
// "require": "./feature-node.cjs"
// }
// }
if (!indexval && spec[specruntime])
indexval = esmparse(spec[specruntime], specifier)
if (!indexval && spec[specdefault])
indexval = esmparse(spec[specdefault], specifier)
if (!indexval)
indexval = (opts.specprioritylist || [ specruntime, specdefault ])
.reduce((prev, specname) => (
prev || esmparse(spec[specname], specifier, opts)
), false)

if (!indexval && spec[specifier])
indexval = esmparse(spec[specifier], specifier)
indexval = esmparse(spec[specifier], specifier, opts)

// "exports": "./lib/index.js",
// "exports": { "import": "./lib/index.js" },
// "exports": { ".": "./lib/index.js" },
// "exports": { ".": { "import": "./lib/index.js" } }
if (!indexval && spec[specdot])
indexval = typeof spec[specdot] === 'string'
? specifier === specimport && esmparse(spec[specdot], specifier)
: esmparse(spec[specdot], specifier)
? specifier === specimport && esmparse(spec[specdot], specifier, opts)
: esmparse(spec[specdot], specifier, opts)

// "exports": {
// ".": "./lib/index.test.js",
Expand All @@ -255,8 +258,8 @@ const esmparse = (spec, specifier) => {
}

const gettargetindex = (packagejson, opts) => {
let moduleobj = opts && opts.ismodule && packagejson.module,
browserobj = moduleobj || opts && opts.browser && packagejson.browser,
let moduleobj = opts && opts.isimport && packagejson.module,
browserobj = moduleobj || opts && opts.isbrowser && packagejson.browser,
esmexportsobj = packagejson.exports,
indexprop,
indexval
Expand All @@ -272,7 +275,7 @@ const gettargetindex = (packagejson, opts) => {
}

if (esmexportsobj) {
indexval = esmparse(esmexportsobj, specimport)
indexval = esmparse(esmexportsobj, specimport, opts)
}

return indexval
Expand Down Expand Up @@ -345,9 +348,9 @@ const getasfileordir = (moduleId, parent, opts) => {
// }
// }
// }
const esmparseimport = (targetpath, specifier, pjson) => {
const esmparseimport = (targetpath, specifier, pjson, opts) => {
const pjsonimports = pjson && pjson.imports
const firstmatch = esmparse(pjsonimports, specifier)
const firstmatch = esmparse(pjsonimports, specifier, opts)

return firstmatch && (
isRelPathRe.test(firstmatch)
Expand All @@ -373,10 +376,11 @@ const esmparseimport = (targetpath, specifier, pjson) => {
// 7. Let packageSubpath be "." concatenated with the substring of
// packageSpecifier from the position at the length of packageName.
// (removed steps 8-12 related to urls and error cases)
const esmparseexport = (targetpath, pname, pspecifier, pjson) => {
const esmparseexport = (targetpath, pname, pspecifier, pjson, opts) => {
const firstmatch = esmparse(
pjson && pjson.exports,
pspecifier ? './' + pspecifier : specimport)
pspecifier ? './' + pspecifier : specimport,
opts)

return firstmatch && path.join(targetpath, pname, firstmatch)
}
Expand Down Expand Up @@ -459,9 +463,19 @@ const begin = (moduleId, parent, opts) => {
}

const createopts = (moduleId, parent, opts) => {
const boolOr = (v, def) => typeof v === 'boolean' ? v : def

opts = opts || {}
opts.isTypescript = typeof opts.isTypescript === 'boolean'
? opts.isTypescript : isTsExtnRe.test(parent)
opts.isTypescript = boolOr(opts.isTypescript, isTsExtnRe.test(parent))
opts.isbrowser = boolOr(opts.isbrowser, false)
opts.isimport = boolOr(opts.isimport, true)

opts.specprioritylist = []

if (opts.isbrowser) opts.specprioritylist.push(specbrowser)
if (opts.isimport) opts.specprioritylist.push(specimport)
opts.specprioritylist.push(specruntime)
opts.specprioritylist.push(specdefault)

return opts
}
Expand Down
76 changes: 76 additions & 0 deletions tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,79 @@ test('should handle mixed exports', () => {
}
}), './index.mjs')
})

test('should return esm by default', () => {
// used by '[email protected]'
assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
main: './index.js',
module: './index.esm.js'
}, { isimport: true }), './index.esm.js')

// used by '@apollo/[email protected]'
assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
exports: {
'.': {
import: './dist/esm/index.js',
require: './dist/cjs/index.js'
}
}
}), './dist/esm/index.js')

// similar patter used by '[email protected]'
assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
exports: {
'.': {
deno: './server.deno.js',
worker: './server.worker.js',
browser: './server.browser.js',
import: './server.import.js',
default: './server.default.js'
}
}
}), './server.import.js')

assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
exports: {
'.': {
deno: './server.deno.js',
worker: './server.worker.js',
browser: './server.browser.js',
default: './server.node.default.js'
}
}
}), './server.node.default.js')
})

test('should return browser over import when both true', () => {
assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
exports: {
'.': {
deno: './server.deno.js',
worker: './server.worker.js',
browser: './server.browser.js',
default: './server.default.js'
}
}
}, {
specprioritylist: [ 'import', 'browser', 'default' ]
}), './server.browser.js')

assert.strictEqual(resolvewithplus.gettargetindex({
name: 'test',
exports: {
'.': {
deno: './server.deno.js',
worker: './server.worker.js',
browser: './server.browser.js',
default: './server.default.js'
}
}
}, {
specprioritylist: [ 'default' ]
}), './server.default.js')
})
Loading