Skip to content

Commit 4f55fa7

Browse files
committed
Merge pull request #72 from bluedaniel/master
waitUntilValid function using ready function
2 parents 4525806 + 513c050 commit 4f55fa7

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,40 @@ app.use(webpackMiddleware(webpack({
6666
// options for formating the statistics
6767
}));
6868
```
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+
```

middleware.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = function(compiler, options) {
9595
function ready(fn, req) {
9696
if(state) return fn();
9797
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));
9999
callbacks.push(fn);
100100
}
101101

@@ -193,9 +193,22 @@ module.exports = function(compiler, options) {
193193

194194
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl;
195195

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, {});
198200
};
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+
199212
webpackDevMiddleware.close = function(callback) {
200213
callback = callback || function(){};
201214
if(watching) watching.close(callback);

0 commit comments

Comments
 (0)