1 | [![npm][npm]][npm-url]
|
2 | [![node][node]][node-url]
|
3 | [![deps][deps]][deps-url]
|
4 | [![test][test]][test-url]
|
5 | [![coverage][cover]][cover-url]
|
6 | [![chat][chat]][chat-url]
|
7 |
|
8 | <div align="center">
|
9 | <a href="https://webpack.js.org/">
|
10 | <img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
|
11 | </a>
|
12 | <h1>Cache Loader</h1>
|
13 | <p>Caches the result of following loaders on disk (default) or in the database</p>
|
14 | </div>
|
15 |
|
16 | <h2 align="center">Install</h2>
|
17 |
|
18 | ```bash
|
19 | npm install --save-dev cache-loader
|
20 | ```
|
21 |
|
22 | <h2 align="center">Usage</h2>
|
23 |
|
24 | Add this loader in front of other (expensive) loaders to cache the result on disk.
|
25 |
|
26 | **webpack.config.js**
|
27 | ```js
|
28 | module.exports = {
|
29 | module: {
|
30 | rules: [
|
31 | {
|
32 | test: /\.ext$/,
|
33 | use: [
|
34 | 'cache-loader',
|
35 | ...loaders
|
36 | ],
|
37 | include: path.resolve('src')
|
38 | }
|
39 | ]
|
40 | }
|
41 | }
|
42 | ```
|
43 |
|
44 | > ⚠️ Note that there is an overhead for saving the reading and saving the cache file, so only use this loader to cache expensive loaders.
|
45 |
|
46 | <h2 align="center">Options</h2>
|
47 |
|
48 | |Name|Type|Default|Description|
|
49 | |:--:|:--:|:-----:|:----------|
|
50 | |**`cacheKey`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
|
51 | |**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
|
52 | |**`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)|
|
53 | |**`write`**|`{Function(cacheKey, data, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached)|
|
54 | |**`read`**|`{Function(cacheKey, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|
|
55 |
|
56 | <h2 align="center">Examples</h2>
|
57 |
|
58 | **webpack.config.js**
|
59 | ```js
|
60 | module.exports = {
|
61 | module: {
|
62 | rules: [
|
63 | {
|
64 | test: /\.js$/,
|
65 | use: [
|
66 | 'cache-loader',
|
67 | 'babel-loader'
|
68 | ],
|
69 | include: path.resolve('src')
|
70 | }
|
71 | ]
|
72 | }
|
73 | }
|
74 | ```
|
75 |
|
76 | ### `Database Integration`
|
77 |
|
78 | **webpack.config.js**
|
79 | ```js
|
80 | // Or different database client - memcached, mongodb, ...
|
81 | const redis = require('redis');
|
82 | const crypto = require('crypto');
|
83 |
|
84 | // ...
|
85 | // connect to client
|
86 | // ...
|
87 |
|
88 | const BUILD_CACHE_TIMEOUT = 24 * 3600; // 1 day
|
89 |
|
90 | function digest(str) {
|
91 | return crypto.createHash('md5').update(str).digest('hex');
|
92 | }
|
93 |
|
94 | // Generate own cache key
|
95 | function cacheKey(options, request) {
|
96 | return `build:cache:${digest(request)}`;
|
97 | }
|
98 |
|
99 |
|
100 | // Read data from database and parse them
|
101 | function read(key, callback) {
|
102 | client.get(key, (err, result) => {
|
103 | if (err) {
|
104 | return callback(err);
|
105 | }
|
106 |
|
107 | if (!result) {
|
108 | return callback(new Error(`Key ${key} not found`));
|
109 | }
|
110 |
|
111 | try {
|
112 | let data = JSON.parse(result);
|
113 | callback(null, data);
|
114 | } catch (e) {
|
115 | callback(e);
|
116 | }
|
117 | });
|
118 | }
|
119 |
|
120 |
|
121 | // Write data to database under cacheKey
|
122 | function write(key, data, callback) {
|
123 | client.set(key, JSON.stringify(data), 'EX', BUILD_CACHE_TIMEOUT, callback);
|
124 | }
|
125 |
|
126 | module.exports = {
|
127 | module: {
|
128 | rules: [
|
129 | {
|
130 | test: /\.js$/,
|
131 | use: [
|
132 | {
|
133 | loader: 'cache-loader',
|
134 | options: {
|
135 | cacheKey,
|
136 | read,
|
137 | write,
|
138 | }
|
139 | },
|
140 | 'babel-loader'
|
141 | ],
|
142 | include: path.resolve('src')
|
143 | }
|
144 | ]
|
145 | }
|
146 | }
|
147 | ```
|
148 |
|
149 | <h2 align="center">Maintainers</h2>
|
150 |
|
151 | <table>
|
152 | <tbody>
|
153 | <tr>
|
154 | <td align="center">
|
155 | <a href="https://github.com/sokra">
|
156 | <img width="150" height="150" src="https://github.com/sokra.png?size=150">
|
157 | </br>
|
158 | Tobias Koppers
|
159 | </a>
|
160 | </td>
|
161 | <td align="center">
|
162 | <a href="https://github.com/bebraw">
|
163 | <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
|
164 | </br>
|
165 | Juho Vepsäläinen
|
166 | </a>
|
167 | </td>
|
168 | <td align="center">
|
169 | <a href="https://github.com/d3viant0ne">
|
170 | <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
|
171 | </br>
|
172 | Joshua Wiens
|
173 | </a>
|
174 | </td>
|
175 | <td align="center">
|
176 | <a href="https://github.com/michael-ciniawsky">
|
177 | <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
|
178 | </br>
|
179 | Michael Ciniawsky
|
180 | </a>
|
181 | </td>
|
182 | <td align="center">
|
183 | <a href="https://github.com/evilebottnawi">
|
184 | <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
|
185 | </br>
|
186 | Alexander Krasnoyarov
|
187 | </a>
|
188 | </td>
|
189 | </tr>
|
190 | <tbody>
|
191 | </table>
|
192 |
|
193 |
|
194 | [npm]: https://img.shields.io/npm/v/cache-loader.svg
|
195 | [npm-url]: https://npmjs.com/package/cache-loader
|
196 |
|
197 | [node]: https://img.shields.io/node/v/cache-loader.svg
|
198 | [node-url]: https://nodejs.org
|
199 |
|
200 | [deps]: https://david-dm.org/webpack-contrib/cache-loader.svg
|
201 | [deps-url]: https://david-dm.org/webpack-contrib/cache-loader
|
202 |
|
203 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
204 | [chat-url]: https://gitter.im/webpack/webpack
|
205 |
|
206 | [test]: https://img.shields.io/travis/webpack-contrib/cache-loader.svg
|
207 | [test-url]: https://travis-ci.org/webpack-contrib/cache-loader
|
208 |
|
209 | [cover]: https://codecov.io/gh/webpack-contrib/cache-loader/branch/master/graph/badge.svg
|
210 | [cover-url]: https://codecov.io/gh/webpack-contrib/cache-loader
|