From bf144f647579d8903dcf8beffa177f8a784d7fff Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 22 Jan 2024 21:02:01 -0800 Subject: [PATCH 1/9] resolve named-properties that are also asterisk folder name --- resolvewithplus.js | 81 ++++++++++++++++--- .../nodejsexample_13_exports/package.json | 14 ++++ .../src/mystuff/index.js | 1 + .../types/mystuff/index.d.ts | 5 ++ tests/tests-basic/package.json | 1 + .../tests-basic/tests-export-patterns.test.js | 19 +++++ 6 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 tests/tests-basic/nodejsexample_13_exports/package.json create mode 100644 tests/tests-basic/nodejsexample_13_exports/src/mystuff/index.js create mode 100644 tests/tests-basic/nodejsexample_13_exports/types/mystuff/index.d.ts diff --git a/resolvewithplus.js b/resolvewithplus.js index f037486..82df9e2 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -183,23 +183,78 @@ const getesmkeyvalglobreplaced = (esmkey, esmval, pathlocal) => { // // All instances of * on the right hand side will then be replaced // with this value, including if it contains any / separators. -const getesmkeyvalmatch = (esmkey, esmval, path, keyvalmatch = false) => { - if (ispathesmmatch(esmkey, path)) { - if (esmval.includes('*')) { - if (ispathesmmatch(esmval, path)) { - keyvalmatch = path +const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { + if (ispathesmmatch(esmkey, idpath)) { + if (String(esmval).includes('*')) { + if (ispathesmmatch(esmval, idpath)) { + keyvalmx = idpath } else if (esmkey.includes('*') && esmkey !== esmval) { - keyvalmatch = getesmkeyvalglobreplaced(esmkey, esmval, path) + keyvalmx = getesmkeyvalglobreplaced(esmkey, esmval, idpath) } } else { - keyvalmatch = esmval + // if below condition is true, assume exports look this way and, + // * the namespace defined on the key is valid for the moduleId, + // * expanded key used as replacement for nested path value wildcard + // ``` + // "exports": { + // "./*": { + // "default": "./src/*/index.js", + // "types": "./types/*/index.d.ts" + // } + // } + // ``` + if (isobj(esmval) && esmkey.includes('*')) { + const expandedkey = getesmkeyvalglobreplaced( + esmkey, idpath, idpath) + const expandedkeyname = expandedkey && expandedkey + .split(/\.?\//).find(name => name) + + if (expandedkey) { + // fragile. expand spec value from expanded key path + // + // input ({ + // default: './src/*/index.js', + // types: './types/*/index.d.ts' + // }) + // + // output ({ + // default: './src/mystuff/index.js', + // types: './types/mystuff/index.d.ts' + // }) + const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { + exp[nestkey] = esmval[nestkey] + .split('*').join(expandedkeyname) + + return exp + }, {}) + + keyvalmx = Object.keys(expandedspec).reduce((resolved, nestkey) => { + if (resolved) return resolved + + const nestkeypathvalue = expandedspec[nestkey] + const nestkeypathvaluedir = nestkeypathvalue + .split(/\.?\//).find(name => name) + + // create a "fullpath" moduleId from expanded path dir + // ``` + // ('src', 'mystuff') => './src/mystuff') + const idpathexpanded = './' + path.join( + nestkeypathvaluedir, expandedkey) + + // eslint-disable-next-line no-use-before-define + return esmparse(expandedspec, idpathexpanded, opts) + }, null) + } + } + + keyvalmx = keyvalmx || esmval } } - return keyvalmatch + return keyvalmx } -const esmparselist = (list, spec, specifier, key = list[0]) => { +const esmparselist = (list, spec, specifier, opts, key = list[0]) => { if (!list.length) return null const isKeyValid = isESMImportSubpathRe.test(specifier) @@ -207,9 +262,9 @@ const esmparselist = (list, spec, specifier, key = list[0]) => { : isRelPathRe.test(key) return (isKeyValid - && typeof spec[key] === 'string' - && getesmkeyvalmatch(key, spec[key], specifier)) - || esmparselist(list.slice(1), spec, specifier) + && (typeof spec[key] === 'string' || isobj(spec[key])) + && getesmkeyvalmatch(key, spec[key], specifier, opts)) + || esmparselist(list.slice(1), spec, specifier, opts) } const esmparse = (spec, specifier, opts = {}) => { @@ -266,7 +321,7 @@ const esmparse = (spec, specifier, opts = {}) => { // "./lib/*": "./lib/*.js" // } if (!indexval) - indexval = esmparselist(Object.keys(spec), spec, specifier) + indexval = esmparselist(Object.keys(spec), spec, specifier, opts) // "exports": "./lib/index.js", // "exports": { "import": "./lib/index.js" }, diff --git a/tests/tests-basic/nodejsexample_13_exports/package.json b/tests/tests-basic/nodejsexample_13_exports/package.json new file mode 100644 index 0000000..367593b --- /dev/null +++ b/tests/tests-basic/nodejsexample_13_exports/package.json @@ -0,0 +1,14 @@ +{ + "name": "nodejsexample_13_exports", + "type": "module", + "repository": { + "type": "git", + "url": "git+https://github.com/iambumblehead/resolvewithplus.git" + }, + "exports": { + "./*": { + "default": "./src/*/index.js", + "types": "./types/*/index.d.ts" + } + } +} diff --git a/tests/tests-basic/nodejsexample_13_exports/src/mystuff/index.js b/tests/tests-basic/nodejsexample_13_exports/src/mystuff/index.js new file mode 100644 index 0000000..05d0c6b --- /dev/null +++ b/tests/tests-basic/nodejsexample_13_exports/src/mystuff/index.js @@ -0,0 +1 @@ +export default 'mystuff' diff --git a/tests/tests-basic/nodejsexample_13_exports/types/mystuff/index.d.ts b/tests/tests-basic/nodejsexample_13_exports/types/mystuff/index.d.ts new file mode 100644 index 0000000..ce1872e --- /dev/null +++ b/tests/tests-basic/nodejsexample_13_exports/types/mystuff/index.d.ts @@ -0,0 +1,5 @@ +declare const mystuff: string + +export { + mystuff as default +} diff --git a/tests/tests-basic/package.json b/tests/tests-basic/package.json index 31b5056..275873e 100644 --- a/tests/tests-basic/package.json +++ b/tests/tests-basic/package.json @@ -18,6 +18,7 @@ "nodejsexample_10_exports": "file:nodejsexample_10_exports", "nodejsexample_11_exports": "file:nodejsexample_11_exports", "nodejsexample_12_exports": "file:nodejsexample_12_exports", + "nodejsexample_13_exports": "file:nodejsexample_13_exports", "resolvewithplus": "file:.." }, "workspaces": [ diff --git a/tests/tests-basic/tests-export-patterns.test.js b/tests/tests-basic/tests-export-patterns.test.js index 2d828dc..e4656e2 100644 --- a/tests/tests-basic/tests-export-patterns.test.js +++ b/tests/tests-basic/tests-export-patterns.test.js @@ -275,6 +275,25 @@ test('should mock exports from nodejsexample_12_exports, nested cond', () => { noderesolvedfeaturenode) }) +// "asterisk-directory named-property exports" +// from: https://github.com/iambumblehead/esmock/issues/289 @dschnare +// { +// "exports": { +// "./*": { +// "default": "./src/*/index.js", +// "types": "./types/*/index.d.ts" +// } +// } +// } +test('should mock exports from nodejsexample_13_exports, asterisk dir', () => { + const noderesolvedindex = toresolvefileurl( + './nodejsexample_13_exports/src/mystuff/index.js') + + assert.strictEqual( + resolvewithplus('nodejsexample_13_exports/mystuff'), + noderesolvedindex) +}) + // "exports": './lib/index.js', // "exports": { "import": "./lib/index.js" }, // "exports": { ".": "./lib/index.js" }, From 1cf0693676fe249af9ebb7ed4c14853210784080 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 00:25:03 -0800 Subject: [PATCH 2/9] simplify loop --- resolvewithplus.js | 63 ++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 82df9e2..ade76af 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -204,47 +204,28 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // } // ``` if (isobj(esmval) && esmkey.includes('*')) { - const expandedkey = getesmkeyvalglobreplaced( - esmkey, idpath, idpath) - const expandedkeyname = expandedkey && expandedkey - .split(/\.?\//).find(name => name) - - if (expandedkey) { - // fragile. expand spec value from expanded key path - // - // input ({ - // default: './src/*/index.js', - // types: './types/*/index.d.ts' - // }) - // - // output ({ - // default: './src/mystuff/index.js', - // types: './types/mystuff/index.d.ts' - // }) - const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { - exp[nestkey] = esmval[nestkey] - .split('*').join(expandedkeyname) - - return exp - }, {}) - - keyvalmx = Object.keys(expandedspec).reduce((resolved, nestkey) => { - if (resolved) return resolved - - const nestkeypathvalue = expandedspec[nestkey] - const nestkeypathvaluedir = nestkeypathvalue - .split(/\.?\//).find(name => name) - - // create a "fullpath" moduleId from expanded path dir - // ``` - // ('src', 'mystuff') => './src/mystuff') - const idpathexpanded = './' + path.join( - nestkeypathvaluedir, expandedkey) - - // eslint-disable-next-line no-use-before-define - return esmparse(expandedspec, idpathexpanded, opts) - }, null) - } + const resolvedkey = getesmkeyvalglobreplaced(esmkey, idpath, idpath) + // './mystuff', './*' -> 'mystuff/*', + const expandedkey = path.join(resolvedkey, esmkey) + const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { + // ./src/*/index.js -> 'src' + const pathfirstdir = esmval[nestkey].split(/\.?\//).find(e => e) + + // eg, + // exp[nestkey] = getesmkeyvalglobreplaced( + // './mystuff/*.js', + // './src/mystuff/*.js', + // './mystuff/index.js') + exp[nestkey] = getesmkeyvalglobreplaced( + expandedkey, + path.join(pathfirstdir, expandedkey), + path.join(resolvedkey, esmval[nestkey].split('*')[1])) + + return exp + }, {}) + + // eslint-disable-next-line no-use-before-define + return esmparse(expandedspec, idpath, opts) } keyvalmx = keyvalmx || esmval From dceceeb63b968178a735499e324f5c9d4df05e91 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 00:37:59 -0800 Subject: [PATCH 3/9] add log for windows runtime --- resolvewithplus.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resolvewithplus.js b/resolvewithplus.js index ade76af..3fb049b 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -216,6 +216,11 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // './mystuff/*.js', // './src/mystuff/*.js', // './mystuff/index.js') + console.log([ + expandedkey, + path.join(pathfirstdir, expandedkey), + path.join(resolvedkey, esmval[nestkey].split('*')[1]) + ]) exp[nestkey] = getesmkeyvalglobreplaced( expandedkey, path.join(pathfirstdir, expandedkey), From 4fb7fe8436785ab026cb27260b409f6f6361b58d Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 00:46:49 -0800 Subject: [PATCH 4/9] add log for windows runtime --- resolvewithplus.js | 1 + 1 file changed, 1 insertion(+) diff --git a/resolvewithplus.js b/resolvewithplus.js index 3fb049b..ef0b7a7 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -204,6 +204,7 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // } // ``` if (isobj(esmval) && esmkey.includes('*')) { + console.log([ esmkey, idpath, idpath ]) const resolvedkey = getesmkeyvalglobreplaced(esmkey, idpath, idpath) // './mystuff', './*' -> 'mystuff/*', const expandedkey = path.join(resolvedkey, esmkey) From 702bc52cfe58db63cc5ccb0819c09fd748a205a3 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 00:53:09 -0800 Subject: [PATCH 5/9] remove console.log --- resolvewithplus.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index ef0b7a7..db51d82 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -204,10 +204,8 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // } // ``` if (isobj(esmval) && esmkey.includes('*')) { - console.log([ esmkey, idpath, idpath ]) - const resolvedkey = getesmkeyvalglobreplaced(esmkey, idpath, idpath) - // './mystuff', './*' -> 'mystuff/*', - const expandedkey = path.join(resolvedkey, esmkey) + const resolvedkey = idpath // 'mystuff' + const expandedkey = path.join(idpath, esmkey) // 'mystuff/*' const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { // ./src/*/index.js -> 'src' const pathfirstdir = esmval[nestkey].split(/\.?\//).find(e => e) @@ -217,11 +215,6 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // './mystuff/*.js', // './src/mystuff/*.js', // './mystuff/index.js') - console.log([ - expandedkey, - path.join(pathfirstdir, expandedkey), - path.join(resolvedkey, esmval[nestkey].split('*')[1]) - ]) exp[nestkey] = getesmkeyvalglobreplaced( expandedkey, path.join(pathfirstdir, expandedkey), From d1324f16bd276da1a87b6ae329a6388b49d41670 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 01:07:26 -0800 Subject: [PATCH 6/9] add log for windows runtime --- resolvewithplus.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resolvewithplus.js b/resolvewithplus.js index db51d82..d3ec580 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -215,6 +215,11 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // './mystuff/*.js', // './src/mystuff/*.js', // './mystuff/index.js') + console.log([ + expandedkey, + path.join(pathfirstdir, expandedkey), + path.join(resolvedkey, esmval[nestkey].split('*')[1]) + ]) exp[nestkey] = getesmkeyvalglobreplaced( expandedkey, path.join(pathfirstdir, expandedkey), From 459ed652ac427db7970eaa2c451e1af1b7461658 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 07:03:24 -0800 Subject: [PATCH 7/9] use path.posix.join to resolve windows runtime --- resolvewithplus.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index d3ec580..417b24f 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -205,25 +205,25 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { // ``` if (isobj(esmval) && esmkey.includes('*')) { const resolvedkey = idpath // 'mystuff' - const expandedkey = path.join(idpath, esmkey) // 'mystuff/*' + const expandedkey = path.posix.join(idpath, esmkey) // 'mystuff/*' const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { // ./src/*/index.js -> 'src' const pathfirstdir = esmval[nestkey].split(/\.?\//).find(e => e) // eg, // exp[nestkey] = getesmkeyvalglobreplaced( - // './mystuff/*.js', - // './src/mystuff/*.js', - // './mystuff/index.js') + // 'mystuff/*', + // 'src/mystuff/*', + // 'mystuff/index') console.log([ expandedkey, - path.join(pathfirstdir, expandedkey), - path.join(resolvedkey, esmval[nestkey].split('*')[1]) + path.posix.join(pathfirstdir, expandedkey), + path.posix.join(resolvedkey, esmval[nestkey].split('*')[1]) ]) exp[nestkey] = getesmkeyvalglobreplaced( expandedkey, - path.join(pathfirstdir, expandedkey), - path.join(resolvedkey, esmval[nestkey].split('*')[1])) + path.posix.join(pathfirstdir, expandedkey), + path.posix.join(resolvedkey, esmval[nestkey].split('*')[1])) return exp }, {}) From b2e201c11d429e63b7cb120dbe9efaac7df8e3ef Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 07:07:52 -0800 Subject: [PATCH 8/9] remove console.log --- resolvewithplus.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 417b24f..cd0505f 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -207,19 +207,13 @@ const getesmkeyvalmatch = (esmkey, esmval, idpath, opts, keyvalmx = false) => { const resolvedkey = idpath // 'mystuff' const expandedkey = path.posix.join(idpath, esmkey) // 'mystuff/*' const expandedspec = Object.keys(esmval).reduce((exp, nestkey) => { - // ./src/*/index.js -> 'src' - const pathfirstdir = esmval[nestkey].split(/\.?\//).find(e => e) + const pathfirstdir = esmval[nestkey] // ./src/a/b.js -> 'src' + .split(/\.?\//).find(e => e) // eg, - // exp[nestkey] = getesmkeyvalglobreplaced( - // 'mystuff/*', - // 'src/mystuff/*', - // 'mystuff/index') - console.log([ - expandedkey, - path.posix.join(pathfirstdir, expandedkey), - path.posix.join(resolvedkey, esmval[nestkey].split('*')[1]) - ]) + // getesmkeyvalglobreplaced( + // 'mystuff/*', 'src/mystuff/*', 'mystuff/index.js') + // 'src/mystuff/index.js' exp[nestkey] = getesmkeyvalglobreplaced( expandedkey, path.posix.join(pathfirstdir, expandedkey), From 749f131bcc320a24105e4487895cf03442bbbad0 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 23 Jan 2024 07:44:22 -0800 Subject: [PATCH 9/9] update changelog and increment version --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aaeca2..a24657a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.1.4 _Jan.01.2024_ + * [resolve nested exports defined on named-properties](https://github.com/iambumblehead/resolvewithplus/pull/65) with wildcards, eg `exports: { './*': { default: './src/*/index.js' } }` resolves [this issue at esmock](https://github.com/iambumblehead/esmock/issues/289) * 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_ diff --git a/package.json b/package.json index 329f65b..51261a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "2.1.3", + "version": "2.1.4", "description": "resolvewith with extra power", "readmeFilename": "README.md", "license": "ISC",