-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve compatibility with webpack@5
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
1 parent
7de0317
commit 1f9674e
Showing
36 changed files
with
1,515 additions
and
548 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/coverage | ||
/dist | ||
/node_modules | ||
/test/fixtures | ||
/test/fixtures | ||
/test/outputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ npm-debug.log* | |
/local | ||
/reports | ||
/node_modules | ||
/test/outputs | ||
.DS_Store | ||
Thumbs.db | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/dist | ||
/node_modules | ||
/test/fixtures | ||
CHANGELOG.md | ||
/test/outputs | ||
CHANGELOG.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.