Skip to content

Commit 2fdf166

Browse files
committed
Support for webpack-dev-middleware, and tests
1 parent 8c0ab9c commit 2fdf166

File tree

9 files changed

+84
-45
lines changed

9 files changed

+84
-45
lines changed

.gitignore

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/node_modules
2-
/test/js
3-
/test/browsertest/js
4-
/test/browsertest/node_modules/vm-browserify
5-
/examples/*/js
1+
node_modules
2+
test/js
3+
test/browsertest/js
4+
examples/*/js

.npmignore

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/node_modules
2-
/test/js
3-
/test/browsertest/js
4-
/test/browsertest/node_modules/vm-browserify
5-
/examples
1+
node_modules
2+
test/js
3+
test/browsertest/js
4+
examples
65
README.md

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ You can also save this options object in a JSON file and use it with the shell c
409409
// "module" (module, filename) before a module is loaded
410410
// "context" (module, dirname) before a context is loaded
411411
// "dependency" (filename) before a dependency is loaded
412+
// "loader" (filename) before a loader is required
412413
// -- events for progress --
413414
// "task" (name?) start of a task
414415
// "task-end" (name?) end of a task

lib/execLoaders.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ module.exports = function(context, request, loaders, filenames, contents, cacheE
2424
callback(null, contents, true);
2525
} else {
2626
// try to load all loaders
27-
// TODO this doesn't reload the loader if it has changed
28-
// TODO to support watch mode better, fix that
2927
loaderFunctions = [];
3028
try {
3129
loaders.forEach(function(name) {
32-
var loader = require(name);
30+
var loaderFilename = require.resolve(name);
31+
options.events.emit("loader", loaderFilename);
32+
33+
// require loader in fresh context
34+
var oldCache = {};
35+
for(var entry in require.cache) {
36+
oldCache[entry] = require.cache[entry];
37+
delete require.cache[entry];
38+
}
39+
var loader = require(loaderFilename);
40+
for(var entry in oldCache) {
41+
require.cache[entry] = oldCache[entry];
42+
}
43+
3344
loaderFunctions.push(loader);
3445
});
3546
} catch(e) {

lib/webpack.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ module.exports = function(context, moduleName, options, callback) {
203203
}));
204204
});
205205

206+
// on before a loader is read
207+
options.events.on("loader", function(filename) {
208+
if(!filename) return;
209+
watchers.push(fs.watch(filename, function() {
210+
change();
211+
}));
212+
});
213+
214+
// on user defines the bundle as invalid
215+
options.events.on("invalid", function() {
216+
change();
217+
});
218+
206219
// on bundle finished compiling
207220
options.events.on("bundle", function(stats) {
208221
isRunning = false;

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.5.8",
3+
"version": "0.5.9",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loading of js, json, jade, coffee, css, ... out of the box and more with custom loaders.",
66
"dependencies": {
@@ -29,7 +29,10 @@
2929
],
3030
"devDependencies": {
3131
"mocha": "*",
32-
"should": "*"
32+
"should": "*",
33+
"vm-browserify": "*",
34+
"express": "*",
35+
"webpack-dev-middleware": "0.5.x"
3336
},
3437
"engines": {
3538
"node": ">=0.1.30"

test/browsertest/build.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,21 @@ function join(a, b) {
2323
return a;
2424
}
2525

26-
try {
27-
require("vm-browserify");
28-
compile();
29-
} catch(e) {
30-
console.log("install vm-browserify...");
31-
cp.exec("npm install vm-browserify", function (error, stdout, stderr) {
32-
console.log(stdout);
33-
compile();
34-
});
35-
}
36-
function compile() {
37-
console.log("compile scripts...");
26+
console.log("compile scripts...");
3827

39-
var extraArgsNoWatch = extraArgs.slice(0);
40-
var watchIndex = extraArgsNoWatch.indexOf("--watch");
41-
if(watchIndex != -1) extraArgsNoWatch.splice(watchIndex, 1);
42-
var libary1 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--single", "--libary", "libary1",
43-
"node_modules/libary1", "js/libary1.js"], extraArgsNoWatch));
44-
bindOutput(libary1);
45-
libary1.on("exit", function(code) {
46-
if(code === 0) {
47-
var main = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--alias", "vm=vm-browserify",
48-
"--public-prefix", "js/", "lib/index", "js/web.js"], extraArgs));
49-
bindOutput(main);
50-
}
51-
});
52-
var libary2 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--libary", "libary2",
53-
"--script-src-prefix", "js/", "--options", "libary2config.js", "node_modules/libary2", "js/libary2.js"], extraArgs));
54-
bindOutput(libary2);
55-
}
28+
var extraArgsNoWatch = extraArgs.slice(0);
29+
var watchIndex = extraArgsNoWatch.indexOf("--watch");
30+
if(watchIndex != -1) extraArgsNoWatch.splice(watchIndex, 1);
31+
var libary1 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--single", "--libary", "libary1",
32+
"node_modules/libary1", "js/libary1.js"], extraArgsNoWatch));
33+
bindOutput(libary1);
34+
libary1.on("exit", function(code) {
35+
if(code === 0) {
36+
var main = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--alias", "vm=vm-browserify",
37+
"--public-prefix", "js/", "lib/index", "js/web.js"], extraArgs));
38+
bindOutput(main);
39+
}
40+
});
41+
var libary2 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--libary", "libary2",
42+
"--script-src-prefix", "js/", "--options", "libary2config.js", "node_modules/libary2", "js/libary2.js"], extraArgs));
43+
bindOutput(libary2);

test/browsertest/lib/index.web.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ require("../css/stylesheet.css");
137137
require("../less/stylesheet.less");
138138

139139
// file loader
140-
window.test(require("file/png!../img/image.png").indexOf("js/") === 0, "Buildin 'file' loader, png");
140+
window.test(require("file/png!../img/image.png").indexOf("js/") >= 0, "Buildin 'file' loader, png");
141141
setTimeout(function() {
142142
document.getElementById("image").src = require("file/png!../img/image.png");
143143
}, 200);

test/browsertest/middlewareTest.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var webpackMiddleware = require("webpack-dev-middleware");
2+
var express = require("express");
3+
var path = require("path");
4+
5+
var app = express();
6+
7+
app.configure(function() {
8+
app.use(webpackMiddleware(path.join(__dirname, "lib", "index"), {
9+
publicPrefix: "http://localhost:8080/js/",
10+
watch: true,
11+
watchDelay: 5000,
12+
debug: true,
13+
output: "web.js",
14+
outputPostfix: ".web.js",
15+
resolve: {
16+
alias: {
17+
vm: "vm-browserify"
18+
}
19+
}
20+
}));
21+
app.use(express.static(path.join(__dirname)));
22+
23+
});
24+
25+
app.listen(8080);

0 commit comments

Comments
 (0)