cache-loader icon indicating copy to clipboard operation
cache-loader copied to clipboard

Hard to set custom cacheIdentifier that uses cache-loader version number

Open jochakovsky opened this issue 7 years ago • 3 comments

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:

  1. Append the user-provided cacheIdentifier to the default string instead of replacing the default string
  2. Export the cache-loader version number

Happy to submit a PR with whatever fix is deemed best!

jochakovsky avatar Jan 27 '18 08:01 jochakovsky

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)

filmic avatar Mar 23 '18 17:03 filmic

I believe you can just use:

const version = require('cache-loader/package.json').version;

Cleecanth avatar Dec 06 '18 18:12 Cleecanth

What exactly does the cacheIdentifier do? The documentation isn't clear.

prograhammer avatar Mar 27 '19 03:03 prograhammer