This commit is contained in:
Tobias Koppers 2012-05-02 14:06:42 +02:00
parent 95e89267f4
commit e96c8369bb
5 changed files with 83 additions and 52 deletions

View File

@ -141,7 +141,49 @@ if(!output) {
if(!options.outputDirectory) options.outputDirectory = path.dirname(output);
if(!options.output) options.output = path.basename(output);
if(!options.outputPostfix) options.outputPostfix = "." + path.basename(output);
var events = webpack(input, options, function(err, stats) {
if(argv.progress) {
if(!options.events) options.events = new (require("events").EventEmitter)();
var events = options.events;
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.stderr.write("\b");
process.stderr.write(msg);
chars = msg.length;
}
events.on("task", function(name) {
sum++;
print();
});
events.on("task-end", function(name) {
finished++;
if(name) {
for(var i = 0; i < chars; i++)
process.stderr.write("\b \b");
process.stderr.write(name + " " + c("\033[1m\033[32m") + "done" + c("\033[39m\033[22m") + "\n");
chars = 0;
}
print();
});
events.on("bundle", function(name) {
sum = 0;
finished = 0;
for(var i = 0; i < chars; i++)
process.stderr.write("\b \b");
chars = 0;
});
}
webpack(input, options, function(err, stats) {
if(err) {
console.error(err);
return;
@ -232,35 +274,4 @@ if(!output) {
}
}
});
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();
});
}
}

View File

@ -6,6 +6,7 @@ var parse = require("./parse");
var resolve = require("./resolve");
var fs = require("fs");
var path = require("path");
var assert = require("assert");
/**
* context: current directory
@ -31,11 +32,11 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
nextChunkId: 0,
chunkModules: {} // used by checkObsolete
}
options.events.emit("task", "build modules");
options.events.emit("task", "build chunks");
options.events.emit("task", "optimize");
options.events.emit("task", "cleanup");
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) {
if(err) {
callback(err);
@ -45,18 +46,18 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
buildTree();
});
function buildTree() {
options.events.emit("task-end");
options.events.emit("task-end", "build modules");
addChunk(depTree, depTree.modulesById[mainModuleId], options);
createRealIds(depTree, options);
options.events.emit("task-end");
options.events.emit("task-end", "build chunks");
for(var chunkId in depTree.chunks) {
removeParentsModules(depTree, depTree.chunks[chunkId]);
removeChunkIfEmpty(depTree, depTree.chunks[chunkId]);
checkObsolete(depTree, depTree.chunks[chunkId]);
}
options.events.emit("task-end");
options.events.emit("task-end", "optimize");
// cleanup
delete depTree.chunkModules;
@ -66,7 +67,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
delete depTree.nextModuleId;
delete depTree.nextChunkId;
// return
options.events.emit("task-end");
options.events.emit("task-end", "cleanup");
callback(null, depTree);
}
}
@ -93,8 +94,9 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
return;
}
if(loaderFunctions.length > 0) {
var async = false;
var done = false;
try {
var async = false;
var context = {
request: request,
filenames: filenames,
@ -114,6 +116,8 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
},
callback: function() {
async = true;
assert(!done);
done = true;
nextLoader.apply(null, arguments);
},
web: true,
@ -123,10 +127,18 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
options: options
};
var retVal = loaderFunctions.pop().apply(context, args);
if(!async)
if(!async) {
done = true;
nextLoader(retVal === undefined ? new Error("loader did not return a value") : null, retVal);
}
} catch(e) {
callback("Loader throwed exeception: " + e);
if(!done) {
done = true;
callback("Loader throwed exeception: " + e);
} else {
if(e.stack) console.error(e.stack);
else console.error(e);
}
return;
}
} else {
@ -270,6 +282,7 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
endOne();
function endOne() {
count--;
assert(count >= 0);
if(count === 0) {
if(errors.length) {
callback(errors.join("\n"));
@ -328,6 +341,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
errors.push(err);
}
count--;
assert(count >= 0);
if(count == 0) {
if(errors.length > 0)
done(errors.join("\n"));

View File

@ -71,7 +71,6 @@ module.exports = function(context, moduleName, options, callback) {
}
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;
@ -136,12 +135,15 @@ function webpack(context, moduleName, options, finalCallback) {
options.resolve.loaders.push({test: /\.jade$/, loader: "jade"});
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
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);
}
options.events.emit("task", "create ouput directory");
options.events.emit("task", "prepare chunks");
options.events.emit("task", "statistics");
buildDeps(context, moduleName, options, function(err, depTree) {
function callback(err, result) {
options.events.emit("task-end", "statistics");
finalCallback(err, result);
}
if(err) {
callback(err);
return;
@ -249,6 +251,7 @@ function webpack(context, moduleName, options, finalCallback) {
});
fileModulesMap[path.basename(filename)] = modulesArray;
});
options.events.emit("task-end", "prepare chunks");
options.events.emit("start-writing", hash);
// write files
var remFiles = fileWrites.length;
@ -278,12 +281,15 @@ function webpack(context, moduleName, options, finalCallback) {
});
}
createDir(outDir, function(err) {
options.events.emit("task-end", "create ouput directory");
if(err) return callback(err);
writeFiles();
});
function writeFiles() {
fileWrites.forEach(function(writeAction) {
options.events.emit("task", "write " + writeAction[0]);
fs.writeFile(writeAction[0].replace(HASH_REGEXP, hash), writeAction[1], "utf-8", function(err) {
options.events.emit("task-end", "write " + writeAction[0]);
if(err) throw err;
remFiles--;
if(remFiles === 0)

View File

@ -175,8 +175,8 @@ module.exports = function(module, options, toRealId) {
}
result = [
"eval(",
JSON.stringify(result.join("")),
"\n\n// WEBPACK FOOTER //\n",
JSON.stringify(result),
");\n\n// WEBPACK FOOTER //\n",
"// module.id = ", module.id, "\n",
"// module.realId = ", module.realId, "\n",
"// module.chunks = ", module.chunks.join(", "), "\n",

View File

@ -1,6 +1,6 @@
{
"name": "webpack",
"version": "0.3.9",
"version": "0.3.10",
"author": "Tobias Koppers @sokra",
"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.",
"dependencies": {