2424'use strict' ;
2525
2626// 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 */
2930/** @typedef {{hash: string, entry: any, content: string } } ChildCompilationResultEntry */
30- /** @typedef {import("./file-watcher-api").Snapshot } Snapshot */
3131/** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]} } FileDependencies */
3232/** @typedef {{
3333 dependencies: FileDependencies,
3838}} ChildCompilationResult */
3939
4040const { HtmlWebpackChildCompiler } = require ( './child-compiler' ) ;
41- const fileWatcherApi = require ( './file-watcher-api' ) ;
4241
4342/**
4443 * This plugin is a singleton for performance reasons.
4544 * To keep track if a plugin does already exist for the compiler they are cached
4645 * in this map
47- * @type {WeakMap<WebpackCompiler , PersistentChildCompilerSingletonPlugin> }}
46+ * @type {WeakMap<Compiler , PersistentChildCompilerSingletonPlugin> }}
4847 */
4948const compilerMap = new WeakMap ( ) ;
5049
5150class CachedChildCompilation {
5251 /**
53- * @param {WebpackCompiler } compiler
52+ * @param {Compiler } compiler
5453 */
5554 constructor ( compiler ) {
5655 /**
5756 * @private
58- * @type {WebpackCompiler }
57+ * @type {Compiler }
5958 */
6059 this . compiler = compiler ;
6160 // Create a singleton instance for the compiler
@@ -114,6 +113,60 @@ class CachedChildCompilation {
114113}
115114
116115class 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+
117170 constructor ( ) {
118171 /**
119172 * @private
@@ -158,7 +211,7 @@ class PersistentChildCompilerSingletonPlugin {
158211
159212 /**
160213 * apply is called by the webpack main compiler during the start phase
161- * @param {WebpackCompiler } compiler
214+ * @param {Compiler } compiler
162215 */
163216 apply ( compiler ) {
164217 /** @type Promise<ChildCompilationResult> */
@@ -216,7 +269,7 @@ class PersistentChildCompilerSingletonPlugin {
216269 // this might possibly cause bugs if files were changed inbetween
217270 // compilation start and snapshot creation
218271 compiledEntriesPromise . then ( ( childCompilationResult ) => {
219- return fileWatcherApi . createSnapshot ( childCompilationResult . dependencies , mainCompilation , compilationStartTime ) ;
272+ return PersistentChildCompilerSingletonPlugin . createSnapshot ( childCompilationResult . dependencies , mainCompilation , compilationStartTime ) ;
220273 } ) . then ( ( snapshot ) => {
221274 previousFileSystemSnapshot = snapshot ;
222275 } ) ;
@@ -309,7 +362,7 @@ class PersistentChildCompilerSingletonPlugin {
309362 * Verify that the cache is still valid
310363 * @private
311364 * @param {Snapshot | undefined } snapshot
312- * @param {WebpackCompilation } mainCompilation
365+ * @param {Compilation } mainCompilation
313366 * @returns {Promise<boolean> }
314367 */
315368 isCacheValid ( snapshot , mainCompilation ) {
@@ -328,14 +381,14 @@ class PersistentChildCompilerSingletonPlugin {
328381 if ( ! snapshot ) {
329382 return Promise . resolve ( false ) ;
330383 }
331- return fileWatcherApi . isSnapShotValid ( snapshot , mainCompilation ) ;
384+ return PersistentChildCompilerSingletonPlugin . isSnapshotValid ( snapshot , mainCompilation ) ;
332385 }
333386
334387 /**
335388 * Start to compile all templates
336389 *
337390 * @private
338- * @param {WebpackCompilation } mainCompilation
391+ * @param {Compilation } mainCompilation
339392 * @param {string[] } entries
340393 * @returns {Promise<ChildCompilationResult> }
341394 */
@@ -366,11 +419,11 @@ class PersistentChildCompilerSingletonPlugin {
366419
367420 /**
368421 * @private
369- * @param {WebpackCompilation } mainCompilation
422+ * @param {Compilation } mainCompilation
370423 * @param {FileDependencies } files
371424 */
372425 watchFiles ( mainCompilation , files ) {
373- fileWatcherApi . watchFiles ( mainCompilation , files ) ;
426+ PersistentChildCompilerSingletonPlugin . watchFiles ( mainCompilation , files ) ;
374427 }
375428}
376429
0 commit comments