Skip to content

Commit

Permalink
add a lazy mode on watch = false, which rebundles on every request.
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 14, 2013
1 parent af7673c commit 619f79d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ app.use(webpackMiddleware(/* context = */ __dirname, /* module = */ "./lib/file"
webpack: {
watch: true,
// This is not required, but recommendated to get the best out of the middleware.
// If you pass false, the middleware switch to lazy mode, which means rebundling
// every request. A cache is automatically attached in lazy mode.

publicPrefix: "http://localhost:8080/assets/",
// The prefix for request that should be handled.
Expand Down
30 changes: 27 additions & 3 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = function() {
// We need you own events to bind some stuff
wpOpt.events = wpOpt.events || new (require("events").EventEmitter)();

// We need a cache to cache also in lazy mode
wpOpt.cache = wpOpt.cache || new (require("webpack/lib/Cache"))();

// Grap files from webpack
wpOpt.emitFile = function(filename, content) {
files[filename] = content;
Expand Down Expand Up @@ -61,6 +64,12 @@ module.exports = function() {
cb();
});
});

// In lazy mode, we may issue another rebuild
if(forceRebuild) {
forceRebuild = false;
rebuild();
}
});

// on bundle invalidated
Expand All @@ -78,12 +87,24 @@ module.exports = function() {
});
webpack.apply(null, args);

function rebuild() {
if(state) {
state = false;
webpack.apply(null, args);
} else {
forceRebuild = true;
}
}

// store our files in memory
var files = {};

// the state, false: bundle invalid, true: bundle valid
var state = false;

// in lazy mode, rebuild automatically
var forceRebuild = false;

// delayed callback
var callbacks = [];

Expand All @@ -104,10 +125,13 @@ module.exports = function() {
}
// fast exit if another directory requested
if(req.url.indexOf(localPrefix) != 0) return next();
// else delay the request until we have a vaild bundle
// get filename from request
var filename = req.url.substr(localPrefix.length);
// in lazy mode, rebuild on bundle request
if(filename === wpOpt.output && !wpOpt.watch)
rebuild();
// delay the request until we have a vaild bundle
ready(function() {
// get filename from request
var filename = req.url.substr(localPrefix.length);
// check if it is a generated file
if(!(filename in files)) return next();

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-dev-middleware",
"version": "0.8.1",
"version": "0.8.2",
"author": "Tobias Koppers @sokra",
"description": "Offers a dev middleware for webpack, which arguments a live bundle to a directory",
"dependencies": {
Expand Down

0 comments on commit 619f79d

Please sign in to comment.