@@ -61,6 +61,12 @@ class NonErrorEmittedError extends WebpackError {
6161 }
6262}
6363
64+ /**
65+ * @typedef {Object } CachedSourceEntry
66+ * @property {any } source the generated source
67+ * @property {string } hash the hash value
68+ */
69+
6470class NormalModule extends Module {
6571 constructor ( {
6672 type,
@@ -90,8 +96,8 @@ class NormalModule extends Module {
9096 this . error = null ;
9197 this . _source = null ;
9298 this . buildTimestamp = undefined ;
93- this . _cachedSource = undefined ;
94- this . _cachedSourceHash = undefined ;
99+ /** @private @type { Map<string, CachedSourceEntry> } */
100+ this . _cachedSources = new Map ( ) ;
95101
96102 // Options for the NormalModule set by plugins
97103 // TODO refactor this -> options object filled from Factory
@@ -343,8 +349,7 @@ class NormalModule extends Module {
343349 } ;
344350
345351 return this . doBuild ( options , compilation , resolver , fs , err => {
346- this . _cachedSource = undefined ;
347- this . _cachedSourceHash = undefined ;
352+ this . _cachedSources . clear ( ) ;
348353
349354 // if we have an error mark module as failed and exit
350355 if ( err ) {
@@ -403,22 +408,26 @@ class NormalModule extends Module {
403408 return `${ this . hash } -${ dtHash } ` ;
404409 }
405410
406- source ( dependencyTemplates , runtimeTemplate ) {
411+ source ( dependencyTemplates , runtimeTemplate , type = "javascript" ) {
407412 const hashDigest = this . getHashDigest ( dependencyTemplates ) ;
408- if ( this . _cachedSourceHash === hashDigest ) {
413+ const cacheEntry = this . _cachedSources . get ( type ) ;
414+ if ( cacheEntry !== undefined && cacheEntry . hash === hashDigest ) {
409415 // We can reuse the cached source
410- return this . _cachedSource ;
416+ return cacheEntry . source ;
411417 }
412418
413419 const source = this . generator . generate (
414420 this ,
415421 dependencyTemplates ,
416- runtimeTemplate
422+ runtimeTemplate ,
423+ type
417424 ) ;
418425
419426 const cachedSource = new CachedSource ( source ) ;
420- this . _cachedSource = cachedSource ;
421- this . _cachedSourceHash = hashDigest ;
427+ this . _cachedSources . set ( type , {
428+ source : cachedSource ,
429+ hash : hashDigest
430+ } ) ;
422431 return cachedSource ;
423432 }
424433
0 commit comments