Skip to content

Commit 9de2a88

Browse files
fix: caching (#194)
1 parent 7fc01f0 commit 9de2a88

21 files changed

+1616
-732
lines changed

package-lock.json

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

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,33 @@
4343
"dependencies": {
4444
"cacache": "^15.0.5",
4545
"find-cache-dir": "^3.3.1",
46-
"schema-utils": "^2.7.0",
47-
"serialize-javascript": "^4.0.0",
46+
"schema-utils": "^2.7.1",
47+
"serialize-javascript": "^5.0.1",
4848
"webpack-sources": "^1.4.3"
4949
},
5050
"devDependencies": {
51-
"@babel/cli": "^7.10.5",
52-
"@babel/core": "^7.11.4",
53-
"@babel/preset-env": "^7.11.0",
54-
"@commitlint/cli": "^10.0.0",
55-
"@commitlint/config-conventional": "^10.0.0",
51+
"@babel/cli": "^7.11.6",
52+
"@babel/core": "^7.11.6",
53+
"@babel/preset-env": "^7.11.5",
54+
"@commitlint/cli": "^11.0.0",
55+
"@commitlint/config-conventional": "^11.0.0",
5656
"@gfx/zopfli": "^1.0.15",
5757
"@webpack-contrib/defaults": "^6.3.0",
5858
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5959
"babel-jest": "^26.3.0",
6060
"cross-env": "^7.0.2",
6161
"del": "^5.1.0",
6262
"del-cli": "^3.0.1",
63-
"eslint": "^7.7.0",
63+
"eslint": "^7.9.0",
6464
"eslint-config-prettier": "^6.11.0",
6565
"eslint-plugin-import": "^2.22.0",
66-
"file-loader": "^6.0.0",
67-
"husky": "^4.2.5",
68-
"jest": "^26.4.1",
69-
"lint-staged": "^10.2.11",
66+
"file-loader": "^6.1.0",
67+
"husky": "^4.3.0",
68+
"jest": "^26.4.2",
69+
"lint-staged": "^10.3.0",
7070
"memfs": "^3.2.0",
7171
"npm-run-all": "^4.1.5",
72-
"prettier": "^2.0.5",
72+
"prettier": "^2.1.1",
7373
"standard-version": "^9.0.0",
7474
"webpack": "^4.44.1"
7575
},

src/Webpack4Cache.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ export default class Webpack4Cache {
1717
return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir();
1818
}
1919

20-
async get(cacheData, sources) {
21-
const weakOutput = this.weakCache.get(cacheData.source);
22-
23-
if (weakOutput) {
24-
return weakOutput;
25-
}
26-
20+
async get(cacheData, { RawSource }) {
2721
if (!this.cache) {
2822
// eslint-disable-next-line no-undefined
2923
return undefined;
3024
}
3125

26+
const weakOutput = this.weakCache.get(cacheData.inputSource);
27+
28+
if (weakOutput) {
29+
return weakOutput;
30+
}
31+
3232
// eslint-disable-next-line no-param-reassign
3333
cacheData.cacheIdent =
3434
cacheData.cacheIdent || serialize(cacheData.cacheKeys);
@@ -42,21 +42,19 @@ export default class Webpack4Cache {
4242
return undefined;
4343
}
4444

45-
return new sources.RawSource(
46-
Buffer.from(JSON.parse(cachedResult.data).data)
47-
);
45+
return new RawSource(Buffer.from(JSON.parse(cachedResult.data).data));
4846
}
4947

5048
async store(cacheData) {
51-
if (!this.weakCache.has(cacheData.source)) {
52-
this.weakCache.set(cacheData.source, cacheData.output);
53-
}
54-
5549
if (!this.cache) {
5650
// eslint-disable-next-line no-undefined
5751
return undefined;
5852
}
5953

54+
if (!this.weakCache.has(cacheData.inputSource)) {
55+
this.weakCache.set(cacheData.inputSource, cacheData.output);
56+
}
57+
6058
const { cacheIdent, output } = cacheData;
6159

6260
return cacache.put(this.cache, cacheIdent, JSON.stringify(output.source()));

src/Webpack5Cache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export default class Cache {
66
async get(cacheData) {
77
// eslint-disable-next-line no-param-reassign
88
cacheData.eTag =
9-
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.source);
9+
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.inputSource);
1010

11-
return this.cache.getPromise(cacheData.assetName, cacheData.eTag);
11+
return this.cache.getPromise(cacheData.name, cacheData.eTag);
1212
}
1313

1414
async store(cacheData) {
1515
return this.cache.storePromise(
16-
cacheData.assetName,
16+
cacheData.name,
1717
cacheData.eTag,
1818
cacheData.output
1919
);

src/index.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ class CompressionPlugin {
175175
weakCache
176176
);
177177

178-
for (const assetName of assetNames) {
178+
for (const name of assetNames) {
179179
scheduledTasks.push(
180180
(async () => {
181-
const { source, info } = CompressionPlugin.getAsset(
181+
const { source: inputSource, info } = CompressionPlugin.getAsset(
182182
compilation,
183-
assetName
183+
name
184184
);
185185

186186
if (info.compressed) {
@@ -207,7 +207,7 @@ class CompressionPlugin {
207207
return;
208208
}
209209

210-
let input = source.source();
210+
let input = inputSource.source();
211211

212212
if (!Buffer.isBuffer(input)) {
213213
input = Buffer.from(input);
@@ -217,7 +217,7 @@ class CompressionPlugin {
217217
return;
218218
}
219219

220-
const cacheData = { source };
220+
const cacheData = { inputSource };
221221

222222
if (CompressionPlugin.isWebpack4()) {
223223
cacheData.cacheKeys = {
@@ -227,11 +227,11 @@ class CompressionPlugin {
227227
algorithm: this.algorithm,
228228
originalAlgorithm: this.options.algorithm,
229229
compressionOptions: this.compressionOptions,
230-
assetName,
230+
name,
231231
contentHash: crypto.createHash('md4').update(input).digest('hex'),
232232
};
233233
} else {
234-
cacheData.assetName = assetName;
234+
cacheData.name = name;
235235
}
236236

237237
let output = await cache.get(cacheData, { RawSource });
@@ -255,21 +255,39 @@ class CompressionPlugin {
255255
}
256256

257257
const newAssetName = CompressionPlugin.interpolateName(
258-
assetName,
258+
name,
259259
this.options.filename
260260
);
261261

262-
CompressionPlugin.emitAsset(compilation, newAssetName, output, {
263-
compressed: true,
264-
});
262+
const newInfo = { compressed: true };
263+
264+
if (info.immutable) {
265+
newInfo.immutable = true;
266+
}
267+
268+
CompressionPlugin.emitAsset(
269+
compilation,
270+
newAssetName,
271+
output,
272+
newInfo
273+
);
265274

266275
if (this.options.deleteOriginalAssets) {
267276
// eslint-disable-next-line no-param-reassign
268-
CompressionPlugin.deleteAsset(compilation, assetName);
277+
CompressionPlugin.deleteAsset(compilation, name);
269278
} else {
270-
CompressionPlugin.updateAsset(compilation, assetName, source, {
279+
// TODO `...` required only for webpack@4
280+
const newOriginalInfo = {
281+
...info,
271282
related: { [relatedName]: newAssetName },
272-
});
283+
};
284+
285+
CompressionPlugin.updateAsset(
286+
compilation,
287+
name,
288+
inputSource,
289+
newOriginalInfo
290+
);
273291
}
274292
})()
275293
);

0 commit comments

Comments
 (0)