From f629810a11444631bde233734e0748886ab0407d Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 25 Nov 2021 22:02:04 -0800 Subject: [PATCH 1/2] added caching, small update to README --- README.md | 19 +++++++++++-------- package.json | 2 +- resolvewithplus.mjs | 17 +++++++++++++---- resolvewithplus.spec.mjs | 6 ++++++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b091924..5ede0b1 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,27 @@ resolvewithplus =============== [![npm version](https://badge.fury.io/js/resolvewithplus.svg)](https://badge.fury.io/js/resolvewithplus) [![Build Status](https://github.com/iambumblehead/resolvewithplus/workflows/nodejs-ci/badge.svg)][2] -_resolvewithplus_ is an iteration of the _resolvewith_ package, which resolves CJS modules following [the original node.js spec.][2] _resolvewithplus_ is changed to an ESM module and adds support for ESM-style `import 'name'` resolutions. When packages export both ESM and CJS, `resolvewithplus` will return the ESM path. Resolving ESM paths is complex and ESM support will be lacking for edge-cases. +_resolvewithplus_ iterates on the _resolvewith_ package for resolving CJS modules following [the original node.js spec.][2] _resolvewithplus_ is changed to an ESM module and adds support for ESM `import 'name'` resolutions. -```javascript -// CJS -resolvewithplus('./testfiles/testscript.js', '/Users/bumble/resolvewith/test/') -// '/Users/bumble/resolvewith/test/testfiles/testscript.js' -resolvewithplus('testmodule', '/Users/bumble/resolvewith/test/') -// '/Users/bumble/resolvewith/node_modules/testmodule/index.js' +When a package exports both ESM and CJS, `resolvewithplus` returns the ESM path (tries to). Resolving ESM paths is complex and ESM support will be lacking for edge-cases. -// ESM paths are returned by default and not CJS paths +```javascript +// ESM paths are returned by default rather than CJS paths resolvewithplus('koa', '/Users/bumble/resolvewith/test/'); // '/Users/bumble/resolvewith/node_modules/koa/dist/koa.mjs' +// A CJS path is returned when the ESM path is not found +resolvewithplus('./testfiles/testscript.js', '/Users/bumble/resolvewith/test/') +// '/Users/bumble/resolvewith/test/testfiles/testscript.js' + // use the older 'resolvewith' package to resolve CJS paths resolvewith('koa', '/Users/bumble/resolvewith/test/'); // '/Users/bumble/resolvewith/node_modules/koa/lib/application.js' ``` +_resolvewithplus_ caches and reuse results it generates. The first call follows rules to locate the file. Subsequent calls using the values return reults from a key store. + + [0]: http://www.bumblehead.com "bumblehead" [1]: https://github.com/iambumblehead/resolvewith/blob/master/src/resolvewith.js [2]: https://nodejs.org/api/modules.html#modules_module_require_id diff --git a/package.json b/package.json index a0f76f6..f9dea7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "main": "resolvewithplus.mjs", "readmeFilename": "README.md", diff --git a/resolvewithplus.mjs b/resolvewithplus.mjs index 64edc20..afecb4e 100644 --- a/resolvewithplus.mjs +++ b/resolvewithplus.mjs @@ -5,8 +5,16 @@ import module from 'module'; const require = module.createRequire(import.meta.url); export default (o => { - o = (requirepath, withpath, opts) => - o.begin(requirepath, withpath, opts || {}); + o = (requirepath, withpath, opts) => { + let resolvedpath = o.cache[requirepath+withpath]; + if (resolvedpath) return resolvedpath; + + resolvedpath = o.begin(requirepath, withpath, opts || {}); + + return o.cache[requirepath+withpath] = resolvedpath; + }; + + o.cache = {}; // https://nodejs.org/api/modules.html#modules_module_require_id // @@ -45,8 +53,9 @@ export default (o => { o.iscoremodule = p => { try { - return p === require.resolve(p) && - !(p.includes(path.sep) && path.existsSync(p)); + return !/^[/.]/.test(p) + && p === require.resolve(p) + && !(p.includes(path.sep) && path.existsSync(p)); } catch (e) { return false; } diff --git a/resolvewithplus.spec.mjs b/resolvewithplus.spec.mjs index c5fed58..ffc6b6e 100644 --- a/resolvewithplus.spec.mjs +++ b/resolvewithplus.spec.mjs @@ -82,3 +82,9 @@ test('should handle package.json "exports" field', t => { resolvewithplus('koa', fullpath, { esm : true }), path.resolve('./node_modules/koa/dist/koa.mjs')); }); + +test('should return values from cache', t => { + resolvewithplus.cache['filepathkey'] = 'filepathvalue'; + + t.is(resolvewithplus('filepath', 'key'), 'filepathvalue' ); +}); From a467ea9b42d8a08e03a21cc86e471e36b54e43d1 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 25 Nov 2021 22:06:01 -0800 Subject: [PATCH 2/2] small change to pass eslint --- resolvewithplus.spec.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolvewithplus.spec.mjs b/resolvewithplus.spec.mjs index ffc6b6e..d489a7c 100644 --- a/resolvewithplus.spec.mjs +++ b/resolvewithplus.spec.mjs @@ -86,5 +86,5 @@ test('should handle package.json "exports" field', t => { test('should return values from cache', t => { resolvewithplus.cache['filepathkey'] = 'filepathvalue'; - t.is(resolvewithplus('filepath', 'key'), 'filepathvalue' ); + t.is(resolvewithplus('filepath', 'key'), 'filepathvalue'); });