24
24
'use strict' ;
25
25
26
26
// Import types
27
- /**@typedef {import("webpack/lib/Compiler.js") } WebpackCompiler */
28
- /**@typedef {import("webpack/lib/Compilation.js") } WebpackCompilation */
27
+ /**@typedef {import("webpack/lib/Compiler.js") } Compiler */
28
+ /**@typedef {import("webpack/lib/Compilation.js") } Compilation */
29
+ /**@typedef {import("webpack/lib/FileSystemInfo").Snapshot } Snapshot */
29
30
/**@typedef {{hash: string, entry: any, content: string } } ChildCompilationResultEntry */
30
- /**@typedef {import("./file-watcher-api").Snapshot } Snapshot */
31
31
/**@typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]} } FileDependencies */
32
32
/**@typedef {{
33
33
dependencies: FileDependencies,
38
38
}} ChildCompilationResult */
39
39
40
40
const { HtmlWebpackChildCompiler} = require ( './child-compiler' ) ;
41
- const fileWatcherApi = require ( './file-watcher-api' ) ;
42
41
43
42
/**
44
43
* This plugin is a singleton for performance reasons.
45
44
* To keep track if a plugin does already exist for the compiler they are cached
46
45
* in this map
47
- *@type {WeakMap<WebpackCompiler , PersistentChildCompilerSingletonPlugin> }}
46
+ *@type {WeakMap<Compiler , PersistentChildCompilerSingletonPlugin> }}
48
47
*/
49
48
const compilerMap = new WeakMap ( ) ;
50
49
51
50
class CachedChildCompilation {
52
51
/**
53
- *@param {WebpackCompiler } compiler
52
+ *@param {Compiler } compiler
54
53
*/
55
54
constructor ( compiler ) {
56
55
/**
57
56
*@private
58
- *@type {WebpackCompiler }
57
+ *@type {Compiler }
59
58
*/
60
59
this . compiler = compiler ;
61
60
// Create a singleton instance for the compiler
@@ -114,6 +113,60 @@ class CachedChildCompilation {
114
113
}
115
114
116
115
class PersistentChildCompilerSingletonPlugin {
116
+ /**
117
+ *
118
+ *@param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]} } fileDependencies
119
+ *@param {Compilation } mainCompilation
120
+ *@param {number } startTime
121
+ */
122
+ static createSnapshot ( fileDependencies , mainCompilation , startTime ) {
123
+ return new Promise ( ( resolve , reject ) => {
124
+ mainCompilation . fileSystemInfo . createSnapshot (
125
+ startTime ,
126
+ fileDependencies . fileDependencies ,
127
+ fileDependencies . contextDependencies ,
128
+ fileDependencies . missingDependencies ,
129
+ null ,
130
+ ( err , snapshot ) => {
131
+ if ( err ) {
132
+ return reject ( err ) ;
133
+ }
134
+ resolve ( snapshot ) ;
135
+ }
136
+ ) ;
137
+ } ) ;
138
+ }
139
+
140
+ /**
141
+ * Returns true if the files inside this snapshot
142
+ * have not been changed
143
+ *
144
+ *@param {Snapshot } snapshot
145
+ *@param {Compilation } mainCompilation
146
+ *@returns {Promise<boolean> }
147
+ */
148
+ static isSnapshotValid ( snapshot , mainCompilation ) {
149
+ return new Promise ( ( resolve , reject ) => {
150
+ mainCompilation . fileSystemInfo . checkSnapshotValid (
151
+ snapshot ,
152
+ ( err , isValid ) => {
153
+ if ( err ) {
154
+ reject ( err ) ;
155
+ }
156
+ resolve ( isValid ) ;
157
+ }
158
+ ) ;
159
+ } ) ;
160
+ }
161
+
162
+ static watchFiles ( mainCompilation , fileDependencies ) {
163
+ Object . keys ( fileDependencies ) . forEach ( ( depencyTypes ) => {
164
+ fileDependencies [ depencyTypes ] . forEach ( fileDependency => {
165
+ mainCompilation [ depencyTypes ] . add ( fileDependency ) ;
166
+ } ) ;
167
+ } ) ;
168
+ }
169
+
117
170
constructor ( ) {
118
171
/**
119
172
*@private
@@ -158,7 +211,7 @@ class PersistentChildCompilerSingletonPlugin {
158
211
159
212
/**
160
213
* apply is called by the webpack main compiler during the start phase
161
- *@param {WebpackCompiler } compiler
214
+ *@param {Compiler } compiler
162
215
*/
163
216
apply ( compiler ) {
164
217
/**@type Promise<ChildCompilationResult> */
@@ -216,7 +269,7 @@ class PersistentChildCompilerSingletonPlugin {
216
269
// this might possibly cause bugs if files were changed inbetween
217
270
// compilation start and snapshot creation
218
271
compiledEntriesPromise . then ( ( childCompilationResult ) => {
219
- return fileWatcherApi . createSnapshot ( childCompilationResult . dependencies , mainCompilation , compilationStartTime ) ;
272
+ return PersistentChildCompilerSingletonPlugin . createSnapshot ( childCompilationResult . dependencies , mainCompilation , compilationStartTime ) ;
220
273
} ) . then ( ( snapshot ) => {
221
274
previousFileSystemSnapshot = snapshot ;
222
275
} ) ;
@@ -309,7 +362,7 @@ class PersistentChildCompilerSingletonPlugin {
309
362
* Verify that the cache is still valid
310
363
*@private
311
364
*@param {Snapshot | undefined } snapshot
312
- *@param {WebpackCompilation } mainCompilation
365
+ *@param {Compilation } mainCompilation
313
366
*@returns {Promise<boolean> }
314
367
*/
315
368
isCacheValid ( snapshot , mainCompilation ) {
@@ -328,14 +381,14 @@ class PersistentChildCompilerSingletonPlugin {
328
381
if ( ! snapshot ) {
329
382
return Promise . resolve ( false ) ;
330
383
}
331
- return fileWatcherApi . isSnapShotValid ( snapshot , mainCompilation ) ;
384
+ return PersistentChildCompilerSingletonPlugin . isSnapshotValid ( snapshot , mainCompilation ) ;
332
385
}
333
386
334
387
/**
335
388
* Start to compile all templates
336
389
*
337
390
*@private
338
- *@param {WebpackCompilation } mainCompilation
391
+ *@param {Compilation } mainCompilation
339
392
*@param {string[] } entries
340
393
*@returns {Promise<ChildCompilationResult> }
341
394
*/
@@ -366,11 +419,11 @@ class PersistentChildCompilerSingletonPlugin {
366
419
367
420
/**
368
421
*@private
369
- *@param {WebpackCompilation } mainCompilation
422
+ *@param {Compilation } mainCompilation
370
423
*@param {FileDependencies } files
371
424
*/
372
425
watchFiles ( mainCompilation , files ) {
373
- fileWatcherApi . watchFiles ( mainCompilation , files ) ;
426
+ PersistentChildCompilerSingletonPlugin . watchFiles ( mainCompilation , files ) ;
374
427
}
375
428
}
376
429