Skip to content

Commit

Permalink
feat: improve compatibility with webpack@5
Browse files Browse the repository at this point in the history
BREAKING CHANGE: use `processAssets` hook for webpack@5 compatibility, it can create incompatibility with plugins that do not support webpack@5, please open an issue in their repositories
  • Loading branch information
evilebottnawi committed Aug 14, 2020
1 parent 7de0317 commit 1f9674e
Show file tree
Hide file tree
Showing 36 changed files with 1,515 additions and 548 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/coverage
/dist
/node_modules
/test/fixtures
/test/fixtures
/test/outputs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ npm-debug.log*
/local
/reports
/node_modules
/test/outputs
.DS_Store
Thumbs.db
.idea
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/dist
/node_modules
/test/fixtures
CHANGELOG.md
/test/outputs
CHANGELOG.md
23 changes: 15 additions & 8 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ export default class Webpack4Cache {
return Boolean(this.cacheDir);
}

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

return cacache.get(this.cacheDir, task.cacheIdent).then(({ data }) => {
const result = JSON.parse(data);
let cachedResult;

result.output = Buffer.from(result.output);
try {
cachedResult = await cacache.get(this.cacheDir, task.cacheIdent);
} catch (ignoreError) {
// eslint-disable-next-line no-undefined
return undefined;
}

return result;
});
return Buffer.from(JSON.parse(cachedResult.data).data);
}

store(task, data) {
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
async store(task) {
return cacache.put(
this.cacheDir,
task.cacheIdent,
JSON.stringify(task.output)
);
}
}
68 changes: 8 additions & 60 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
// eslint-disable-next-line import/extensions,import/no-unresolved
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';

import { util } from 'webpack';

export default class Cache {
// eslint-disable-next-line no-unused-vars
constructor(compilation, ignored) {
this.compilation = compilation;
this.cache = compilation.getCache('CompressionWebpackPlugin');
}

// eslint-disable-next-line class-methods-use-this
isEnabled() {
return Boolean(this.compilation.cache);
}

createCacheIdent(task) {
const {
outputOptions: { hashSalt, hashDigest, hashDigestLength, hashFunction },
} = this.compilation;

const hash = util.createHash(hashFunction);

if (hashSalt) {
hash.update(hashSalt);
}

hash.update(JSON.stringify(task.cacheKeys));

const digest = hash.digest(hashDigest);
const cacheKeys = digest.substr(0, hashDigestLength);

return `${this.compilation.compilerPath}/CompressionPlugin/${cacheKeys}/${task.filename}`;
return true;
}

get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
async get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);
task.eTag = task.eTag || this.cache.getLazyHashedEtag(task.assetSource);

return new Promise((resolve, reject) => {
this.compilation.cache.get(
task.cacheIdent,
task.cacheETag,
(err, result) => {
if (err) {
reject(err);
} else if (result) {
resolve(result);
} else {
reject();
}
}
);
});
return this.cache.getPromise(task.assetName, task.eTag);
}

store(task, data) {
return new Promise((resolve, reject) => {
this.compilation.cache.store(
task.cacheIdent,
task.cacheETag,
data,
(err) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}
);
});
async store(task) {
return this.cache.storePromise(task.assetName, task.eTag, task.output);
}
}
Loading

0 comments on commit 1f9674e

Please sign in to comment.