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

added caching, small update to README #7

Merged
merged 2 commits into from
Nov 26, 2021
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
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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": "0.4.1",
"version": "0.4.2",
"license": "MIT",
"main": "resolvewithplus.mjs",
"readmeFilename": "README.md",
Expand Down
17 changes: 13 additions & 4 deletions resolvewithplus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions resolvewithplus.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});