{"id":13615826,"url":"https://github.com/webpack-contrib/cache-loader","last_synced_at":"2025-04-13T21:31:40.913Z","repository":{"id":57113877,"uuid":"89460275","full_name":"webpack-contrib/cache-loader","owner":"webpack-contrib","description":"[DEPRECATED] Caches the result of following loaders on disk","archived":true,"fork":false,"pushed_at":"2020-10-27T14:38:24.000Z","size":1208,"stargazers_count":640,"open_issues_count":26,"forks_count":51,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-09T10:04:10.008Z","etag":null,"topics":["cache","webpack-loader"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"open_collective":"webpack"}},"created_at":"2017-04-26T08:59:47.000Z","updated_at":"2024-12-03T22:14:14.000Z","dependencies_parsed_at":"2022-08-22T06:51:07.433Z","dependency_job_id":null,"html_url":"https://github.com/webpack-contrib/cache-loader","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcache-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcache-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcache-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcache-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-contrib","download_url":"https://codeload.github.com/webpack-contrib/cache-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248786362,"owners_count":21161436,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","webpack-loader"],"created_at":"2024-08-01T20:01:18.593Z","updated_at":"2025-04-13T21:31:35.895Z","avatar_url":"https://github.com/webpack-contrib.png","language":"JavaScript","readme":"# DEPRECATED\n\nConsider upgrading webpack to version 5 and setup cache https://webpack.js.org/configuration/other-options/#cache\n\n---\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://webpack.js.org/\"\u003e\n    \u003cimg width=\"200\" height=\"200\" src=\"https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n[![coverage][cover]][cover-url]\n[![chat][chat]][chat-url]\n[![size][size]][size-url]\n\n# cache-loader\n\nThe `cache-loader` allow to Caches the result of following loaders on disk (default) or in the database.\n\n## Getting Started\n\nTo begin, you'll need to install `cache-loader`:\n\n```console\nnpm install --save-dev cache-loader\n```\n\nAdd this loader in front of other (expensive) loaders to cache the result on disk.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.ext$/,\n        use: ['cache-loader', ...loaders],\n        include: path.resolve('src'),\n      },\n    ],\n  },\n};\n```\n\n\u003e ⚠️ Note that there is an overhead for saving the reading and saving the cache file, so only use this loader to cache expensive loaders.\n\n## Options\n\n|         Name          |                       Type                       |                        n Default                        | Description                                                                                                                                                            |\n| :-------------------: | :----------------------------------------------: | :-----------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n|  **`cacheContext`**   |                    `{String}`                    |                       `undefined`                       | Allows you to override the default cache context in order to generate the cache relatively to a path. By default it will use absolute paths                            |\n|    **`cacheKey`**     |    `{Function(options, request) -\u003e {String}}`    |                       `undefined`                       | Allows you to override default cache key generator                                                                                                                     |\n| **`cacheDirectory`**  |                    `{String}`                    | `findCacheDir({ name: 'cache-loader' }) or os.tmpdir()` | Provide a cache directory where cache items should be stored (used for default read/write implementation)                                                              |\n| **`cacheIdentifier`** |                    `{String}`                    |     `cache-loader:{version} {process.env.NODE_ENV}`     | Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation) |\n|     **`compare`**     |      `{Function(stats, dep) -\u003e {Boolean}}`       |                       `undefined`                       | Allows you to override default comparison function between the cached dependency and the one is being read. Return `true` to use the cached resource                   |\n|    **`precision`**    |                    `{Number}`                    |                           `0`                           | Round `mtime` by this number of milliseconds both for `stats` and `dep` before passing those params to the comparing function                                          |\n|      **`read`**       |    `{Function(cacheKey, callback) -\u003e {void}}`    |                       `undefined`                       | Allows you to override default read cache data from file                                                                                                               |\n|    **`readOnly`**     |                   `{Boolean}`                    |                         `false`                         | Allows you to override default value and make the cache read only (useful for some environments where you don't want the cache to be updated, only read from it)       |\n|      **`write`**      | `{Function(cacheKey, data, callback) -\u003e {void}}` |                       `undefined`                       | Allows you to override default write cache data to file (e.g. Redis, memcached)                                                                                        |\n\n## Examples\n\n### Basic\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.js$/,\n        use: ['cache-loader', 'babel-loader'],\n        include: path.resolve('src'),\n      },\n    ],\n  },\n};\n```\n\n### Database Integration\n\n**webpack.config.js**\n\n```js\n// Or different database client - memcached, mongodb, ...\nconst redis = require('redis');\nconst crypto = require('crypto');\n\n// ...\n// connect to client\n// ...\n\nconst BUILD_CACHE_TIMEOUT = 24 * 3600; // 1 day\n\nfunction digest(str) {\n  return crypto\n    .createHash('md5')\n    .update(str)\n    .digest('hex');\n}\n\n// Generate own cache key\nfunction cacheKey(options, request) {\n  return `build:cache:${digest(request)}`;\n}\n\n// Read data from database and parse them\nfunction read(key, callback) {\n  client.get(key, (err, result) =\u003e {\n    if (err) {\n      return callback(err);\n    }\n\n    if (!result) {\n      return callback(new Error(`Key ${key} not found`));\n    }\n\n    try {\n      let data = JSON.parse(result);\n      callback(null, data);\n    } catch (e) {\n      callback(e);\n    }\n  });\n}\n\n// Write data to database under cacheKey\nfunction write(key, data, callback) {\n  client.set(key, JSON.stringify(data), 'EX', BUILD_CACHE_TIMEOUT, callback);\n}\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.js$/,\n        use: [\n          {\n            loader: 'cache-loader',\n            options: {\n              cacheKey,\n              read,\n              write,\n            },\n          },\n          'babel-loader',\n        ],\n        include: path.resolve('src'),\n      },\n    ],\n  },\n};\n```\n\n## Contributing\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/cache-loader.svg\n[npm-url]: https://npmjs.com/package/cache-loader\n[node]: https://img.shields.io/node/v/cache-loader.svg\n[node-url]: https://nodejs.org\n[deps]: https://david-dm.org/webpack-contrib/cache-loader.svg\n[deps-url]: https://david-dm.org/webpack-contrib/cache-loader\n[tests]: https://dev.azure.com/webpack-contrib/cache-loader/_apis/build/status/webpack-contrib.cache-loader?branchName=master\n[tests-url]: https://dev.azure.com/webpack-contrib/cache-loader/_build/latest?definitionId=4\u0026branchName=master\n[cover]: https://codecov.io/gh/webpack-contrib/cache-loader/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack-contrib/cache-loader\n[chat]: https://badges.gitter.im/webpack/webpack.svg\n[chat-url]: https://gitter.im/webpack/webpack\n[size]: https://packagephobia.now.sh/badge?p=cache-loader\n[size-url]: https://packagephobia.now.sh/result?p=cache-loader\n","funding_links":["https://opencollective.com/webpack"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fcache-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-contrib%2Fcache-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fcache-loader/lists"}