Skip to content

Commit 266bd3b

Browse files
feat: add cache and parallel (#9)
1 parent 6f1e5b2 commit 266bd3b

16 files changed

+893
-189
lines changed

package-lock.json

Lines changed: 305 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@
4141
"webpack": "^4.0.0 || ^5.0.0"
4242
},
4343
"dependencies": {
44+
"cacache": "^15.0.5",
4445
"cssnano": "^4.1.10",
46+
"find-cache-dir": "^3.3.1",
47+
"jest-worker": "^26.1.0",
4548
"p-limit": "^3.0.2",
4649
"schema-utils": "^2.7.0",
50+
"serialize-javascript": "^4.0.0",
4751
"source-map": "^0.6.1",
4852
"webpack-sources": "^1.4.3"
4953
},

src/Webpack4Cache.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os from 'os';
2+
3+
import cacache from 'cacache';
4+
import findCacheDir from 'find-cache-dir';
5+
import serialize from 'serialize-javascript';
6+
7+
export default class Webpack4Cache {
8+
constructor(compilation, options) {
9+
this.cacheDir =
10+
options.cache === true
11+
? Webpack4Cache.getCacheDirectory()
12+
: options.cache;
13+
}
14+
15+
static getCacheDirectory() {
16+
return findCacheDir({ name: 'cssnano-webpack-plugin' }) || os.tmpdir();
17+
}
18+
19+
isEnabled() {
20+
return Boolean(this.cacheDir);
21+
}
22+
23+
get(task) {
24+
// eslint-disable-next-line no-param-reassign
25+
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
26+
27+
return cacache
28+
.get(this.cacheDir, task.cacheIdent)
29+
.then(({ data }) => JSON.parse(data));
30+
}
31+
32+
store(task, data) {
33+
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
34+
}
35+
}

src/Webpack5Cache.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// eslint-disable-next-line import/extensions,import/no-unresolved
2+
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
3+
import serialize from 'serialize-javascript';
4+
5+
import { util } from 'webpack';
6+
7+
export default class Cache {
8+
// eslint-disable-next-line no-unused-vars
9+
constructor(compilation, ignored) {
10+
this.compilation = compilation;
11+
}
12+
13+
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}/CssnanoWebpackPlugin/${cacheKeys}/${task.file}`;
34+
}
35+
36+
get(task) {
37+
// eslint-disable-next-line no-param-reassign
38+
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
39+
// eslint-disable-next-line no-param-reassign
40+
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);
41+
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+
});
57+
}
58+
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+
});
74+
}
75+
}

0 commit comments

Comments
 (0)