Skip to content

Commit 997e00f

Browse files
fix: compatibility cache feature with webpack@5 (#16)
1 parent 75895bb commit 997e00f

File tree

7 files changed

+306
-66
lines changed

7 files changed

+306
-66
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ npm-debug.log*
1010
/local
1111
/reports
1212
/node_modules
13+
/test/outputs/
1314
.DS_Store
1415
Thumbs.db
1516
.idea
1617
*.iml
1718
*.sublime-project
18-
*.sublime-workspace
19+
*.sublime-workspace

src/Webpack4Cache.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ export default class Webpack4Cache {
2222
return Boolean(this.cacheDir);
2323
}
2424

25-
get(task) {
25+
async get(task) {
2626
// eslint-disable-next-line no-param-reassign
2727
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
2828

29-
return cacache
30-
.get(this.cacheDir, task.cacheIdent)
31-
.then(({ data }) => JSON.parse(data));
29+
const { data } = await cacache.get(this.cacheDir, task.cacheIdent);
30+
31+
return JSON.parse(data);
3232
}
3333

34-
store(task, data) {
34+
async store(task, data) {
3535
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
3636
}
3737
}

src/Webpack5Cache.js

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,28 @@
1-
// eslint-disable-next-line import/extensions,import/no-unresolved
2-
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
31
import serialize from 'serialize-javascript';
42

5-
import { util } from 'webpack';
6-
73
export default class Cache {
84
// eslint-disable-next-line no-unused-vars
95
constructor(compilation, ignored) {
10-
this.compilation = compilation;
6+
this.cache = compilation.getCache('CssMinimizerWebpackPlugin');
117
}
128

9+
// eslint-disable-next-line class-methods-use-this
1310
isEnabled() {
14-
return Boolean(this.compilation.cache);
15-
}
16-
17-
createCacheIdent(task) {
18-
const {
19-
outputOptions: { hashSalt, hashDigest, hashDigestLength, hashFunction },
20-
} = this.compilation;
21-
22-
const hash = util.createHash(hashFunction);
23-
24-
if (hashSalt) {
25-
hash.update(hashSalt);
26-
}
27-
28-
hash.update(serialize(task.cacheKeys));
29-
30-
const digest = hash.digest(hashDigest);
31-
const cacheKeys = digest.substr(0, hashDigestLength);
32-
33-
return `${this.compilation.compilerPath}/CssMinimizerWebpackPlugin/${cacheKeys}/${task.file}`;
11+
return true;
3412
}
3513

36-
get(task) {
14+
async get(task) {
3715
// eslint-disable-next-line no-param-reassign
38-
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
16+
task.cacheIdent =
17+
task.cacheIdent || `${task.file}|${serialize(task.cacheKeys)}`;
3918
// eslint-disable-next-line no-param-reassign
40-
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);
19+
task.cacheETag =
20+
task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource);
4121

42-
return new Promise((resolve, reject) => {
43-
this.compilation.cache.get(
44-
task.cacheIdent,
45-
task.cacheETag,
46-
(err, result) => {
47-
if (err) {
48-
reject(err);
49-
} else if (result) {
50-
resolve(result);
51-
} else {
52-
reject();
53-
}
54-
}
55-
);
56-
});
22+
return this.cache.getPromise(task.cacheIdent, task.cacheETag);
5723
}
5824

59-
store(task, data) {
60-
return new Promise((resolve, reject) => {
61-
this.compilation.cache.store(
62-
task.cacheIdent,
63-
task.cacheETag,
64-
data,
65-
(err) => {
66-
if (err) {
67-
reject(err);
68-
} else {
69-
resolve(data);
70-
}
71-
}
72-
);
73-
});
25+
async store(task, data) {
26+
return this.cache.storePromise(task.cacheIdent, task.cacheETag, data);
7427
}
7528
}

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ class CssMinimizerPlugin {
399399
return enqueue(task);
400400
}
401401

402+
// Webpack@5 return `undefined` when cache is not found
403+
if (!taskResult) {
404+
return enqueue(task);
405+
}
406+
402407
task.callback(taskResult);
403408

404409
return Promise.resolve();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"cache" option should work with "false" value for the "cache" option: assets 1`] = `
4+
Object {
5+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
6+
"four.css": "h1{color:green}h2{color:#ff0}",
7+
"one.css": "a{text-align:center}",
8+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
9+
"two.css": "a{text-align:center}",
10+
}
11+
`;
12+
13+
exports[`"cache" option should work with "false" value for the "cache" option: assets 2`] = `
14+
Object {
15+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
16+
"four.css": "h1{color:green}h2{color:#ff0}",
17+
"one.css": "a{text-align:center}",
18+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
19+
"two.css": "a{text-align:center}",
20+
}
21+
`;
22+
23+
exports[`"cache" option should work with "false" value for the "cache" option: errors 1`] = `Array []`;
24+
25+
exports[`"cache" option should work with "false" value for the "cache" option: errors 2`] = `Array []`;
26+
27+
exports[`"cache" option should work with "false" value for the "cache" option: warnings 1`] = `Array []`;
28+
29+
exports[`"cache" option should work with "false" value for the "cache" option: warnings 2`] = `Array []`;
30+
31+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: assets 1`] = `
32+
Object {
33+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
34+
"four.css": "h1{color:green}h2{color:#ff0}",
35+
"one.css": "a{text-align:center}",
36+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
37+
"two.css": "a{text-align:center}",
38+
}
39+
`;
40+
41+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: assets 2`] = `
42+
Object {
43+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
44+
"four.css": "h1{color:green}h2{color:#ff0}",
45+
"one.css": "a{text-align:center}",
46+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
47+
"two.css": "a{text-align:center}",
48+
}
49+
`;
50+
51+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: errors 1`] = `Array []`;
52+
53+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: errors 2`] = `Array []`;
54+
55+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: warnings 1`] = `Array []`;
56+
57+
exports[`"cache" option should work with "filesystem" value for the "cache.type" option: warnings 2`] = `Array []`;
58+
59+
exports[`"cache" option should work with "memory" value for the "cache.type" option: assets 1`] = `
60+
Object {
61+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
62+
"four.css": "h1{color:green}h2{color:#ff0}",
63+
"one.css": "a{text-align:center}",
64+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
65+
"two.css": "a{text-align:center}",
66+
}
67+
`;
68+
69+
exports[`"cache" option should work with "memory" value for the "cache.type" option: assets 2`] = `
70+
Object {
71+
"five.css": "body{color:red;font-size:20px}a{color:#00f}",
72+
"four.css": "h1{color:green}h2{color:#ff0}",
73+
"one.css": "a{text-align:center}",
74+
"three.css": "body{color:red;font-size:20px}a{color:#00f}",
75+
"two.css": "a{text-align:center}",
76+
}
77+
`;
78+
79+
exports[`"cache" option should work with "memory" value for the "cache.type" option: errors 1`] = `Array []`;
80+
81+
exports[`"cache" option should work with "memory" value for the "cache.type" option: errors 2`] = `Array []`;
82+
83+
exports[`"cache" option should work with "memory" value for the "cache.type" option: warnings 1`] = `Array []`;
84+
85+
exports[`"cache" option should work with "memory" value for the "cache.type" option: warnings 2`] = `Array []`;

0 commit comments

Comments
 (0)