What is caching-transform?
The caching-transform npm package is designed to cache the results of expensive transformation operations, such as those performed during the build or compilation process. It works by storing the results of these operations in a cache, keyed by a hash of the input. This means that if the input hasn't changed, the transformation can be skipped, and the cached result can be used instead. This can significantly speed up build times in development environments or for repeated builds.
What are caching-transform's main functionalities?
Caching file transformations
This code demonstrates how to use caching-transform to cache the results of a file transformation. The `transform` function represents an expensive operation. The `cache` function, created by `cachingTransform`, checks if a cached result exists for the given input. If not, it runs the transformation and caches the result.
const cachingTransform = require('caching-transform');
const fs = require('fs');
const path = require('path');
const cacheDir = path.join(__dirname, '.cache');
const transform = (input) => {
// Perform an expensive transformation here
return transformedInput;
};
const cache = cachingTransform({
cacheDir: cacheDir,
transform: transform
});
const input = fs.readFileSync('input.txt', 'utf8');
const output = cache(input);
console.log(output);
Other packages similar to caching-transform
babel-loader
babel-loader is a Webpack plugin that transpiles JavaScript files using Babel and caches the results. While caching-transform is a more generic tool that can be used with any transformation function, babel-loader is specifically designed for use with Webpack and Babel.
cache-loader
cache-loader is another Webpack loader that caches the result of expensive operations. It is similar to caching-transform in its purpose of caching transformation results to speed up build processes. However, cache-loader is specifically tailored for Webpack's ecosystem.
hard-source-webpack-plugin
hard-source-webpack-plugin is a plugin for Webpack that provides an intermediate caching step for modules. Like caching-transform, it aims to speed up build times by caching the results of expensive operations. However, it is tightly integrated with Webpack's architecture and caching strategy.
caching-transform
Wraps a transform and provides caching.
Caching transform results can greatly improve performance. nyc
saw dramatic performance increases when we implemented caching.
Install
$ npm install --save caching-transform
Usage
const cachingTransform = require('caching-transform');
cachingTransform({
cacheDir: '/path/to/cache/directory',
salt: 'hash-salt',
transform: (input, additionalData, hash) => {
return transformedResult;
}
});
API
cachingTransform(options)
Returns a transform callback that takes two arguments:
input
a string to be transformedadditionalData
an arbitrary data object.
Both arguments are passed to the wrapped transform. Results are cached in the cache directory using an md5
hash of input
and an optional salt
value. If a cache entry already exist for input
, the wrapped transform function will never be called.
options
salt
Type: string
Default: empty string
A string that uniquely identifies your transform, a typical salt value might be the concatenation of the module name of your transform and its version':
const pkg = require('my-transform/package.json');
const salt = pkg.name + ':' + pkg.version;
Including the package version in the salt ensures existing cache entries will be automatically invalidated when you bump the version of your transform. If your transform relies on additional dependencies, and the transform output might change as those dependencies update, then your salt should incorporate the versions of those dependencies as well.
transform
Type: Function(input: string, additionalData: *, hash: string): string
input
: The string to be transformed. passed through from the wrapper.additionalData
: An arbitrary data object passed through from the wrapper. A typical value might be a string filename.hash
: The salted hash of input
. You will rarely need to use this, unless you intend to create multiple cache entries per transform invocation.
The transform function should return a string
containing the result of transforming input
.
factory
Type: Function(cacheDir: string): transformFunction
If the transform
function is expensive to create, and it is reasonable to expect that it may never be called during the life of the process, you can may supply a factory
function that will be used to create the transform
function the first time it is needed.
cacheDir
Type: string
The directory where cached transform results will be stored. The directory is automatically created with mkdirp
. You can set options.createCacheDir = false
if you are certain the directory already exists.
ext
Type: string
Default: empty string
An extension that will be appended to the salted hash to create the filename inside your cache directory. It is not required, but recommended if you know the file type. Appending the extension allows you to easily inspect the contents of the cache directory with your file browser.
License
MIT © James Talmage