-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (68 loc) · 2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const Trailpack = require('trailpack')
const _ = require('lodash')
const webpack = require('webpack')
/**
* React Trailpack
*
* @class React
*
* Use React in your Trails application
*/
module.exports = class Hapi extends Trailpack {
/**
* Ensure that config/webpack is valid
*/
validate() {
const config = this.app.config
const logger = this.app.config.log.logger
if (!config.webpack.options) {
logger.warn('trailspack-react: no Webpack "options" are defined.')
logger.warn('trailspack-react: Please configure config/webpack.js')
}
return Promise.resolve()
}
/**
* Start Webpack
*/
initialize() {
const logger = this.app.config.log.logger
return new Promise((resolve, reject) => {
this.compiler = webpack(this.app.config.webpack.options, (err, stats) => {
if (err) return reject(err)
logger.info('trailspack-react: compiler loaded.')
logger.silly('trailspack-react: ', stats.toString())
if (process.env.NODE_ENV == 'development') {
logger.info('trailspack-react: watching...')
this.compiler.watch(_.extend({}, this.app.config.webpack.watchOptions), this.afterBuild.bind(this))
}
else {
logger.info('trailspack-react: running...')
this.compiler.run(this.afterBuild.bind(this))
}
resolve()
})
})
}
afterBuild(err, rawStats) {
const logger = this.app.config.log.logger
if (err) return logger.error('trailspack-react: FATAL ERROR', err)
const stats = rawStats.toJson()
logger.debug('trailspack-react: Build Info\n' + rawStats.toString({
colors: true,
chunks: false
}))
if (stats.errors.length > 0) {
logger.error('trailspack-react:', stats.errors)
}
if (stats.warnings.length > 0) {
logger.warn('trailspack-react:', stats.warnings)
}
}
constructor(app, config) {
super(app, {
config: require('./config'),
pkg: require('./package')
})
}
}