resolver-cache

Unsafe SQLITE3 paths and file contents cache for JS

this package has been deprecated
Last updated 5 years ago by silviu.marian .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install resolver-cache 
SYNC missed versions from official npm registry.

Resolver Cache

This is an unsafe Resolver Cache.

It intercepts require() calls and caches:

  • file paths
  • file contents
  • (optional) transpiled file contents

Is unsafe is because there is this edge case:

  • src/file.js requires some-module via require() call
  • module resolves to node_modules/some-module
  • node_modules/some-module is moved or removed
  • module now resolves to ../node_modules/some-module

It's still very useful as it decreases require() time. The highest recorded local net decrease was as high as 42.44%. However this is not a guarantee, and you should always run your own performance test before and after using this package.

Installation

npm install --save-dev resolver-cache

In your file, make sure you run your code after class instantiation:

ES5

var cacheDir = path.join(__dirname, '..','.cache');
var resolverCacheInstance = new ResolverCache({
    cacheFile: path.join(cacheDir, 'resolver-cache-server.sqlite3'),
    disable: false,
}, function () {

    // run your code here
});

ES6

const cacheDir = path.join(__dirname, '..', '.cache');

function startResolverCache() => {
    return new Promise( (resolve, reject) =>{
        new ResolverCache({
            cacheFile: path.join(cacheDir, 'resolver-cache-server.sqlite3'),
            disable: false,
        }, () => {
            resolve();
        });
    } );
}

Configuration

The following configuration options are available

  • cacheFile (string) the cache file to use; defaults to resolver-cache.sqlite3 in the OS's temporary directory
  • hookRequire (bool) whether to intercept calls to require(); defaults to true; this can be turned on later by calling classInstance.hookRequire()
  • alwaysVerifyCache (bool) whether to verify filemtime and filesize during all calls, defaults to false. This is useful during dev for file watchers
  • saveDelay (int) how many ms to wait for other file changes, before updating the cache database; defaults to 300
  • disable (bool) convenience parameter to disable the class entirely. Useful for debugging; defaults to false
  • transpiler (bool false or function) a function used to transform the contents of resolved files; defaults to false; the function is called with the following parameters: transpier(filepath, filesource); you are responsible for embedding transpiler options in the function you pass
  • resolve (function) the original file path resolution mechanism; defaults to NodeJS's Module._resolveFilename; this is useful for other tools such as webpack which already wrap resolution

Other Implementation Examples

With Webpack

var resolverCache = new (require('resolver-cache'))( { /* config opts */ }, function(){
    var webpackConfig = {
        // ...
        plugins: [
            new webpack.ResolverPlugin( () => {
                return { apply: function(resolver){
        			var oldResolve = resolver.resolve;
        			resolver.resolve = function resolve(context, request, callback) {

        				// perform cache lookup
        				var result = resolverCache.lookup(request, { id:context });

        				if(result){
        					return callback(null, result.path);
        				}

        				// call the old resolver
        				oldResolve.call(resolver, context, request, function(err, result) {
        					if(err) {
        						return callback(err);
        					}

        					// save to cache
        					try{
        						var source = fs.readFileSync(result, 'utf-8');
        						var mtime = fs.statSync(result).mtime.getTime();
        						resolverCache.cache(request, {id: context}, result, source, mtime);
        					}catch(e){
        						//
        					}

        					// send result on
        					callback(null, result);
        				});
        			};
        		} }
            }),
            // ...
        ]
    }
} );

Known Issues

  • there is no error handling during instantiation
  • class should be a singleton
  • code is not very tidy, although does the job

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0 [deprecated]           ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |