UNPKG

3.28 kBJavaScriptView Raw
1var chalk = require('chalk');
2var webpack = require('webpack');
3
4var clearConsole = require('./clearConsole');
5var formatWebpackMessages = require('./formatWebpackMessages');
6
7var isInteractive = process.stdout.isTTY;
8
9function setupCompiler(config, server) {
10 var compiler;
11 // "Compiler" is a low-level interface to Webpack.
12 // It lets us listen to some events and provide our own custom messages.
13 try {
14 compiler = webpack(config);
15 } catch (err) {
16 console.log(chalk.red('Failed to compile.'));
17 console.log();
18 console.log(err.message || err);
19 console.log();
20 process.exit(1);
21 }
22
23 // "invalid" event fires when you have changed a file, and Webpack is
24 // recompiling a bundle. WebpackDevServer takes care to pause serving the
25 // bundle, so if you refresh, it'll wait instead of serving the old one.
26 // "invalid" is short for "bundle invalidated", it doesn't imply any errors.
27 compiler.plugin('invalid', function () {
28 if (server && isInteractive) {
29 clearConsole();
30 }
31 console.log('Compiling...');
32 });
33
34 var isFirstCompile = true;
35
36 // "done" event fires when Webpack has finished recompiling the bundle.
37 // Whether or not you have warnings or errors, you will get this event.
38 compiler.plugin('done', function (stats) {
39 if (server && isInteractive) {
40 clearConsole();
41 }
42
43 // We have switched off the default Webpack output in WebpackDevServer
44 // options so we are going to "massage" the warnings and errors and present
45 // them in a readable focused way.
46 var messages = formatWebpackMessages(stats.toJson({}, true));
47 var isSuccessful = !messages.errors.length && !messages.warnings.length;
48 var showInstructions = server && isSuccessful && (isInteractive || isFirstCompile);
49
50 if (isSuccessful) {
51 console.log(chalk.green('Compiled successfully!'));
52 }
53
54 if (showInstructions) {
55 console.log();
56 console.log('The app is running at:');
57 console.log();
58 console.log(' ' + chalk.cyan(server.protocol + '://' + server.host + ':' + server.port + '/'));
59 console.log();
60 console.log('Note that the development build is not optimized.');
61 // console.log('To create a production build, use ' + chalk.cyan('npm run build') + '.');
62 console.log();
63 isFirstCompile = false;
64 }
65
66 // If errors exist, only show errors.
67 if (messages.errors.length) {
68 console.log(chalk.red('Failed to compile.'));
69 console.log();
70 messages.errors.forEach(function (message) {
71 console.log(message);
72 console.log();
73 });
74 return;
75 }
76
77 // Show warnings if no errors were found.
78 if (messages.warnings.length) {
79 console.log(chalk.yellow('Compiled with warnings.'));
80 console.log();
81 messages.warnings.forEach(function (message) {
82 console.log(message);
83 console.log();
84 });
85 // Teach some ESLint tricks.
86 console.log('You may use special comments to disable some warnings.');
87 console.log('Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.');
88 console.log('Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.');
89 }
90 });
91 return compiler;
92}
93
94module.exports = setupCompiler;