@@ -14,166 +14,14 @@ const AsyncSeriesHook = require("tapable").AsyncSeriesHook;
1414
1515const Compilation = require ( "./Compilation" ) ;
1616const Stats = require ( "./Stats" ) ;
17+ const Watching = require ( "./Watching" ) ;
1718const NormalModuleFactory = require ( "./NormalModuleFactory" ) ;
1819const ContextModuleFactory = require ( "./ContextModuleFactory" ) ;
1920const ResolverFactory = require ( "./ResolverFactory" ) ;
2021
2122const RequestShortener = require ( "./RequestShortener" ) ;
2223const makePathsRelative = require ( "./util/identifier" ) . makePathsRelative ;
2324
24- class Watching {
25- constructor ( compiler , watchOptions , handler ) {
26- this . startTime = null ;
27- this . invalid = false ;
28- this . handler = handler ;
29- this . callbacks = [ ] ;
30- this . closed = false ;
31- if ( typeof watchOptions === "number" ) {
32- this . watchOptions = {
33- aggregateTimeout : watchOptions
34- } ;
35- } else if ( watchOptions && typeof watchOptions === "object" ) {
36- this . watchOptions = Object . assign ( { } , watchOptions ) ;
37- } else {
38- this . watchOptions = { } ;
39- }
40- this . watchOptions . aggregateTimeout = this . watchOptions . aggregateTimeout || 200 ;
41- this . compiler = compiler ;
42- this . running = true ;
43- this . compiler . readRecords ( err => {
44- if ( err ) return this . _done ( err ) ;
45-
46- this . _go ( ) ;
47- } ) ;
48- }
49-
50- _go ( ) {
51- this . startTime = Date . now ( ) ;
52- this . running = true ;
53- this . invalid = false ;
54- this . compiler . hooks . watchRun . callAsync ( this . compiler , err => {
55- if ( err ) return this . _done ( err ) ;
56- const onCompiled = ( err , compilation ) => {
57- if ( err ) return this . _done ( err ) ;
58- if ( this . invalid ) return this . _done ( ) ;
59-
60- if ( this . compiler . hooks . shouldEmit . call ( compilation ) === false ) {
61- return this . _done ( null , compilation ) ;
62- }
63-
64- this . compiler . emitAssets ( compilation , err => {
65- if ( err ) return this . _done ( err ) ;
66- if ( this . invalid ) return this . _done ( ) ;
67-
68- this . compiler . emitRecords ( err => {
69- if ( err ) return this . _done ( err ) ;
70-
71- if ( compilation . hooks . needAdditionalPass . call ( ) ) {
72- compilation . needAdditionalPass = true ;
73-
74- const stats = new Stats ( compilation ) ;
75- stats . startTime = this . startTime ;
76- stats . endTime = Date . now ( ) ;
77- this . compiler . hooks . done . call ( stats ) ;
78-
79- this . compiler . hooks . additionalPass . callAsync ( err => {
80- if ( err ) return this . _done ( err ) ;
81- this . compiler . compile ( onCompiled ) ;
82- } ) ;
83- return ;
84- }
85- return this . _done ( null , compilation ) ;
86- } ) ;
87- } ) ;
88- } ;
89- this . compiler . compile ( onCompiled ) ;
90- } ) ;
91- }
92-
93- _getStats ( compilation ) {
94- const stats = new Stats ( compilation ) ;
95- stats . startTime = this . startTime ;
96- stats . endTime = Date . now ( ) ;
97- return stats ;
98- }
99-
100- _done ( err , compilation ) {
101- this . running = false ;
102- if ( this . invalid ) return this . _go ( ) ;
103-
104- const stats = compilation ? this . _getStats ( compilation ) : null ;
105- if ( err ) {
106- this . compiler . hooks . failed . call ( err ) ;
107- this . handler ( err , stats ) ;
108- return ;
109- }
110-
111- this . compiler . hooks . done . call ( stats ) ;
112- this . handler ( null , stats ) ;
113- if ( ! this . closed ) {
114- this . watch ( Array . from ( compilation . fileDependencies ) , Array . from ( compilation . contextDependencies ) , Array . from ( compilation . missingDependencies ) ) ;
115- }
116- this . callbacks . forEach ( cb => cb ( ) ) ;
117- this . callbacks . length = 0 ;
118- }
119-
120- watch ( files , dirs , missing ) {
121- this . pausedWatcher = null ;
122- this . watcher = this . compiler . watchFileSystem . watch ( files , dirs , missing , this . startTime , this . watchOptions , ( err , filesModified , contextModified , missingModified , fileTimestamps , contextTimestamps ) => {
123- this . pausedWatcher = this . watcher ;
124- this . watcher = null ;
125- if ( err ) return this . handler ( err ) ;
126-
127- this . compiler . fileTimestamps = fileTimestamps ;
128- this . compiler . contextTimestamps = contextTimestamps ;
129- this . invalidate ( ) ;
130- } , ( fileName , changeTime ) => {
131- this . compiler . hooks . invalid . call ( fileName , changeTime ) ;
132- } ) ;
133- }
134-
135- invalidate ( callback ) {
136- if ( callback ) {
137- this . callbacks . push ( callback ) ;
138- }
139- if ( this . watcher ) {
140- this . pausedWatcher = this . watcher ;
141- this . watcher . pause ( ) ;
142- this . watcher = null ;
143- }
144- if ( this . running ) {
145- this . invalid = true ;
146- return false ;
147- } else {
148- this . _go ( ) ;
149- }
150- }
151-
152- close ( callback ) {
153- if ( callback === undefined ) callback = ( ) => { } ;
154-
155- this . closed = true ;
156- if ( this . watcher ) {
157- this . watcher . close ( ) ;
158- this . watcher = null ;
159- }
160- if ( this . pausedWatcher ) {
161- this . pausedWatcher . close ( ) ;
162- this . pausedWatcher = null ;
163- }
164- if ( this . running ) {
165- this . invalid = true ;
166- this . _done = ( ) => {
167- this . compiler . hooks . watchClose . call ( ) ;
168- callback ( ) ;
169- } ;
170- } else {
171- this . compiler . hooks . watchClose . call ( ) ;
172- callback ( ) ;
173- }
174- }
175- }
176-
17725class Compiler extends Tapable {
17826 constructor ( context ) {
17927 super ( ) ;
@@ -331,8 +179,7 @@ class Compiler extends Tapable {
331179 watch ( watchOptions , handler ) {
332180 this . fileTimestamps = { } ;
333181 this . contextTimestamps = { } ;
334- const watching = new Watching ( this , watchOptions , handler ) ;
335- return watching ;
182+ return new Watching ( this , watchOptions , handler ) ;
336183 }
337184
338185 run ( callback ) {
@@ -626,5 +473,4 @@ class Compiler extends Tapable {
626473 }
627474}
628475
629- Compiler . Watching = Watching ;
630476module . exports = Compiler ;
0 commit comments