1 | const webpack = require('webpack')
|
2 | const readline = require('readline')
|
3 | const chalk = require('chalk')
|
4 | const defaults = require('lodash/defaults')
|
5 |
|
6 | const formatWebpackMessage = require('./formatWebpackMessage')
|
7 | const writeWebpackStats = require('./writeWebpackStats')
|
8 |
|
9 | const { CI, PROFILE } = process.env
|
10 |
|
11 | const DEFAULT_OPTIONS = {
|
12 | progress: true
|
13 | }
|
14 |
|
15 | module.exports = function createCompiler(config, options) {
|
16 | options = defaults(options, DEFAULT_OPTIONS)
|
17 |
|
18 | const compiler = webpack(config)
|
19 |
|
20 | if (options.progress && !CI) {
|
21 | new webpack.ProgressPlugin((progress, message) => {
|
22 | readline.clearLine(process.stdout)
|
23 | readline.cursorTo(process.stdout, 0)
|
24 | const percents = `${Math.round(progress * 100)}%`
|
25 | process.stdout.write(`Compiling ${percents}`)
|
26 | }).apply(compiler)
|
27 | }
|
28 |
|
29 | let start
|
30 | if (PROFILE) {
|
31 | compiler.hooks.run.tap('gnoll', () => {
|
32 | start = new Date()
|
33 | })
|
34 | compiler.hooks.watchRun.tap('gnoll', () => {
|
35 | start = new Date()
|
36 | })
|
37 | }
|
38 |
|
39 | const logTime = () => {
|
40 | if (PROFILE) {
|
41 | const time = new Date() - start
|
42 | console.log(chalk.cyan('Time:'), `${time}ms`)
|
43 | }
|
44 | }
|
45 |
|
46 | compiler.hooks.done.tap('gnoll', stats => {
|
47 |
|
48 |
|
49 | const jsonStats = stats.toJson({ errorDetails: false })
|
50 | const hasErrors = stats.hasErrors()
|
51 | const hasWarnings = stats.hasWarnings()
|
52 |
|
53 | if (PROFILE) writeWebpackStats(stats)
|
54 |
|
55 | console.log('\n')
|
56 |
|
57 | if (!hasErrors && !hasWarnings) {
|
58 | console.log(chalk.green('Compiled successfully!'))
|
59 | logTime()
|
60 | console.log()
|
61 | return
|
62 | }
|
63 |
|
64 | if (hasErrors) {
|
65 | console.log(chalk.red('Failed to compile.'))
|
66 | logTime()
|
67 | console.log()
|
68 | jsonStats.errors.forEach(message => {
|
69 | console.log(formatWebpackMessage(message))
|
70 | console.log()
|
71 | })
|
72 | return
|
73 | }
|
74 |
|
75 | if (hasWarnings) {
|
76 | console.log(chalk.yellow('Compiled with warnings.'))
|
77 | logTime()
|
78 | console.log()
|
79 | jsonStats.warnings.forEach(message => {
|
80 | console.log(formatWebpackMessage(message))
|
81 | console.log()
|
82 | })
|
83 | }
|
84 | })
|
85 |
|
86 | return compiler
|
87 | }
|