@@ -5,21 +5,24 @@ const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
55const NodeTargetPlugin = require ( 'webpack/lib/node/NodeTargetPlugin' ) ;
66const LoaderTargetPlugin = require ( 'webpack/lib/LoaderTargetPlugin' ) ;
77const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
8- const loaderUtils = require ( 'loader-utils' ) ;
98
109
1110interface CompilationOutput {
12- hash : string ;
11+ rendered : boolean ;
1312 outputName : string ;
1413 source : string ;
15- newSource ?: string ;
14+ }
15+
16+ interface CachedCompilation {
17+ outputName : string ;
18+ evaluatedSource ?: string ;
1619}
1720
1821export class WebpackResourceLoader {
1922 private _parentCompilation : any ;
2023 private _context : string ;
2124 private _uniqueId = 0 ;
22- private _cache = new Map < string , CompilationOutput > ( ) ;
25+ private _cache = new Map < string , CachedCompilation > ( ) ;
2326
2427 constructor ( ) { }
2528
@@ -95,15 +98,13 @@ export class WebpackResourceLoader {
9598 }
9699 } ) ;
97100
98- const source = childCompilation . assets [ outputName ] . source ( ) ;
99-
100101 resolve ( {
101- // Hash of the source .
102- hash : loaderUtils . getHashDigest ( source ) ,
102+ // Boolean showing if this entry was changed since the last compilation .
103+ rendered : entries [ 0 ] . rendered ,
103104 // Output name.
104105 outputName,
105106 // Compiled code.
106- source
107+ source : childCompilation . assets [ outputName ] . source ( )
107108 } ) ;
108109 }
109110 } ) ;
@@ -116,12 +117,12 @@ export class WebpackResourceLoader {
116117 const vmScript = new vm . Script ( output . source , { filename : output . outputName } ) ;
117118
118119 // Evaluate code and cast to string
119- let newSource : string ;
120- newSource = vmScript . runInContext ( vmContext ) ;
120+ let evaluatedSource : string ;
121+ evaluatedSource = vmScript . runInContext ( vmContext ) ;
121122
122- if ( typeof newSource == 'string' ) {
123- this . _cache . set ( output . outputName , { ... output , newSource } ) ;
124- return Promise . resolve ( newSource ) ;
123+ if ( typeof evaluatedSource == 'string' ) {
124+ this . _cache . set ( output . outputName , { outputName : output . outputName , evaluatedSource } ) ;
125+ return Promise . resolve ( evaluatedSource ) ;
125126 }
126127
127128 return Promise . reject ( 'The loader "' + output . outputName + '" didn\'t return a string.' ) ;
@@ -132,17 +133,14 @@ export class WebpackResourceLoader {
132133
133134 get ( filePath : string ) : Promise < string > {
134135 return this . _compile ( filePath )
135- . then ( ( result : any ) => {
136- // Check cache.
137- const outputName = result . outputName ;
138- const output = this . _cache . get ( outputName ) ;
139- if ( output ) {
140- if ( output . hash === result . hash && output . newSource ) {
141- // Return cached newSource.
142- return Promise . resolve ( output . newSource ) ;
143- } else {
144- // Delete cache entry.
145- this . _cache . delete ( outputName ) ;
136+ . then ( ( result : CompilationOutput ) => {
137+ if ( ! result . rendered ) {
138+ // Check cache.
139+ const outputName = result . outputName ;
140+ const cachedOutput = this . _cache . get ( outputName ) ;
141+ if ( cachedOutput ) {
142+ // Return cached evaluatedSource.
143+ return Promise . resolve ( cachedOutput . evaluatedSource ) ;
146144 }
147145 }
148146
0 commit comments