cache-loader
cache-loader copied to clipboard
Hard to set custom cacheIdentifier that uses cache-loader version number
The default value for cacheIdentifier
is cache-loader:{version} {process.env.NODE_ENV}
. My project has multiple webpack config files where the step that uses cache-loader gets the same input from previous loaders but performs different transformations (eg. Istanbul instrumentation for test builds only).
I like that the default string includes the cache-loader version number, since it makes sense to invalidate the cache when this loader is updated. When setting a custom value, it is difficult to replicate this behavior since retrieving the cache-loader version number is tricky. It seems like the best options are hard-coding the version number into the string and hoping to remember to update it when upgrading to a new version of cache-loader, or some hacky manipulation of the path returned by require.resolve("cache-loader")
.
Couple of possible fixes:
- Append the user-provided
cacheIdentifier
to the default string instead of replacing the default string - Export the cache-loader version number
Happy to submit a PR with whatever fix is deemed best!
You can read the version number from package.json
file:
const fs = require('fs-extra');
const version = fs.readJsonSync('node_modules/cache-loader/package.json').version;
(alternatively use async method fs.readJson
to get a Promise
)
I believe you can just use:
const version = require('cache-loader/package.json').version;
What exactly does the cacheIdentifier
do? The documentation isn't clear.