Skip to content

Commit 494eb90

Browse files
authored
Merge pull request #206 from developit/feat/less-verbose-errors
feat: Less verbose error output
2 parents f5a7ec0 + 031d6cb commit 494eb90

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/lib/webpack/run-webpack.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,30 @@ const devBuild = async (env, onprogress) => {
2626

2727
let compiler = webpack(config);
2828
return await new Promise((resolve, reject) => {
29-
let first = true;
29+
3030
compiler.plugin('done', stats => {
31-
if (first) {
32-
first = false;
33-
let devServer = config.devServer;
31+
let devServer = config.devServer;
32+
33+
let protocol = devServer.https ? 'https' : 'http';
34+
let host = process.env.HOST || devServer.host || 'localhost';
3435

35-
let protocol = devServer.https ? 'https' : 'http';
36-
let host = process.env.HOST || devServer.host || 'localhost';
36+
let serverAddr = `${protocol}://${host}:${chalk.bold(port)}`;
37+
let localIpAddr = `${protocol}://${ip.address()}:${chalk.bold(port)}`;
3738

38-
let serverAddr = `${protocol}://${host}:${chalk.bold(port)}`;
39-
let localIpAddr = `${protocol}://${ip.address()}:${chalk.bold(port)}`;
39+
clearConsole();
40+
if (stats.hasErrors()) {
41+
process.stdout.write(chalk.red('\Build failed!\n\n'));
42+
} else {
43+
process.stdout.write(chalk.green('\nCompiled successfully!\n\n'));
4044

41-
process.stdout.write(chalk.green('\nCompiled successfully!!\n\n'));
4245
if (userPort !== port) {
4346
process.stdout.write(`Port ${chalk.bold(userPort)} is in use, using ${chalk.bold(port)} instead\n\n`);
4447
}
4548
process.stdout.write('You can view the application in browser.\n\n');
4649
process.stdout.write(`${chalk.bold('Local:')} ${serverAddr}\n`);
4750
process.stdout.write(`${chalk.bold('On Your Network:')} ${localIpAddr}\n`);
4851
}
52+
4953
if (onprogress) onprogress(stats);
5054
});
5155
compiler.plugin('failed', reject);
@@ -71,7 +75,8 @@ const prodBuild = async (env) => {
7175
return await new Promise((resolve, reject) => {
7276
compiler.run((err, stats) => {
7377
if (err || stats.hasErrors()) {
74-
reject(err || stats.toJson().errors.join('\n'));
78+
showStats(stats);
79+
reject(chalk.red('Build failed!'));
7580
}
7681
else {
7782
// Timeout for plugins that work on `after-emit` event of webpack
@@ -82,16 +87,16 @@ const prodBuild = async (env) => {
8287
};
8388

8489
export function showStats(stats) {
85-
let info = stats.toJson();
90+
let info = stats.toJson("errors-only");
8691

8792
if (stats.hasErrors()) {
88-
info.errors.forEach( message => {
89-
process.stderr.write(message+'\n');
93+
info.errors.map(stripBabelLoaderPrefix).forEach( message => {
94+
process.stderr.write(chalk.red(message)+'\n');
9095
});
9196
}
9297

9398
if (stats.hasWarnings()) {
94-
info.warnings.forEach( message => {
99+
info.warnings.map(stripBabelLoaderPrefix).forEach( message => {
95100
process.stderr.write(chalk.yellow(message)+'\n');
96101
});
97102
}
@@ -109,8 +114,8 @@ export function writeJsonStats(stats) {
109114

110115
jsonStats = (jsonStats.children && jsonStats.children[0]) || jsonStats;
111116

112-
jsonStats.modules.forEach(normalizeModule);
113-
jsonStats.chunks.forEach(c => c.modules.forEach(normalizeModule));
117+
jsonStats.modules.forEach(stripBabelLoaderFromModuleNames);
118+
jsonStats.chunks.forEach(c => c.modules.forEach(stripBabelLoaderFromModuleNames));
114119

115120
return fs.writeFile(outputPath, JSON.stringify(jsonStats))
116121
.then(() => {
@@ -121,20 +126,26 @@ export function writeJsonStats(stats) {
121126
});
122127
}
123128

124-
const normalizeModule = m => {
129+
const clearConsole = () => {
130+
process.stdout.write(
131+
process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'
132+
);
133+
};
134+
135+
const stripBabelLoaderFromModuleNames = m => {
125136
const keysToNormalize = ['identifier', 'name', 'module', 'moduleName', 'moduleIdentifier'];
126137

127138
keysToNormalize.forEach(key => {
128139
if (key in m) {
129-
m[key] = normalizeName(m[key]);
140+
m[key] = stripBabelLoaderPrefix(m[key]);
130141
}
131142
});
132143

133144
if (m.reasons) {
134-
m.reasons.forEach(normalizeModule);
145+
m.reasons.forEach(stripBabelLoaderFromModuleNames);
135146
}
136147

137148
return m;
138149
};
139150

140-
const normalizeName = p => p.substr(p.lastIndexOf('!') + 1);
151+
const stripBabelLoaderPrefix = log => log.replace(/@?\s*(\.\/~\/babel-loader\/lib\?{[\s\S]*?}!)/g, '');

0 commit comments

Comments
 (0)