Skip to content

Add watch-content-base option to Webpack-1 #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var optimist = require("optimist")

.string("content-base").describe("content-base", "A directory or URL to serve HTML content from.")

.boolean("watch-content-base").describe("watch-content-base", "Enable live-reloading of the content-base.")

.string("content-base-target").describe("content-base-target", "Proxy requests to this target.")

.boolean("history-api-fallback").describe("history-api-fallback", "Fallback to /index.html for Single Page Applications.")
Expand Down Expand Up @@ -125,6 +127,9 @@ if(argv["content-base"]) {
options.contentBase = process.cwd();
}

if(argv["watch-content-base"])
options.watchContentBase = true

if(!options.stats) {
options.stats = {
cached: false,
Expand Down
64 changes: 64 additions & 0 deletions build_pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

# This is the script run by buildbot
# (not by any of eventshopper or eventshopper-pull-requests instances)
# It produces an outdeploy-able package, and does not runs tests.

# START Common expectations for all build scripts.
PACKAGE=$1
BRANCH=$2
TAG=$3
RELEASETYPE=$4

if [ -z "$PACKAGE" ] || [ -z "$BRANCH" ] || [ -z "$TAG" ] || [ -z "$RELEASETYPE" ]; then
echo "Usage: $0 package branch tag releasetype"
echo "where:"
echo " - 'package' is the name of the GitHub project. Ex: 'eventshopper'"
echo " - 'branch' is the branch of the GitHub project. Ex: 'master'"
echo " - 'tag' is the release tag. Ex: '1.2.3'"
echo " - 'releasetype' is either 'release', 'prerelease', or 'push'"
exit 1
fi

echo "Parameters: $PACKAGE $BRANCH $TAG $RELEASETYPE"
echo "Packaging $PACKAGE-$TAG for Outlaunch"

set -x

# We use node version manager to use the right version of node depending on
# the version specified in .nvmrc
NVM_DIR="/home/buildbot/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
nvm install
NVM_RESULTS=$?
set -e

rm -rf ./node_modules
RM_RESULTS=$?

# gyp requires Python >= 2.4, and the default on our build machine is only 2.4
npm cache clean
npm install --python="/opt/outbox/python2.7/bin/python"
INSTALL_RESULTS=$?

# build the project files
npm run prepublish
BUILD_RESULTS=$?

# move project files as specified by project.json to prepare for compression
mkdir -p build
cp lib ssl client bin build

# Matching structure of regular `setup.py`ed Python packages, having a root
# folder level describing package-version.
tar cvfz "$PACKAGE-$TAG.tar.gz" build
COMPRESS_RESULTS=$?

# cleanup
rm -rf build

# put package in dist
mkdir -p dist
mv "$PACKAGE-$TAG.tar.gz" dist

exit $((NVM_RESULTS || INSTALL_RESULTS || RM_RESULTS || BUILD_RESULTS || COMPRESS_RESULTS))
2 changes: 2 additions & 0 deletions buildbot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
build_script_path=./build_pkg.sh
4 changes: 4 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ var onSocketMsg = {
if(initial) return initial = false;
reloadApp();
},
"content-changed": function() {
log("info", "[WDS] Content base changed. Reloading...")
self.location.reload();
},
warnings: function(warnings) {
log("info", "[WDS] Warnings while compiling.");
for(var i = 0; i < warnings.length; i++)
Expand Down
11 changes: 11 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ http://localhost:8080/some/url/from/spa
```

The contents of /index.html is served.

## Watching `devServer.contentBase`

``` text
webpack-dev-server --inline --content-base assets --watch-content-base
http://localhost:8080/
```

Try to update `assets/index.html`.

The browser should reflect your changes.
11 changes: 11 additions & 0 deletions example/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8">
</head>
<body>
<h1>Watch content base</h1>
<div id="mytext"></div>
<script src="bundle.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require("fs");
var chokidar = require("chokidar");
var path = require("path");
var webpackDevMiddleware = require("webpack-dev-middleware");
var express = require("express");
Expand All @@ -23,6 +24,7 @@ function Server(compiler, options) {
this.headers = options.headers;
this.clientLogLevel = options.clientLogLevel;
this.sockets = [];
this.contentBaseWatchers = [];

// Listening for events
var invalidPlugin = function() {
Expand Down Expand Up @@ -247,6 +249,19 @@ function Server(compiler, options) {
}
}.bind(this),

watchContentBase: function() {
var contentBase = options.contentBase || process.cwd();
if(/^(https?:)?\/\//.test(contentBase) || typeof contentBase === "number") {
throw new Error("Watching remote files is not supported.");
} else if(Array.isArray(contentBase)) {
contentBase.forEach(function(item) {
this._watch(item);
}.bind(this));
} else {
this._watch(contentBase);
}
}.bind(this),

middleware: function() {
// include our middleware to ensure it is able to handle '/index.html' request after redirect
app.use(this.middleware);
Expand Down Expand Up @@ -274,6 +289,8 @@ function Server(compiler, options) {
defaultFeatures.push("magicHtml");
if(options.contentBase !== false)
defaultFeatures.push("contentBase");
if(options.watchContentBase)
defaultFeatures.push("watchContentBase");
// compress is placed last and uses unshift so that it will be the first middleware used
if(options.compress)
defaultFeatures.unshift("compress");
Expand Down Expand Up @@ -362,6 +379,10 @@ Server.prototype.close = function() {
this.sockets = [];
this.middleware.close();
this.listeningApp.close();
this.contentBaseWatchers.forEach(function(watcher) {
watcher.close();
});
this.contentBaseWatchers = [];
}

Server.prototype.sockWrite = function(sockets, type, data) {
Expand Down Expand Up @@ -409,6 +430,14 @@ Server.prototype._sendStats = function(sockets, stats, force) {
this.sockWrite(sockets, "ok");
}

Server.prototype._watch = function(path) {
var watcher = chokidar.watch(path).on("change", function() {
this.sockWrite(this.sockets, "content-changed");
}.bind(this))

this.contentBaseWatchers.push(watcher);
}

Server.prototype.invalidate = function() {
if(this.middleware) this.middleware.invalidate();
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"webpack": ">=1.3.0 <3"
},
"dependencies": {
"chokidar": "^1.6.0",
"compression": "^1.5.2",
"connect-history-api-fallback": "^1.3.0",
"express": "^4.13.3",
Expand Down