-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
181 lines (161 loc) · 4.51 KB
/
gulpfile.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* global argv */
// Include Gulp and other build automation tools and utilities
// See: https://github.com/gulpjs/gulp/blob/master/docs/API.md
var _ = require('lodash')
var gulp = require('gulp')
var $ = require('gulp-load-plugins')()
var del = require('del')
var path = require('path')
var runSequence = require('run-sequence')
var webpack = require('webpack')
var options = require('minimist')(process.argv.slice(2), {
alias: {
debug: 'D',
verbose: 'V'
},
boolean: ['debug', 'verbose'],
default: {
debug: false,
verbose: false
}
})
$.util.log('[args]', ' debug = ' + options.debug)
$.util.log('[args]', ' verbose = ' + options.verbose)
// https://github.com/ai/autoprefixer
options.autoprefixer = [
'last 2 version'
]
var paths = {
build: 'build',
ritzy: 'node_modules/ritzy/dist',
src: [
'src/**/*.js',
'!src/server.js',
'!src/**/__tests__/**/*.js',
'!src/**/__mocks__/**/*.js',
'!src/assets/*',
'!src/templates/*'
]
}
var src = {
assets: [
'src/assets/**',
'src/templates*/**'
],
server: [
paths.build + '/app.js',
paths.build + '/server.js',
paths.build + '/templates/**/*',
paths.build + '/fonts/**/*'
]
}
var watch = false
var webpackOpts = function(output, configs, debug) {
return require('./webpack.config.js')(output, configs, debug, options.verbose, options.autoprefixer)
}
var webpackCompletion = function(err, stats) {
if(err) {
throw new $.util.PluginError('webpack', err, {showStack: true})
}
var jsonStats = stats.toJson()
var statsOptions = { colors: true/*, modulesSort: 'size'*/ }
if(jsonStats.errors.length > 0) {
if(watch) {
$.util.log('[webpack]', stats.toString(statsOptions))
} else {
throw new $.util.PluginError('webpack', stats.toString(statsOptions))
}
}
if(jsonStats.warnings.length > 0 || options.verbose) {
$.util.log('[webpack]', stats.toString(statsOptions))
}
if(jsonStats.errors.length === 0 && jsonStats.warnings.length === 0) {
$.util.log('[webpack]', 'No errors or warnings.')
}
}
// Check the version of node currently being used
gulp.task('node-version', function(cb) { // eslint-disable-line no-unused-vars
return require('child_process').fork(null, {execArgv: ['--version']})
})
// The default task
gulp.task('default', ['serve'])
// Clean output directory
gulp.task('clean', del.bind(
null, ['.tmp', paths.build], {dot: true}
))
gulp.task('assets:local', function() {
return gulp.src(src.assets)
.pipe($.changed(paths.build))
.pipe(gulp.dest(paths.build))
.pipe($.size({title: 'assets'}))
})
gulp.task('assets:fonts', function() {
return gulp.src(paths.ritzy + '/fonts/**/*.ttf', {base: paths.ritzy})
.pipe($.changed(paths.build))
.pipe(gulp.dest(paths.build))
.pipe($.size({title: 'fonts'}))
})
gulp.task('assets', ['assets:local', 'assets:fonts'])
var compile = function(cb, webpackConfigs) {
var started = false
function webpackCb(err, stats) {
webpackCompletion(err, stats)
if (!started) {
started = true
return cb()
}
}
var compiler = webpack(webpackOpts(paths.build, webpackConfigs, true))
if (watch) {
compiler.watch(200, webpackCb)
} else {
compiler.run(webpackCb)
}
}
gulp.task('compile:client', function(cb) {
compile(cb, {client: true})
})
gulp.task('compile:server', function(cb) {
compile(cb, {server: true})
})
// Build and run the app from source code
gulp.task('build', ['clean'], function(cb) {
runSequence(['assets', 'compile:client', 'compile:server'], cb)
})
// Run and start watching for modifications
gulp.task('build:watch', function(cb) {
watch = true
runSequence('build', function(err) {
gulp.watch(src.assets, ['assets'])
cb(err)
})
})
// Launch a Node.js/Express server
gulp.task('serve', ['build:watch'], function(cb) {
var started = false
var cp = require('child_process')
var nodeArgs = {}
if(options.debug) {
$.util.log('[node]', 'Node.js debug port set to 5858.')
nodeArgs.execArgv = ['--debug-brk=5858']
}
var server = (function startup() {
var child = cp.fork(paths.build + '/server.js', nodeArgs, {
env: _.assign({NODE_ENV: 'development'}, process.env)
})
child.once('message', function(message) {
if (message.match(/^online$/)) {
if (!started) {
started = true
gulp.watch(src.server, function() {
$.util.log('Restarting demo server.')
server.kill('SIGTERM')
server = startup()
})
cb()
}
}
})
return child
})()
})