File tree Expand file tree Collapse file tree 2 files changed +53
-3
lines changed Expand file tree Collapse file tree 2 files changed +53
-3
lines changed Original file line number Diff line number Diff line change @@ -66,3 +66,40 @@ app.use(webpackMiddleware(webpack({
66
66
// options for formating the statistics
67
67
}));
68
68
```
69
+
70
+ ## Advanced API
71
+
72
+ This part shows how you might interact with the middleware during runtime:
73
+
74
+ * ` close(callback) ` - stop watching for file changes
75
+ ```js
76
+ var webpackDevMiddlewareInstance = webpackMiddleware(/* see example usage */);
77
+ app.use(webpackDevMiddlewareInstance);
78
+ // After 10 seconds stop watching for file changes:
79
+ setTimeout(function(){
80
+ webpackDevMiddlewareInstance.close();
81
+ }, 10000);
82
+ ```
83
+
84
+ * ` invalidate() ` - recompile the bundle - e.g. after you changed the configuration
85
+ ```js
86
+ var compiler = webpack(/* see example usage */);
87
+ var webpackDevMiddlewareInstance = webpackMiddleware(compiler);
88
+ app.use(webpackDevMiddlewareInstance);
89
+ setTimeout(function(){
90
+ // After a short delay the configuration is changed
91
+ // in this example we will just add a banner plugin:
92
+ compiler.apply(new webpack.BannerPlugin('A new banner'));
93
+ // Recompile the bundle with the banner plugin:
94
+ webpackDevMiddlewareInstance.invalidate();
95
+ }, 1000);
96
+ ```
97
+
98
+ * ` waitUntilValid(callback) ` - executes the ` callback ` if the bundle is valid or after it is valid again:
99
+ ```js
100
+ var webpackDevMiddlewareInstance = webpackMiddleware(/* see example usage */);
101
+ app.use(webpackDevMiddlewareInstance);
102
+ webpackDevMiddleware.waitUntilValid(function(){
103
+ console.log('Package is in a valid state');
104
+ });
105
+ ```
Original file line number Diff line number Diff line change @@ -95,7 +95,7 @@ module.exports = function(compiler, options) {
95
95
function ready ( fn , req ) {
96
96
if ( state ) return fn ( ) ;
97
97
if ( ! options . noInfo && ! options . quiet )
98
- console . log ( "webpack: wait until bundle finished: " + req . url ) ;
98
+ console . log ( "webpack: wait until bundle finished: " + ( req . url || fn . name ) ) ;
99
99
callbacks . push ( fn ) ;
100
100
}
101
101
@@ -193,9 +193,22 @@ module.exports = function(compiler, options) {
193
193
194
194
webpackDevMiddleware . getFilenameFromUrl = getFilenameFromUrl ;
195
195
196
- webpackDevMiddleware . invalidate = function ( ) {
197
- if ( watching ) watching . invalidate ( ) ;
196
+ webpackDevMiddleware . waitUntilValid = function ( callback ) {
197
+ callback = callback || function ( ) { } ;
198
+ if ( ! watching || ! watching . running ) callback ( ) ;
199
+ else ready ( callback , { } ) ;
198
200
} ;
201
+
202
+ webpackDevMiddleware . invalidate = function ( callback ) {
203
+ callback = callback || function ( ) { } ;
204
+ if ( watching ) {
205
+ ready ( callback , { } ) ;
206
+ watching . invalidate ( ) ;
207
+ } else {
208
+ callback ( ) ;
209
+ }
210
+ } ;
211
+
199
212
webpackDevMiddleware . close = function ( callback ) {
200
213
callback = callback || function ( ) { } ;
201
214
if ( watching ) watching . close ( callback ) ;
You can’t perform that action at this time.
0 commit comments