@@ -22,6 +22,7 @@ const ModuleParseError = require("./ModuleParseError");
2222const ModuleBuildError = require ( "./ModuleBuildError" ) ;
2323const ModuleError = require ( "./ModuleError" ) ;
2424const ModuleWarning = require ( "./ModuleWarning" ) ;
25+ const createHash = require ( "./util/createHash" ) ;
2526
2627const asString = buf => {
2728 if ( Buffer . isBuffer ( buf ) ) {
@@ -89,6 +90,7 @@ class NormalModule extends Module {
8990 // Info from Build
9091 this . error = null ;
9192 this . _source = null ;
93+ this . _buildHash = "" ;
9294 this . buildTimestamp = undefined ;
9395 this . _cachedSource = undefined ;
9496 this . _cachedSourceHash = undefined ;
@@ -327,11 +329,23 @@ class NormalModule extends Module {
327329 return false ;
328330 }
329331
332+ _initBuildHash ( compilation ) {
333+ const hash = createHash ( compilation . outputOptions . hashFunction ) ;
334+ if ( this . _source ) {
335+ hash . update ( "source" ) ;
336+ this . _source . updateHash ( hash ) ;
337+ }
338+ hash . update ( "meta" ) ;
339+ hash . update ( JSON . stringify ( this . buildMeta ) ) ;
340+ this . _buildHash = hash . digest ( "hex" ) ;
341+ }
342+
330343 build ( options , compilation , resolver , fs , callback ) {
331344 this . buildTimestamp = Date . now ( ) ;
332345 this . built = true ;
333346 this . _source = null ;
334347 this . _ast = null ;
348+ this . _buildHash = "" ;
335349 this . error = null ;
336350 this . errors . length = 0 ;
337351 this . warnings . length = 0 ;
@@ -349,25 +363,29 @@ class NormalModule extends Module {
349363 // if we have an error mark module as failed and exit
350364 if ( err ) {
351365 this . markModuleAsErrored ( err ) ;
366+ this . _initBuildHash ( compilation ) ;
352367 return callback ( ) ;
353368 }
354369
355370 // check if this module should !not! be parsed.
356371 // if so, exit here;
357372 const noParseRule = options . module && options . module . noParse ;
358373 if ( this . shouldPreventParsing ( noParseRule , this . request ) ) {
374+ this . _initBuildHash ( compilation ) ;
359375 return callback ( ) ;
360376 }
361377
362378 const handleParseError = e => {
363379 const source = this . _source . source ( ) ;
364380 const error = new ModuleParseError ( this , source , e ) ;
365381 this . markModuleAsErrored ( error ) ;
382+ this . _initBuildHash ( compilation ) ;
366383 return callback ( ) ;
367384 } ;
368385
369386 const handleParseResult = result => {
370387 this . _lastSuccessfulBuildMeta = this . buildMeta ;
388+ this . _initBuildHash ( compilation ) ;
371389 return callback ( ) ;
372390 } ;
373391
@@ -454,23 +472,8 @@ class NormalModule extends Module {
454472 return this . _source ? this . _source . size ( ) : - 1 ;
455473 }
456474
457- updateHashWithSource ( hash ) {
458- if ( ! this . _source ) {
459- hash . update ( "null" ) ;
460- return ;
461- }
462- hash . update ( "source" ) ;
463- this . _source . updateHash ( hash ) ;
464- }
465-
466- updateHashWithMeta ( hash ) {
467- hash . update ( "meta" ) ;
468- hash . update ( JSON . stringify ( this . buildMeta ) ) ;
469- }
470-
471475 updateHash ( hash ) {
472- this . updateHashWithSource ( hash ) ;
473- this . updateHashWithMeta ( hash ) ;
476+ hash . update ( this . _buildHash ) ;
474477 super . updateHash ( hash ) ;
475478 }
476479}
0 commit comments