@@ -23,35 +23,17 @@ export default class Roots extends EventEmitter {
23
23
const id = this . _id ( )
24
24
const compiler = webpack ( this . config )
25
25
26
- compiler . run ( ( err , stats ) => {
27
- if ( err ) {
28
- return this . emit ( 'error' , new RootsError ( { id : id , message : err } ) )
29
- }
30
-
31
- // Webpack "soft errors" are classified as warnings in roots. An error is
32
- // an error. If it doesn't break the build, it's a warning.
33
- const jsonStats = stats . toJson ( )
34
- if ( jsonStats . errors . length > 0 ) {
35
- this . emit ( 'warning' , new RootsWarning ( { id : id , message : jsonStats . errors } ) )
36
- }
37
- if ( jsonStats . warnings . length > 0 ) {
38
- this . emit ( 'warning' , new RootsWarning ( { id : id , message : jsonStats . warnings } ) )
39
- }
40
-
41
- this . emit ( 'compile' , { id : id , stats : stats } )
42
- } )
26
+ compiler . run ( compileCallback . bind ( this , id ) )
43
27
44
28
// Returns the compilation's ID synchronously, this can be checked against
45
29
// events emitted from the project instance.
46
30
return [ id , compiler ]
47
31
}
48
32
49
- watch ( opts ) {
50
- const [ , compiler ] = this . compile ( )
51
- return compiler . watch ( opts , ( err , stats ) => {
52
- if ( err ) { return this . emit ( 'error' , err ) }
53
- this . emit ( 'compile' , { stats : stats } )
54
- } )
33
+ watch ( opts = { } ) {
34
+ const id = this . _id ( )
35
+ const compiler = webpack ( this . config )
36
+ return compiler . watch ( opts , compileCallback . bind ( this , id ) )
55
37
}
56
38
57
39
clean ( ) {
@@ -117,3 +99,21 @@ export default class Roots extends EventEmitter {
117
99
function npmInstall ( opts ) {
118
100
return node . call ( exec , 'npm install' , { cwd : opts . root } )
119
101
}
102
+
103
+ function compileCallback ( id , err , stats ) {
104
+ if ( err ) {
105
+ return this . emit ( 'error' , new RootsError ( { id : id , message : err } ) )
106
+ }
107
+
108
+ // Webpack "soft errors" are classified as warnings in roots. An error is
109
+ // an error. If it doesn't break the build, it's a warning.
110
+ const jsonStats = stats . toJson ( )
111
+ if ( jsonStats . errors . length ) {
112
+ this . emit ( 'warning' , new RootsWarning ( { id : id , message : jsonStats . errors } ) )
113
+ }
114
+ if ( jsonStats . warnings . length ) {
115
+ this . emit ( 'warning' , new RootsWarning ( { id : id , message : jsonStats . warnings } ) )
116
+ }
117
+
118
+ this . emit ( 'compile' , { id : id , stats : stats } )
119
+ }
0 commit comments