Skip to content

Commit e96c836

Browse files
committed
bugfixes
1 parent 95e8926 commit e96c836

5 files changed

Lines changed: 83 additions & 52 deletions

File tree

bin/webpack.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,49 @@ if(!output) {
141141
if(!options.outputDirectory) options.outputDirectory = path.dirname(output);
142142
if(!options.output) options.output = path.basename(output);
143143
if(!options.outputPostfix) options.outputPostfix = "." + path.basename(output);
144-
var events = webpack(input, options, function(err, stats) {
144+
if(argv.progress) {
145+
if(!options.events) options.events = new (require("events").EventEmitter)();
146+
var events = options.events;
147+
148+
var sum = 0;
149+
var finished = 0;
150+
var chars = 0;
151+
function print() {
152+
var msg = "";
153+
if(sum > 0) {
154+
msg += "compiling... (" + c("\033[1m\033[33m");
155+
msg += sprintf("%4s", finished+"") + "/" + sprintf("%4s", sum+"");
156+
msg += " " + sprintf("%4s", Math.floor(finished*100/sum)+"%");
157+
msg += c("\033[39m\033[22m") + ")";
158+
}
159+
for(var i = 0; i < chars; i++)
160+
process.stderr.write("\b");
161+
process.stderr.write(msg);
162+
chars = msg.length;
163+
}
164+
events.on("task", function(name) {
165+
sum++;
166+
print();
167+
});
168+
events.on("task-end", function(name) {
169+
finished++;
170+
if(name) {
171+
for(var i = 0; i < chars; i++)
172+
process.stderr.write("\b \b");
173+
process.stderr.write(name + " " + c("\033[1m\033[32m") + "done" + c("\033[39m\033[22m") + "\n");
174+
chars = 0;
175+
}
176+
print();
177+
});
178+
events.on("bundle", function(name) {
179+
sum = 0;
180+
finished = 0;
181+
for(var i = 0; i < chars; i++)
182+
process.stderr.write("\b \b");
183+
chars = 0;
184+
});
185+
}
186+
webpack(input, options, function(err, stats) {
145187
if(err) {
146188
console.error(err);
147189
return;
@@ -232,35 +274,4 @@ if(!output) {
232274
}
233275
}
234276
});
235-
if(argv.progress) {
236-
var sum = 0;
237-
var finished = 0;
238-
var chars = 0;
239-
function print() {
240-
var msg = "";
241-
if(sum > 0) {
242-
msg += "compiling... (" + c("\033[1m\033[33m");
243-
msg += sprintf("%4s", finished+"") + "/" + sprintf("%4s", sum+"");
244-
msg += " " + sprintf("%4s", Math.floor(finished*100/sum)+"%");
245-
msg += c("\033[39m\033[22m") + ")";
246-
}
247-
for(var i = 0; i < chars; i++)
248-
process.stdout.write("\b");
249-
process.stdout.write(msg);
250-
chars = msg.length;
251-
}
252-
events.on("task", function() {
253-
sum++;
254-
print();
255-
});
256-
events.on("task-end", function() {
257-
finished++;
258-
print();
259-
});
260-
events.on("bundle", function() {
261-
sum = 0;
262-
finished = 0;
263-
print();
264-
});
265-
}
266277
}

lib/buildDeps.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var parse = require("./parse");
66
var resolve = require("./resolve");
77
var fs = require("fs");
88
var path = require("path");
9+
var assert = require("assert");
910

1011
/**
1112
* context: current directory
@@ -31,11 +32,11 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
3132
nextChunkId: 0,
3233
chunkModules: {} // used by checkObsolete
3334
}
35+
options.events.emit("task", "build modules");
36+
options.events.emit("task", "build chunks");
37+
options.events.emit("task", "optimize");
38+
options.events.emit("task", "cleanup");
3439
var mainModuleId;
35-
options.events.emit("task");
36-
options.events.emit("task");
37-
options.events.emit("task");
38-
options.events.emit("task");
3940
addModule(depTree, context, mainModule, options, {type: "main"}, function(err, id) {
4041
if(err) {
4142
callback(err);
@@ -45,18 +46,18 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
4546
buildTree();
4647
});
4748
function buildTree() {
48-
options.events.emit("task-end");
49+
options.events.emit("task-end", "build modules");
4950

5051
addChunk(depTree, depTree.modulesById[mainModuleId], options);
5152
createRealIds(depTree, options);
52-
options.events.emit("task-end");
53+
options.events.emit("task-end", "build chunks");
5354

5455
for(var chunkId in depTree.chunks) {
5556
removeParentsModules(depTree, depTree.chunks[chunkId]);
5657
removeChunkIfEmpty(depTree, depTree.chunks[chunkId]);
5758
checkObsolete(depTree, depTree.chunks[chunkId]);
5859
}
59-
options.events.emit("task-end");
60+
options.events.emit("task-end", "optimize");
6061

6162
// cleanup
6263
delete depTree.chunkModules;
@@ -66,7 +67,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
6667
delete depTree.nextModuleId;
6768
delete depTree.nextChunkId;
6869
// return
69-
options.events.emit("task-end");
70+
options.events.emit("task-end", "cleanup");
7071
callback(null, depTree);
7172
}
7273
}
@@ -93,8 +94,9 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
9394
return;
9495
}
9596
if(loaderFunctions.length > 0) {
97+
var async = false;
98+
var done = false;
9699
try {
97-
var async = false;
98100
var context = {
99101
request: request,
100102
filenames: filenames,
@@ -114,6 +116,8 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
114116
},
115117
callback: function() {
116118
async = true;
119+
assert(!done);
120+
done = true;
117121
nextLoader.apply(null, arguments);
118122
},
119123
web: true,
@@ -123,10 +127,18 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
123127
options: options
124128
};
125129
var retVal = loaderFunctions.pop().apply(context, args);
126-
if(!async)
130+
if(!async) {
131+
done = true;
127132
nextLoader(retVal === undefined ? new Error("loader did not return a value") : null, retVal);
133+
}
128134
} catch(e) {
129-
callback("Loader throwed exeception: " + e);
135+
if(!done) {
136+
done = true;
137+
callback("Loader throwed exeception: " + e);
138+
} else {
139+
if(e.stack) console.error(e.stack);
140+
else console.error(e);
141+
}
130142
return;
131143
}
132144
} else {
@@ -270,6 +282,7 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
270282
endOne();
271283
function endOne() {
272284
count--;
285+
assert(count >= 0);
273286
if(count === 0) {
274287
if(errors.length) {
275288
callback(errors.join("\n"));
@@ -328,6 +341,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
328341
errors.push(err);
329342
}
330343
count--;
344+
assert(count >= 0);
331345
if(count == 0) {
332346
if(errors.length > 0)
333347
done(errors.join("\n"));

lib/webpack.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ module.exports = function(context, moduleName, options, callback) {
7171
}
7272
if(!options.events) options.events = new (require("events").EventEmitter)();
7373
if(options.watch) {
74-
console.log("start watching...");
7574
var fs = require("fs");
7675
var watchers = [];
7776
var isRunning = true;
@@ -136,12 +135,15 @@ function webpack(context, moduleName, options, finalCallback) {
136135
options.resolve.loaders.push({test: /\.jade$/, loader: "jade"});
137136
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
138137
options.resolve.loaders.push({test: /\.less$/, loader: "style!less"});
139-
options.events.emit("task");
140-
function callback(err, result) {
141-
options.events.emit("task-end");
142-
finalCallback(err, result);
143-
}
138+
139+
options.events.emit("task", "create ouput directory");
140+
options.events.emit("task", "prepare chunks");
141+
options.events.emit("task", "statistics");
144142
buildDeps(context, moduleName, options, function(err, depTree) {
143+
function callback(err, result) {
144+
options.events.emit("task-end", "statistics");
145+
finalCallback(err, result);
146+
}
145147
if(err) {
146148
callback(err);
147149
return;
@@ -249,6 +251,7 @@ function webpack(context, moduleName, options, finalCallback) {
249251
});
250252
fileModulesMap[path.basename(filename)] = modulesArray;
251253
});
254+
options.events.emit("task-end", "prepare chunks");
252255
options.events.emit("start-writing", hash);
253256
// write files
254257
var remFiles = fileWrites.length;
@@ -278,12 +281,15 @@ function webpack(context, moduleName, options, finalCallback) {
278281
});
279282
}
280283
createDir(outDir, function(err) {
284+
options.events.emit("task-end", "create ouput directory");
281285
if(err) return callback(err);
282286
writeFiles();
283287
});
284288
function writeFiles() {
285289
fileWrites.forEach(function(writeAction) {
290+
options.events.emit("task", "write " + writeAction[0]);
286291
fs.writeFile(writeAction[0].replace(HASH_REGEXP, hash), writeAction[1], "utf-8", function(err) {
292+
options.events.emit("task-end", "write " + writeAction[0]);
287293
if(err) throw err;
288294
remFiles--;
289295
if(remFiles === 0)

lib/writeSource.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ module.exports = function(module, options, toRealId) {
175175
}
176176
result = [
177177
"eval(",
178-
JSON.stringify(result.join("")),
179-
"\n\n// WEBPACK FOOTER //\n",
178+
JSON.stringify(result),
179+
");\n\n// WEBPACK FOOTER //\n",
180180
"// module.id = ", module.id, "\n",
181181
"// module.realId = ", module.realId, "\n",
182182
"// module.chunks = ", module.chunks.join(", "), "\n",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.3.9",
3+
"version": "0.3.10",
44
"author": "Tobias Koppers @sokra",
55
"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.",
66
"dependencies": {

0 commit comments

Comments
 (0)