@@ -5,21 +5,24 @@ const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
5
5
const NodeTargetPlugin = require ( 'webpack/lib/node/NodeTargetPlugin' ) ;
6
6
const LoaderTargetPlugin = require ( 'webpack/lib/LoaderTargetPlugin' ) ;
7
7
const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
8
- const loaderUtils = require ( 'loader-utils' ) ;
9
8
10
9
11
10
interface CompilationOutput {
12
- hash : string ;
11
+ rendered : boolean ;
13
12
outputName : string ;
14
13
source : string ;
15
- newSource ?: string ;
14
+ }
15
+
16
+ interface CachedCompilation {
17
+ outputName : string ;
18
+ evaluatedSource ?: string ;
16
19
}
17
20
18
21
export class WebpackResourceLoader {
19
22
private _parentCompilation : any ;
20
23
private _context : string ;
21
24
private _uniqueId = 0 ;
22
- private _cache = new Map < string , CompilationOutput > ( ) ;
25
+ private _cache = new Map < string , CachedCompilation > ( ) ;
23
26
24
27
constructor ( ) { }
25
28
@@ -95,15 +98,13 @@ export class WebpackResourceLoader {
95
98
}
96
99
} ) ;
97
100
98
- const source = childCompilation . assets [ outputName ] . source ( ) ;
99
-
100
101
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 ,
103
104
// Output name.
104
105
outputName,
105
106
// Compiled code.
106
- source
107
+ source : childCompilation . assets [ outputName ] . source ( )
107
108
} ) ;
108
109
}
109
110
} ) ;
@@ -116,12 +117,12 @@ export class WebpackResourceLoader {
116
117
const vmScript = new vm . Script ( output . source , { filename : output . outputName } ) ;
117
118
118
119
// Evaluate code and cast to string
119
- let newSource : string ;
120
- newSource = vmScript . runInContext ( vmContext ) ;
120
+ let evaluatedSource : string ;
121
+ evaluatedSource = vmScript . runInContext ( vmContext ) ;
121
122
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 ) ;
125
126
}
126
127
127
128
return Promise . reject ( 'The loader "' + output . outputName + '" didn\'t return a string.' ) ;
@@ -132,17 +133,14 @@ export class WebpackResourceLoader {
132
133
133
134
get ( filePath : string ) : Promise < string > {
134
135
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 ) ;
146
144
}
147
145
}
148
146
0 commit comments