1.0.0 • Published 8 years ago
resolver-cache v1.0.0
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
requiressome-module
viarequire()
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 toresolver-cache.sqlite3
in the OS's temporary directoryhookRequire
(bool) whether to intercept calls torequire()
; defaults to true; this can be turned on later by callingclassInstance.hookRequire()
alwaysVerifyCache
(bool) whether to verify filemtime and filesize during all calls, defaults to false. This is useful during dev for file watcherssaveDelay
(int) how many ms to wait for other file changes, before updating the cache database; defaults to 300disable
(bool) convenience parameter to disable the class entirely. Useful for debugging; defaults to falsetranspiler
(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 passresolve
(function) the original file path resolution mechanism; defaults to NodeJS'sModule._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
1.0.0
8 years ago