|
| 1 | +const color = require('colors-cli'); |
| 2 | +const loading = require('loading-cli'); |
| 3 | +const clearConsole = require('./clearConsole'); |
| 4 | +const formatWebpackMessages = require('webpack-hot-dev-clients/formatWebpackMessages'); |
| 5 | + |
| 6 | +const isInteractive = process.stdout.isTTY; |
| 7 | +let handleCompile; |
| 8 | +// You can safely remove this after ejecting. |
| 9 | +// We only use this block for testing of Create React App itself: |
| 10 | +const isSmokeTest = process.argv.some(arg => arg.indexOf('--smoke-test') > -1); |
| 11 | +if (isSmokeTest) { |
| 12 | + handleCompile = (err, stats) => { |
| 13 | + if (err || stats.hasErrors() || stats.hasWarnings()) { |
| 14 | + process.exit(1); |
| 15 | + } else { |
| 16 | + process.exit(0); |
| 17 | + } |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +function printInstructions(appName, urls, useYarn) { |
| 23 | + console.log(); |
| 24 | + console.log(`You can now view ${color.bold(appName)} in the browser.`); |
| 25 | + console.log(); |
| 26 | + |
| 27 | + if (urls.lanUrlForTerminal) { |
| 28 | + console.log( |
| 29 | + ` ${color.bold('Local:')} ${urls.localUrlForTerminal}` |
| 30 | + ); |
| 31 | + console.log( |
| 32 | + ` ${color.bold('On Your Network:')} ${urls.lanUrlForTerminal}` |
| 33 | + ); |
| 34 | + } else { |
| 35 | + console.log(` ${urls.localUrlForTerminal}`); |
| 36 | + } |
| 37 | + |
| 38 | + console.log(); |
| 39 | + console.log('Note that the development build is not optimized.'); |
| 40 | + console.log( |
| 41 | + `To create a production build, use ` + |
| 42 | + `${color.cyan(`${useYarn ? 'yarn' : 'npm run'} build`)}.` |
| 43 | + ); |
| 44 | + console.log(); |
| 45 | +} |
| 46 | + |
| 47 | +function printDoneMessage(stats, isInteractive, isFirstCompile, appName, urls, useYarn) { |
| 48 | + // We have switched off the default Webpack output in WebpackDevServer |
| 49 | + // options so we are going to "massage" the warnings and errors and present |
| 50 | + // them in a readable focused way. |
| 51 | + const messages = formatWebpackMessages(stats.toJson({}, true)); |
| 52 | + const isSuccessful = !messages.errors.length && !messages.warnings.length; |
| 53 | + if (isSuccessful) { |
| 54 | + console.log(color.green('Compiled successfully!')); |
| 55 | + } |
| 56 | + if (isSuccessful && (isInteractive || isFirstCompile)) { |
| 57 | + printInstructions(appName, urls, useYarn); |
| 58 | + } |
| 59 | + isFirstCompile = false; |
| 60 | + |
| 61 | + // If errors exist, only show errors. |
| 62 | + if (messages.errors.length) { |
| 63 | + // Only keep the first error. Others are often indicative |
| 64 | + // of the same problem, but confuse the reader with noise. |
| 65 | + if (messages.errors.length > 1) { |
| 66 | + messages.errors.length = 1; |
| 67 | + } |
| 68 | + console.log(color.red('Failed to compile.\n')); |
| 69 | + console.log(messages.errors.join('\n\n')); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Show warnings if no errors were found. |
| 74 | + if (messages.warnings.length) { |
| 75 | + console.log(color.yellow('Compiled with warnings.\n')); |
| 76 | + console.log(messages.warnings.join('\n\n')); |
| 77 | + |
| 78 | + // Teach some ESLint tricks. |
| 79 | + console.log( |
| 80 | + '\nSearch for the ' + |
| 81 | + color.underline(color.yellow('keywords')) + |
| 82 | + ' to learn more about each warning.' |
| 83 | + ); |
| 84 | + console.log( |
| 85 | + 'To ignore, add ' + |
| 86 | + color.cyan('// eslint-disable-next-line') + |
| 87 | + ' to the line before.\n' |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +let isloading = true; |
| 94 | + |
| 95 | +function createCompiler(webpack, config, appName, urls, useYarn) { |
| 96 | + // "Compiler" is a low-level interface to Webpack. |
| 97 | + // It lets us listen to some events and provide our own custom messages. |
| 98 | + let compiler; |
| 99 | + try { |
| 100 | + compiler = webpack(config, handleCompile); |
| 101 | + } catch (err) { |
| 102 | + console.log(color.red('Failed to compile.')); |
| 103 | + console.log(); |
| 104 | + console.log(err.message || err); |
| 105 | + console.log(); |
| 106 | + process.exit(1); |
| 107 | + } |
| 108 | + const load = loading('Starting the development server...\n') |
| 109 | + if (isloading) { |
| 110 | + isloading = false; |
| 111 | + } |
| 112 | + |
| 113 | + // "invalid" event fires when you have changed a file, and Webpack is |
| 114 | + // recompiling a bundle. WebpackDevServer takes care to pause serving the |
| 115 | + // bundle, so if you refresh, it'll wait instead of serving the old one. |
| 116 | + // "invalid" is short for "bundle invalidated", it doesn't imply any errors. |
| 117 | + compiler.plugin('invalid', () => { |
| 118 | + if (isInteractive) { |
| 119 | + clearConsole(); |
| 120 | + } |
| 121 | + load.start() |
| 122 | + load.text = ' Compiling...'; |
| 123 | + }); |
| 124 | + |
| 125 | + let isFirstCompile = true; |
| 126 | + // "done" event fires when Webpack has finished recompiling the bundle. |
| 127 | + // Whether or not you have warnings or errors, you will get this event. |
| 128 | + compiler.plugin('done', stats => { |
| 129 | + // if (isInteractive) { |
| 130 | + // // clearConsole(); |
| 131 | + // } |
| 132 | + clearTimeout(this.timer); |
| 133 | + this.timer = setTimeout(() => { |
| 134 | + clearConsole(); |
| 135 | + load.stop() |
| 136 | + printDoneMessage(stats, isInteractive, isFirstCompile, appName, urls, useYarn) |
| 137 | + },1000) |
| 138 | + |
| 139 | + }); |
| 140 | + return compiler; |
| 141 | +} |
| 142 | + |
| 143 | +module.exports = createCompiler; |
0 commit comments