Skip to content

Commit

Permalink
Merge pull request #11628 from webpack/bugfix/real-content-hash
Browse files Browse the repository at this point in the history
emit error instead of crashing when unexpected problem occurs
  • Loading branch information
sokra committed Oct 10, 2020
2 parents 75ecff2 + 84b196d commit bbe1230
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/optimize/RealContentHashPlugin.js
Expand Up @@ -7,6 +7,7 @@

const { RawSource, CachedSource, CompatSource } = require("webpack-sources");
const Compilation = require("../Compilation");
const WebpackError = require("../WebpackError");
const { compareSelect, compareStrings } = require("../util/comparators");
const createHash = require("../util/createHash");

Expand Down Expand Up @@ -141,6 +142,26 @@ class RealContentHashPlugin {
);
const getDependencies = hash => {
const assets = hashToAssets.get(hash);
if (!assets) {
const referencingAssets = assetsWithInfo.filter(asset =>
asset.referencedHashes.has(hash)
);
const err = new WebpackError(`RealContentHashPlugin
Some kind of unexpected caching problem occurred.
An asset was cached with a reference to another asset (${hash}) that's not in the compilation anymore.
Either the asset was incorrectly cached, or the referenced asset should also be restored from cache.
Referenced by:
${referencingAssets
.map(a => {
const match = new RegExp(`.{0,20}${quoteMeta(hash)}.{0,20}`).exec(
a.content
);
return ` - ${a.name}: ...${match ? match[0] : "???"}...`;
})
.join("\n")}`);
compilation.errors.push(err);
return undefined;
}
const hashes = new Set();
for (const { referencedHashes } of assets) {
for (const hash of referencedHashes) {
Expand All @@ -157,6 +178,7 @@ class RealContentHashPlugin {
for (const hash of hashToAssets.keys()) {
const add = (hash, stack) => {
const deps = getDependencies(hash);
if (!deps) return;
stack.add(hash);
for (const dep of deps) {
if (hashesInOrder.has(dep)) continue;
Expand Down

0 comments on commit bbe1230

Please sign in to comment.