Skip to content

Commit f81c516

Browse files
committed
extracted formatOutput
1 parent ece331c commit f81c516

2 files changed

Lines changed: 103 additions & 86 deletions

File tree

‎bin/webpack.js‎

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ if(argv.alias) {
119119
}
120120

121121
var webpack = require("../lib/webpack.js");
122+
var formatOutput = require("../lib/formatOutput.js");
122123

123124
function c(str) {
124125
return argv.colors ? str : "";
@@ -191,92 +192,11 @@ if(!output) {
191192
if(argv.json)
192193
console.log(util.inspect(stats, false, 10, argv.colors));
193194
else {
194-
console.log("Hash: "+c("\033[1m") + stats.hash + c("\033[22m"));
195-
console.log("Chunks: "+c("\033[1m") + stats.chunkCount + c("\033[22m"));
196-
console.log("Modules: "+c("\033[1m") + stats.modulesCount + c("\033[22m"));
197-
console.log("Modules including duplicates: "+c("\033[1m") + stats.modulesIncludingDuplicates + c("\033[22m"));
198-
console.log("Modules pre chunk: "+c("\033[1m") + stats.modulesPerChunk + c("\033[22m"));
199-
console.log("Modules first chunk: "+c("\033[1m") + stats.modulesFirstChunk + c("\033[22m"));
200-
if(stats.fileSizes)
201-
Object.keys(stats.fileSizes).reverse().forEach(function(file) {
202-
console.log(c("\033[1m") + sprintf("%" + (5 + options.output.length) + "s", file) + c("\033[22m")+": "+c("\033[1m") + sprintf("%8d", stats.fileSizes[file]) + c("\033[22m") + " characters");
203-
});
204-
var cwd = process.cwd();
205-
var cwdParent = path.dirname(cwd);
206-
var buildins = path.join(__dirname, "..");
207-
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
208-
cwd = new RegExp("^" + cwd + "|(!)" + cwd, "g");
209-
var buildinsAsModule = cwd.test(buildins);
210-
cwdParent = cwdParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
211-
cwdParent = new RegExp("^" + cwdParent + "|(!)" + cwdParent, "g");
212-
buildins = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
213-
buildins = new RegExp("^" + buildins + "|(!)" + buildins, "g");
214-
var node_modulesRegExpA = /\/node_modules\//g;
215-
var node_modulesRegExpB = /\\node_modules\\/g;
216-
var index_jsRegExp = /[\\\/]index.js!/g;
217-
function compressFilename(filename) {
218-
if(!filename)
219-
return filename;
220-
if(buildinsAsModule)
221-
filename = filename.replace(buildins, "!(webpack)");
222-
filename = filename.replace(cwd, "!.");
223-
if(!buildinsAsModule)
224-
filename = filename.replace(buildins, "!(webpack)");
225-
filename = filename.replace(node_modulesRegExpA, "/~/");
226-
filename = filename.replace(node_modulesRegExpB, "\\~\\");
227-
filename = filename.replace(index_jsRegExp, "!");
228-
return filename.replace(/^!|!$/, "");
229-
}
230-
if(stats.fileModules) {
231-
console.log();
232-
console.log(" <id> <size> <filename>");
233-
if(argv.verbose)
234-
console.log(" <reason> from <filename>");
235-
Object.keys(stats.fileModules).reverse().forEach(function(file) {
236-
console.log(c("\033[1m\033[32m") + file + c("\033[39m\033[22m"));
237-
var modules = stats.fileModules[file];
238-
if(argv["by-size"])
239-
modules.sort(function(a, b) {
240-
return b.size - a.size;
241-
});
242-
modules.forEach(function(module) {
243-
console.log(" "+c("\033[1m") + sprintf("%3s", module.id+"") + " " + (typeof module.size === "number" ? sprintf("%9s", Math.round(module.size)+"") : " ") + " " +
244-
(compressFilename(module.filename) ||
245-
(module.dirname && ("[context] " + compressFilename(module.dirname))) ||
246-
"[unknown]") + c("\033[22m"));
247-
if(argv.verbose) {
248-
module.reasons.forEach(function(reason) {
249-
switch(reason.type) {
250-
case "require":
251-
console.log(" "+c("\033[36m")+"require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
252-
break;
253-
case "context":
254-
console.log(" "+c("\033[90m")+"context from " + compressFilename(reason.filename) + c("\033[39m"));
255-
break;
256-
case "async require":
257-
console.log(" "+c("\033[35m")+"async require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
258-
break;
259-
case "async context":
260-
console.log(" "+c("\033[35ma")+"sync context from " + compressFilename(reason.filename) + c("\033[39m"));
261-
break;
262-
default:
263-
console.log(" "+c("\033[31m") + reason.type + c("\033[39m"));
264-
}
265-
});
266-
}
267-
});
268-
});
269-
}
270-
if(stats.warnings) {
271-
stats.warnings.forEach(function(warning) {
272-
console.log(c("\033[1m\033[33m")+"WARNING: " + warning + c("\033[39m\033[22m"));
273-
});
274-
}
275-
if(stats.errors) {
276-
stats.errors.forEach(function(error) {
277-
console.log(c("\033[1m\033[31m")+"ERROR: " + error + c("\033[39m\033[22m"));
278-
});
279-
}
195+
console.log(formatOutput(stats, {
196+
colors: argv.colors,
197+
"by-size": argv["by-size"],
198+
verbose: argv.verbose
199+
}));
280200
}
281201
});
282202
}

‎lib/formatOutput.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var sprintf = require("sprintf").sprintf;
2+
var path = require("path");
3+
module.exports = function(stats, options) {
4+
var buf = [];
5+
6+
function c(str) {
7+
return options.colors ? str : "";
8+
}
9+
10+
buf.push("Hash: "+c("\033[1m") + stats.hash + c("\033[22m"));
11+
buf.push("Chunks: "+c("\033[1m") + stats.chunkCount + c("\033[22m"));
12+
buf.push("Modules: "+c("\033[1m") + stats.modulesCount + c("\033[22m"));
13+
buf.push("Modules including duplicates: "+c("\033[1m") + stats.modulesIncludingDuplicates + c("\033[22m"));
14+
buf.push("Modules pre chunk: "+c("\033[1m") + stats.modulesPerChunk + c("\033[22m"));
15+
buf.push("Modules first chunk: "+c("\033[1m") + stats.modulesFirstChunk + c("\033[22m"));
16+
if(stats.fileSizes)
17+
Object.keys(stats.fileSizes).reverse().forEach(function(file) {
18+
buf.push(c("\033[1m") + sprintf("%" + (3 + Object.keys(stats.fileSizes)[0].length) + "s", file) + c("\033[22m")+": "+c("\033[1m") + sprintf("%8d", stats.fileSizes[file]) + c("\033[22m") + " characters");
19+
});
20+
var cwd = process.cwd();
21+
var cwdParent = path.dirname(cwd);
22+
var buildins = path.join(__dirname, "..");
23+
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
24+
cwd = new RegExp("^" + cwd + "|(!)" + cwd, "g");
25+
var buildinsAsModule = cwd.test(buildins);
26+
cwdParent = cwdParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
27+
cwdParent = new RegExp("^" + cwdParent + "|(!)" + cwdParent, "g");
28+
buildins = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
29+
buildins = new RegExp("^" + buildins + "|(!)" + buildins, "g");
30+
var node_modulesRegExpA = /\/node_modules\//g;
31+
var node_modulesRegExpB = /\\node_modules\\/g;
32+
var index_jsRegExp = /[\\\/]index.js!/g;
33+
function compressFilename(filename) {
34+
if(!filename)
35+
return filename;
36+
if(buildinsAsModule)
37+
filename = filename.replace(buildins, "!(webpack)");
38+
filename = filename.replace(cwd, "!.");
39+
if(!buildinsAsModule)
40+
filename = filename.replace(buildins, "!(webpack)");
41+
filename = filename.replace(node_modulesRegExpA, "/~/");
42+
filename = filename.replace(node_modulesRegExpB, "\\~\\");
43+
filename = filename.replace(index_jsRegExp, "!");
44+
return filename.replace(/^!|!$/, "");
45+
}
46+
if(stats.fileModules) {
47+
buf.push("");
48+
buf.push(" <id> <size> <filename>");
49+
if(options.verbose)
50+
buf.push(" <reason> from <filename>");
51+
Object.keys(stats.fileModules).reverse().forEach(function(file) {
52+
buf.push(c("\033[1m\033[32m") + file + c("\033[39m\033[22m"));
53+
var modules = stats.fileModules[file];
54+
if(options["by-size"])
55+
modules.sort(function(a, b) {
56+
return b.size - a.size;
57+
});
58+
modules.forEach(function(module) {
59+
buf.push(" "+c("\033[1m") + sprintf("%3s", module.id+"") + " " + (typeof module.size === "number" ? sprintf("%9s", Math.round(module.size)+"") : " ") + " " +
60+
(compressFilename(module.filename) ||
61+
(module.dirname && ("[context] " + compressFilename(module.dirname))) ||
62+
"[unknown]") + c("\033[22m"));
63+
if(options.verbose) {
64+
module.reasons.forEach(function(reason) {
65+
switch(reason.type) {
66+
case "require":
67+
buf.push(" "+c("\033[36m")+"require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
68+
break;
69+
case "context":
70+
buf.push(" "+c("\033[90m")+"context from " + compressFilename(reason.filename) + c("\033[39m"));
71+
break;
72+
case "async require":
73+
buf.push(" "+c("\033[35m")+"async require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
74+
break;
75+
case "async context":
76+
buf.push(" "+c("\033[35ma")+"sync context from " + compressFilename(reason.filename) + c("\033[39m"));
77+
break;
78+
default:
79+
buf.push(" "+c("\033[31m") + reason.type + c("\033[39m"));
80+
}
81+
});
82+
}
83+
});
84+
});
85+
}
86+
if(stats.warnings) {
87+
stats.warnings.forEach(function(warning) {
88+
buf.push(c("\033[1m\033[33m")+"WARNING: " + warning + c("\033[39m\033[22m"));
89+
});
90+
}
91+
if(stats.errors) {
92+
stats.errors.forEach(function(error) {
93+
buf.push(c("\033[1m\033[31m")+"ERROR: " + error + c("\033[39m\033[22m"));
94+
});
95+
}
96+
return buf.join("\n");
97+
};

0 commit comments

Comments
 (0)