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.outputDirectory) options.outputDirectory = path.dirname(output);
if(!options.output) options.output = path.basename(output); if(!options.output) options.output = path.basename(output);
if(!options.outputPostfix) options.outputPostfix = "." + 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) { if(err) {
console.error(err); console.error(err);
return; 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 resolve = require("./resolve");
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var assert = require("assert");
/** /**
* context: current directory * context: current directory
@ -31,11 +32,11 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
nextChunkId: 0, nextChunkId: 0,
chunkModules: {} // used by checkObsolete 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; 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) { addModule(depTree, context, mainModule, options, {type: "main"}, function(err, id) {
if(err) { if(err) {
callback(err); callback(err);
@ -45,18 +46,18 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
buildTree(); buildTree();
}); });
function buildTree() { function buildTree() {
options.events.emit("task-end"); options.events.emit("task-end", "build modules");
addChunk(depTree, depTree.modulesById[mainModuleId], options); addChunk(depTree, depTree.modulesById[mainModuleId], options);
createRealIds(depTree, options); createRealIds(depTree, options);
options.events.emit("task-end"); options.events.emit("task-end", "build chunks");
for(var chunkId in depTree.chunks) { for(var chunkId in depTree.chunks) {
removeParentsModules(depTree, depTree.chunks[chunkId]); removeParentsModules(depTree, depTree.chunks[chunkId]);
removeChunkIfEmpty(depTree, depTree.chunks[chunkId]); removeChunkIfEmpty(depTree, depTree.chunks[chunkId]);
checkObsolete(depTree, depTree.chunks[chunkId]); checkObsolete(depTree, depTree.chunks[chunkId]);
} }
options.events.emit("task-end"); options.events.emit("task-end", "optimize");
// cleanup // cleanup
delete depTree.chunkModules; delete depTree.chunkModules;
@ -66,7 +67,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
delete depTree.nextModuleId; delete depTree.nextModuleId;
delete depTree.nextChunkId; delete depTree.nextChunkId;
// return // return
options.events.emit("task-end"); options.events.emit("task-end", "cleanup");
callback(null, depTree); callback(null, depTree);
} }
} }
@ -93,8 +94,9 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
return; return;
} }
if(loaderFunctions.length > 0) { if(loaderFunctions.length > 0) {
try {
var async = false; var async = false;
var done = false;
try {
var context = { var context = {
request: request, request: request,
filenames: filenames, filenames: filenames,
@ -114,6 +116,8 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
}, },
callback: function() { callback: function() {
async = true; async = true;
assert(!done);
done = true;
nextLoader.apply(null, arguments); nextLoader.apply(null, arguments);
}, },
web: true, web: true,
@ -123,10 +127,18 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
options: options options: options
}; };
var retVal = loaderFunctions.pop().apply(context, args); 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); nextLoader(retVal === undefined ? new Error("loader did not return a value") : null, retVal);
}
} catch(e) { } catch(e) {
if(!done) {
done = true;
callback("Loader throwed exeception: " + e); callback("Loader throwed exeception: " + e);
} else {
if(e.stack) console.error(e.stack);
else console.error(e);
}
return; return;
} }
} else { } else {
@ -270,6 +282,7 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
endOne(); endOne();
function endOne() { function endOne() {
count--; count--;
assert(count >= 0);
if(count === 0) { if(count === 0) {
if(errors.length) { if(errors.length) {
callback(errors.join("\n")); callback(errors.join("\n"));
@ -328,6 +341,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
errors.push(err); errors.push(err);
} }
count--; count--;
assert(count >= 0);
if(count == 0) { if(count == 0) {
if(errors.length > 0) if(errors.length > 0)
done(errors.join("\n")); 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.events) options.events = new (require("events").EventEmitter)();
if(options.watch) { if(options.watch) {
console.log("start watching...");
var fs = require("fs"); var fs = require("fs");
var watchers = []; var watchers = [];
var isRunning = true; 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: /\.jade$/, loader: "jade"});
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"}); options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
options.resolve.loaders.push({test: /\.less$/, loader: "style!less"}); options.resolve.loaders.push({test: /\.less$/, loader: "style!less"});
options.events.emit("task");
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) { function callback(err, result) {
options.events.emit("task-end"); options.events.emit("task-end", "statistics");
finalCallback(err, result); finalCallback(err, result);
} }
buildDeps(context, moduleName, options, function(err, depTree) {
if(err) { if(err) {
callback(err); callback(err);
return; return;
@ -249,6 +251,7 @@ function webpack(context, moduleName, options, finalCallback) {
}); });
fileModulesMap[path.basename(filename)] = modulesArray; fileModulesMap[path.basename(filename)] = modulesArray;
}); });
options.events.emit("task-end", "prepare chunks");
options.events.emit("start-writing", hash); options.events.emit("start-writing", hash);
// write files // write files
var remFiles = fileWrites.length; var remFiles = fileWrites.length;
@ -278,12 +281,15 @@ function webpack(context, moduleName, options, finalCallback) {
}); });
} }
createDir(outDir, function(err) { createDir(outDir, function(err) {
options.events.emit("task-end", "create ouput directory");
if(err) return callback(err); if(err) return callback(err);
writeFiles(); writeFiles();
}); });
function writeFiles() { function writeFiles() {
fileWrites.forEach(function(writeAction) { 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) { 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; if(err) throw err;
remFiles--; remFiles--;
if(remFiles === 0) if(remFiles === 0)

View File

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

View File

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