1 | var chalk = require('chalk');
|
2 | var webpack = require('webpack');
|
3 |
|
4 | var clearConsole = require('./clearConsole');
|
5 | var formatWebpackMessages = require('./formatWebpackMessages');
|
6 |
|
7 | var isInteractive = process.stdout.isTTY;
|
8 |
|
9 | function setupCompiler(config, server) {
|
10 | var compiler;
|
11 |
|
12 |
|
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 |
|
24 |
|
25 |
|
26 |
|
27 | compiler.plugin('invalid', function () {
|
28 | if (server && isInteractive) {
|
29 | clearConsole();
|
30 | }
|
31 | console.log('Compiling...');
|
32 | });
|
33 |
|
34 | var isFirstCompile = true;
|
35 |
|
36 |
|
37 |
|
38 | compiler.plugin('done', function (stats) {
|
39 | if (server && isInteractive) {
|
40 | clearConsole();
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
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 |
|
62 | console.log();
|
63 | isFirstCompile = false;
|
64 | }
|
65 |
|
66 |
|
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 |
|
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 |
|
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 |
|
94 | module.exports = setupCompiler;
|