Coder Social home page Coder Social logo

davecombs / fast-resolve-package Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stefanpenner/resolve-package-path

0.0 1.0 0.0 261 KB

a special purpose fast memoizing way to resolve a node modules package.json

License: MIT License

JavaScript 2.29% TypeScript 97.71%

fast-resolve-package's Introduction

resolve-package-path CI

This project is special-purpose, made to resolve package.json files for:

  • a given module name and basedir or
  • a given basedir

It cannot and does not resolve anything else.

To achieve its file-resolution performance, it does two specific things:

  • It memoizes results identically to node's require. Specifically, for a given moduleName and baseDir it will, for the duration of the process, always return the exact same response.

  • It re-implements the parts of require.resolve needed to resolve package.json files ONLY. This removes unneeded I/O. (based on @davecombs approach)

Usage

yarn add resolve-package-path
const resolvePackagePath = require('resolve-package-path');

resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from') // => /path/to/rsvp.json or null

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start') // => path/to/package.json or null

Advanced usage

Preserve Symlinks

Node supports --preserve-symlinks and NODE_PRESERVE_SYMLINKS=1 for compatibility this library respects these.

Disable default caching

Although by default resolve-package-path caches or memoizes results, this feature can be disabled:

const resolvePackagePath = require('resolve-package-path');

resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from', false) // => uncached result /path/to/rsvp.json or null

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', false) // => path/to/package.json or null

Purge the cache

const resolvePackagePath = require('resolve-package-path');
resolvePackagePath._resetCache();

Provide an alternative cache

In some advanced circumtances, you may want to gain access to the cache to share between more systems. In that case, a cache instance of the following form can be provided as a third argument:

cache = {
  RESOLVED_PACKAGE_PATH: new Map(),
  REAL_FILE_PATH: new Map(),
  REAL_DIRECTORY_PATH: new Map(),
};
findUpCache = new Map();

const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'path/to/start/from', cache);

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', findUpCache) // => path/to/package.json or null

Use internal helper functions

For consumers who also do getRealFilePath or getRealDirectoryPath calls on relevant paths, we expose them as utilities. These utilties ensure identical functionality to resolve-package-path, and a shared cache, which may help reduce IO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.