Skip to content

Commit

Permalink
fix: caching (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 14, 2020
1 parent 7fc01f0 commit 9de2a88
Show file tree
Hide file tree
Showing 21 changed files with 1,616 additions and 732 deletions.
1,087 changes: 636 additions & 451 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,33 @@
"dependencies": {
"cacache": "^15.0.5",
"find-cache-dir": "^3.3.1",
"schema-utils": "^2.7.0",
"serialize-javascript": "^4.0.0",
"schema-utils": "^2.7.1",
"serialize-javascript": "^5.0.1",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.4",
"@babel/preset-env": "^7.11.0",
"@commitlint/cli": "^10.0.0",
"@commitlint/config-conventional": "^10.0.0",
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@gfx/zopfli": "^1.0.15",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.3.0",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"eslint": "^7.7.0",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"file-loader": "^6.0.0",
"husky": "^4.2.5",
"jest": "^26.4.1",
"lint-staged": "^10.2.11",
"file-loader": "^6.1.0",
"husky": "^4.3.0",
"jest": "^26.4.2",
"lint-staged": "^10.3.0",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"prettier": "^2.1.1",
"standard-version": "^9.0.0",
"webpack": "^4.44.1"
},
Expand Down
26 changes: 12 additions & 14 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ export default class Webpack4Cache {
return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir();
}

async get(cacheData, sources) {
const weakOutput = this.weakCache.get(cacheData.source);

if (weakOutput) {
return weakOutput;
}

async get(cacheData, { RawSource }) {
if (!this.cache) {
// eslint-disable-next-line no-undefined
return undefined;
}

const weakOutput = this.weakCache.get(cacheData.inputSource);

if (weakOutput) {
return weakOutput;
}

// eslint-disable-next-line no-param-reassign
cacheData.cacheIdent =
cacheData.cacheIdent || serialize(cacheData.cacheKeys);
Expand All @@ -42,21 +42,19 @@ export default class Webpack4Cache {
return undefined;
}

return new sources.RawSource(
Buffer.from(JSON.parse(cachedResult.data).data)
);
return new RawSource(Buffer.from(JSON.parse(cachedResult.data).data));
}

async store(cacheData) {
if (!this.weakCache.has(cacheData.source)) {
this.weakCache.set(cacheData.source, cacheData.output);
}

if (!this.cache) {
// eslint-disable-next-line no-undefined
return undefined;
}

if (!this.weakCache.has(cacheData.inputSource)) {
this.weakCache.set(cacheData.inputSource, cacheData.output);
}

const { cacheIdent, output } = cacheData;

return cacache.put(this.cache, cacheIdent, JSON.stringify(output.source()));
Expand Down
6 changes: 3 additions & 3 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export default class Cache {
async get(cacheData) {
// eslint-disable-next-line no-param-reassign
cacheData.eTag =
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.source);
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.inputSource);

return this.cache.getPromise(cacheData.assetName, cacheData.eTag);
return this.cache.getPromise(cacheData.name, cacheData.eTag);
}

async store(cacheData) {
return this.cache.storePromise(
cacheData.assetName,
cacheData.name,
cacheData.eTag,
cacheData.output
);
Expand Down
46 changes: 32 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ class CompressionPlugin {
weakCache
);

for (const assetName of assetNames) {
for (const name of assetNames) {
scheduledTasks.push(
(async () => {
const { source, info } = CompressionPlugin.getAsset(
const { source: inputSource, info } = CompressionPlugin.getAsset(
compilation,
assetName
name
);

if (info.compressed) {
Expand All @@ -207,7 +207,7 @@ class CompressionPlugin {
return;
}

let input = source.source();
let input = inputSource.source();

if (!Buffer.isBuffer(input)) {
input = Buffer.from(input);
Expand All @@ -217,7 +217,7 @@ class CompressionPlugin {
return;
}

const cacheData = { source };
const cacheData = { inputSource };

if (CompressionPlugin.isWebpack4()) {
cacheData.cacheKeys = {
Expand All @@ -227,11 +227,11 @@ class CompressionPlugin {
algorithm: this.algorithm,
originalAlgorithm: this.options.algorithm,
compressionOptions: this.compressionOptions,
assetName,
name,
contentHash: crypto.createHash('md4').update(input).digest('hex'),
};
} else {
cacheData.assetName = assetName;
cacheData.name = name;
}

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

const newAssetName = CompressionPlugin.interpolateName(
assetName,
name,
this.options.filename
);

CompressionPlugin.emitAsset(compilation, newAssetName, output, {
compressed: true,
});
const newInfo = { compressed: true };

if (info.immutable) {
newInfo.immutable = true;
}

CompressionPlugin.emitAsset(
compilation,
newAssetName,
output,
newInfo
);

if (this.options.deleteOriginalAssets) {
// eslint-disable-next-line no-param-reassign
CompressionPlugin.deleteAsset(compilation, assetName);
CompressionPlugin.deleteAsset(compilation, name);
} else {
CompressionPlugin.updateAsset(compilation, assetName, source, {
// TODO `...` required only for webpack@4
const newOriginalInfo = {
...info,
related: { [relatedName]: newAssetName },
});
};

CompressionPlugin.updateAsset(
compilation,
name,
inputSource,
newOriginalInfo
);
}
})()
);
Expand Down
Loading

0 comments on commit 9de2a88

Please sign in to comment.