77
88const { RawSource, CachedSource, CompatSource } = require ( "webpack-sources" ) ;
99const Compilation = require ( "../Compilation" ) ;
10+ const WebpackError = require ( "../WebpackError" ) ;
1011const { compareSelect, compareStrings } = require ( "../util/comparators" ) ;
1112const createHash = require ( "../util/createHash" ) ;
1213
@@ -141,6 +142,26 @@ class RealContentHashPlugin {
141142 ) ;
142143 const getDependencies = hash => {
143144 const assets = hashToAssets . get ( hash ) ;
145+ if ( ! assets ) {
146+ const referencingAssets = assetsWithInfo . filter ( asset =>
147+ asset . referencedHashes . has ( hash )
148+ ) ;
149+ const err = new WebpackError ( `RealContentHashPlugin
150+ Some kind of unexpected caching problem occurred.
151+ An asset was cached with a reference to another asset (${ hash } ) that's not in the compilation anymore.
152+ Either the asset was incorrectly cached, or the referenced asset should also be restored from cache.
153+ Referenced by:
154+ ${ referencingAssets
155+ . map ( a => {
156+ const match = new RegExp ( `.{0,20}${ quoteMeta ( hash ) } .{0,20}` ) . exec (
157+ a . content
158+ ) ;
159+ return ` - ${ a . name } : ...${ match ? match [ 0 ] : "???" } ...` ;
160+ } )
161+ . join ( "\n" ) } `) ;
162+ compilation . errors . push ( err ) ;
163+ return undefined ;
164+ }
144165 const hashes = new Set ( ) ;
145166 for ( const { referencedHashes } of assets ) {
146167 for ( const hash of referencedHashes ) {
@@ -157,6 +178,7 @@ class RealContentHashPlugin {
157178 for ( const hash of hashToAssets . keys ( ) ) {
158179 const add = ( hash , stack ) => {
159180 const deps = getDependencies ( hash ) ;
181+ if ( ! deps ) return ;
160182 stack . add ( hash ) ;
161183 for ( const dep of deps ) {
162184 if ( hashesInOrder . has ( dep ) ) continue ;
0 commit comments