Skip to content

Commit b1abc23

Browse files
charlespwdSpaceK33z
authored andcommitted
Add watch-content-base option (#649)
Fixes #350
1 parent b75ca49 commit b1abc23

File tree

9 files changed

+109
-0
lines changed

9 files changed

+109
-0
lines changed

bin/webpack-dev-server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ yargs.options({
110110
describe: "A directory or URL to serve HTML content from.",
111111
group: RESPONSE_GROUP
112112
},
113+
"watch-content-base": {
114+
type: "boolean",
115+
describe: "Enable live-reloading of the content-base.",
116+
group: RESPONSE_GROUP
117+
},
113118
"history-api-fallback": {
114119
type: "boolean",
115120
describe: "Fallback to /index.html for Single Page Applications.",
@@ -213,6 +218,9 @@ function processOptions(wpOpt) {
213218
}
214219
}
215220

221+
if(argv["watch-content-base"])
222+
options.watchContentBase = true;
223+
216224
if(!options.stats) {
217225
options.stats = {
218226
cached: false,

client/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ var onSocketMsg = {
6363
if(initial) return initial = false;
6464
reloadApp();
6565
},
66+
"content-changed": function() {
67+
log("info", "[WDS] Content base changed. Reloading...")
68+
self.location.reload();
69+
},
6670
warnings: function(warnings) {
6771
log("info", "[WDS] Warnings while compiling.");
6872
for(var i = 0; i < warnings.length; i++)

examples/watch-content-base/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Watch Content Base
2+
3+
## Watching a single directory
4+
5+
```shell
6+
node ../../bin/webpack-dev-server.js --content-base assets --watch-content-base
7+
```
8+
9+
### What should happen
10+
11+
The script should open `http://localhost:8080/`. In the app you should see "Does it work?"
12+
13+
In your editor, edit `assets/index.html`, and save your changes. The app should reload.
14+
15+
16+
## Watching an array of directories
17+
18+
```javascript
19+
// webpack.conf.js
20+
module.exports = {
21+
/* ... */
22+
devServer: {
23+
contentBase: [
24+
"assets",
25+
"css",
26+
]
27+
}
28+
}
29+
```
30+
31+
```shell
32+
node ../../bin/webpack-dev-server.js --watch-content-base
33+
```
34+
35+
### What should happen
36+
37+
The script should open `http://localhost:8080/`. In the app you should see "Does it work?"
38+
39+
In your editor, edit `assets/index.html`, and save your changes. The app should reload.
40+
41+
In your editor, edit `css/styles.csss`, and save your changes. The app should reload.

examples/watch-content-base/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var myText = document.getElementById("mytext");
2+
myText.textContent = "Does it work? yes";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8">
5+
</head>
6+
<body>
7+
<h1>Watch content base</h1>
8+
<div id="mytext"></div>
9+
<script src="bundle.js" type="text/javascript" charset="utf-8"></script>
10+
</body>
11+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: blue;
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
context: __dirname,
3+
entry: "./app.js",
4+
devServer: {
5+
contentBase: [
6+
"assets",
7+
"css",
8+
]
9+
}
10+
}

lib/Server.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require("fs");
2+
var chokidar = require("chokidar");
23
var path = require("path");
34
var webpackDevMiddleware = require("webpack-dev-middleware");
45
var express = require("express");
@@ -22,6 +23,7 @@ function Server(compiler, options) {
2223
this.headers = options.headers;
2324
this.clientLogLevel = options.clientLogLevel;
2425
this.sockets = [];
26+
this.contentBaseWatchers = [];
2527

2628
// Listening for events
2729
var invalidPlugin = function() {
@@ -269,6 +271,18 @@ function Server(compiler, options) {
269271
}
270272
},
271273

274+
watchContentBase: function() {
275+
if(/^(https?:)?\/\//.test(contentBase) || typeof contentBase === "number") {
276+
throw new Error("Watching remote files is not supported.");
277+
} else if(Array.isArray(contentBase)) {
278+
contentBase.forEach(function(item) {
279+
this._watch(item);
280+
}.bind(this));
281+
} else {
282+
this._watch(contentBase);
283+
}
284+
}.bind(this),
285+
272286
middleware: function() {
273287
// include our middleware to ensure it is able to handle '/index.html' request after redirect
274288
app.use(this.middleware);
@@ -293,6 +307,8 @@ function Server(compiler, options) {
293307
defaultFeatures.push("proxy", "middleware");
294308
if(contentBase !== false)
295309
defaultFeatures.push("contentBaseFiles");
310+
if(options.watchContentBase)
311+
defaultFeatures.push("watchContentBase");
296312
if(options.historyApiFallback)
297313
defaultFeatures.push("historyApiFallback", "middleware");
298314
defaultFeatures.push("magicHtml");
@@ -396,6 +412,11 @@ Server.prototype.close = function(callback) {
396412
this.listeningApp.close(function() {
397413
this.middleware.close(callback);
398414
}.bind(this));
415+
416+
this.contentBaseWatchers.forEach(function(watcher) {
417+
watcher.close();
418+
});
419+
this.contentBaseWatchers = [];
399420
}
400421

401422
Server.prototype.sockWrite = function(sockets, type, data) {
@@ -445,6 +466,14 @@ Server.prototype._sendStats = function(sockets, stats, force) {
445466
this.sockWrite(sockets, "ok");
446467
}
447468

469+
Server.prototype._watch = function(path) {
470+
var watcher = chokidar.watch(path).on("change", function() {
471+
this.sockWrite(this.sockets, "content-changed");
472+
}.bind(this))
473+
474+
this.contentBaseWatchers.push(watcher);
475+
}
476+
448477
Server.prototype.invalidate = function() {
449478
if(this.middleware) this.middleware.invalidate();
450479
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"webpack": "^2.1.0-beta"
88
},
99
"dependencies": {
10+
"chokidar": "^1.6.0",
1011
"compression": "^1.5.2",
1112
"connect-history-api-fallback": "^1.3.0",
1213
"express": "^4.13.3",

0 commit comments

Comments
 (0)