webpack
Advanced tools
Comparing version 0.3.8 to 0.3.9
@@ -58,2 +58,10 @@ #!/usr/bin/env node | ||
.boolean("watch") | ||
.describe("watch", "Recompiles on changes (except loaders)") | ||
.default("watch", false) | ||
.boolean("progress") | ||
.describe("progress", "Displays a progress while compiling") | ||
.default("progress", false) | ||
.demand(1) | ||
@@ -90,2 +98,6 @@ .argv; | ||
if(argv.watch) { | ||
options.watch = true; | ||
} | ||
if(argv.filenames) { | ||
@@ -113,3 +125,7 @@ options.includeFilenames = true; | ||
if(argv.single) { | ||
function c(str) { | ||
return argv.colors ? str : ""; | ||
} | ||
if(!output) { | ||
webpack(input, options, function(err, source) { | ||
@@ -131,3 +147,3 @@ if(err) { | ||
if(!options.outputPostfix) options.outputPostfix = "." + path.basename(output); | ||
webpack(input, options, function(err, stats) { | ||
var events = webpack(input, options, function(err, stats) { | ||
if(err) { | ||
@@ -140,5 +156,2 @@ console.error(err); | ||
else { | ||
function c(str) { | ||
return argv.colors ? str : ""; | ||
} | ||
console.log("Hash: "+c("\033[1m") + stats.hash + c("\033[22m")); | ||
@@ -227,2 +240,33 @@ console.log("Chunks: "+c("\033[1m") + stats.chunkCount + c("\033[22m")); | ||
}); | ||
if(argv.progress) { | ||
var sum = 0; | ||
var finished = 0; | ||
var chars = 0; | ||
function print() { | ||
var msg = ""; | ||
if(sum > 0) { | ||
msg += "compiling... (" + c("\033[1m\033[33m"); | ||
msg += sprintf("%4s", finished+"") + "/" + sprintf("%4s", sum+""); | ||
msg += " " + sprintf("%4s", Math.floor(finished*100/sum)+"%"); | ||
msg += c("\033[39m\033[22m") + ")"; | ||
} | ||
for(var i = 0; i < chars; i++) | ||
process.stdout.write("\b"); | ||
process.stdout.write(msg); | ||
chars = msg.length; | ||
} | ||
events.on("task", function() { | ||
sum++; | ||
print(); | ||
}); | ||
events.on("task-end", function() { | ||
finished++; | ||
print(); | ||
}); | ||
events.on("bundle", function() { | ||
sum = 0; | ||
finished = 0; | ||
print(); | ||
}); | ||
} | ||
} |
@@ -22,2 +22,3 @@ /* | ||
if(!options) options = {}; | ||
if(!options.events) options.events = { emit: function() {} }; | ||
@@ -35,2 +36,6 @@ var depTree = { | ||
var mainModuleId; | ||
options.events.emit("task"); | ||
options.events.emit("task"); | ||
options.events.emit("task"); | ||
options.events.emit("task"); | ||
addModule(depTree, context, mainModule, options, {type: "main"}, function(err, id) { | ||
@@ -45,4 +50,8 @@ if(err) { | ||
function buildTree() { | ||
options.events.emit("task-end"); | ||
addChunk(depTree, depTree.modulesById[mainModuleId], options); | ||
createRealIds(depTree, options); | ||
options.events.emit("task-end"); | ||
for(var chunkId in depTree.chunks) { | ||
@@ -53,2 +62,4 @@ removeParentsModules(depTree, depTree.chunks[chunkId]); | ||
} | ||
options.events.emit("task-end"); | ||
// cleanup | ||
@@ -62,2 +73,3 @@ delete depTree.chunkModules; | ||
// return | ||
options.events.emit("task-end"); | ||
callback(null, depTree); | ||
@@ -135,3 +147,9 @@ } | ||
function addModule(depTree, context, modu, options, reason, callback) { | ||
function addModule(depTree, context, modu, options, reason, finalCallback) { | ||
options.events.emit("task"); | ||
function callback(err, result) { | ||
options.events.emit("task-end"); | ||
finalCallback(err, result); | ||
} | ||
resolve(context || path.dirname(modu), modu, options.resolve, resolved); | ||
@@ -156,2 +174,3 @@ function resolved(err, filename) { | ||
filename = loaders.pop(); | ||
options.events.emit("module", modu, filename); | ||
fs.readFile(filename, "utf-8", function(err, content) { | ||
@@ -275,3 +294,9 @@ if(err) { | ||
function addContextModule(depTree, context, contextModuleName, options, reason, callback) { | ||
function addContextModule(depTree, context, contextModuleName, options, reason, finalCallback) { | ||
options.events.emit("task"); | ||
function callback(err, result) { | ||
options.events.emit("task-end"); | ||
finalCallback(err, result); | ||
} | ||
resolve.context(context, contextModuleName, options.resolve, resolved); | ||
@@ -299,2 +324,3 @@ function resolved(err, dirname) { | ||
dirname = loaders.pop(); | ||
options.events.emit("context", contextModule, dirname); | ||
var prependLoaders = loaders.length === 0 ? "" : loaders.join("!") + "!"; | ||
@@ -301,0 +327,0 @@ var extensions = (options.resolve && options.resolve.extensions) || [".web.js", ".js"]; |
@@ -72,2 +72,47 @@ /* | ||
} | ||
if(!options.events) options.events = new (require("events").EventEmitter)(); | ||
if(options.watch) { | ||
console.log("start watching..."); | ||
var fs = require("fs"); | ||
var watchers = []; | ||
var isRunning = true; | ||
var runAgain = false; | ||
function startAgain() { | ||
watchers.forEach(function(watcher) { | ||
watcher.close(); | ||
}); | ||
watchers.length = 0; | ||
isRunning = true; | ||
setTimeout(function() { | ||
runAgain = false; | ||
webpack(context, moduleName, options, callback); | ||
}, 200); | ||
} | ||
function change() { | ||
if(isRunning) | ||
runAgain = true; | ||
else | ||
startAgain() | ||
} | ||
options.events.on("module", function(module, filename) { | ||
if(!filename) return; | ||
var w = fs.watch(filename, function() { | ||
change(); | ||
}); | ||
}); | ||
options.events.on("context", function(module, dirname) { | ||
if(!dirname) return; | ||
fs.watch(dirname, function() { | ||
change(); | ||
}); | ||
}); | ||
options.events.on("bundle", function(stats) { | ||
isRunning = false; | ||
if(runAgain) | ||
startAgain(); | ||
}); | ||
} | ||
return webpack(context, moduleName, options, callback); | ||
} | ||
function webpack(context, moduleName, options, finalCallback) { | ||
options.parse = options.parse || {}; | ||
@@ -95,2 +140,7 @@ options.parse.overwrites = options.parse.overwrites || {}; | ||
options.resolve.loaders.push({test: /\.less$/, loader: "style!less"}); | ||
options.events.emit("task"); | ||
function callback(err, result) { | ||
options.events.emit("task-end"); | ||
finalCallback(err, result); | ||
} | ||
buildDeps(context, moduleName, options, function(err, depTree) { | ||
@@ -204,2 +254,3 @@ if(err) { | ||
}); | ||
options.events.emit("start-writing", hash); | ||
// write files | ||
@@ -271,2 +322,3 @@ var remFiles = fileWrites.length; | ||
buffer.fileModules = fileModulesMap; | ||
options.events.emit("bundle", buffer); | ||
callback(null, buffer); | ||
@@ -293,2 +345,3 @@ } | ||
}); | ||
return options.events; | ||
} | ||
@@ -295,0 +348,0 @@ |
{ | ||
"name": "webpack", | ||
"version": "0.3.8", | ||
"version": "0.3.9", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -5,0 +5,0 @@ "description": "Packs CommonJs 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.", |
@@ -10,4 +10,18 @@ /* | ||
argv.shift(); | ||
var extraArgs = argv.join(" "); | ||
var extraArgs = argv; | ||
function bindOutput(p) { | ||
p.stdout.on("data", function(data) { | ||
process.stdout.write(data); | ||
}); | ||
p.stderr.on("data", function(data) { | ||
process.stderr.write(data); | ||
}); | ||
} | ||
function join(a, b) { | ||
a = a.slice(0); | ||
Array.prototype.push.apply(a, b); | ||
return a; | ||
} | ||
try { | ||
@@ -25,24 +39,19 @@ require("vm-browserify"); | ||
console.log("compile scripts..."); | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --single --libary libary1 node_modules/libary1 js/libary1.js", function (error, stdout, stderr) { | ||
console.log('libary1 stdout:\n' + stdout); | ||
console.log('libary1 stderr:\n ' + stderr); | ||
if (error !== null) { | ||
console.log('libary1 error: ' + error); | ||
var extraArgsNoWatch = extraArgs.slice(0); | ||
var watchIndex = extraArgsNoWatch.indexOf("--watch"); | ||
if(watchIndex != -1) extraArgsNoWatch.splice(watchIndex, 1); | ||
var libary1 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--single", "--libary", "libary1", | ||
"node_modules/libary1", "js/libary1.js"], extraArgsNoWatch)); | ||
bindOutput(libary1); | ||
libary1.on("exit", function(code) { | ||
if(code === 0) { | ||
var main = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--alias", "vm=vm-browserify", | ||
"--script-src-prefix", "js/", "lib/index", "js/web.js"], extraArgs)); | ||
bindOutput(main); | ||
} | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --alias vm=vm-browserify --script-src-prefix js/ lib/index js/web.js", function (error, stdout, stderr) { | ||
console.log('web stdout:\n' + stdout); | ||
console.log('web stderr:\n ' + stderr); | ||
if (error !== null) { | ||
console.log('web error: ' + error); | ||
} | ||
}); | ||
}); | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --script-src-prefix js/ --libary libary2 node_modules/libary2 js/libary2.js", function (error, stdout, stderr) { | ||
console.log('libary2 stdout:\n' + stdout); | ||
console.log('libary2 stderr:\n ' + stderr); | ||
if (error !== null) { | ||
console.log('libary2 error: ' + error); | ||
} | ||
}); | ||
var libary2 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--libary", "libary2", | ||
"--script-src-prefix", "js/", "node_modules/libary2", "js/libary2.js"], extraArgs)); | ||
bindOutput(libary2); | ||
} |
338444
9470
59