-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
163 lines (142 loc) · 4.17 KB
/
gulpfile.js
File metadata and controls
163 lines (142 loc) · 4.17 KB
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
var gulp = require('gulp'),
webpack = require('webpack'),
webpackCfg = require('./config/webpack'),
gutil = require('gulp-util'),
gData = require('gulp-data'),
jade = require('gulp-jade'),
compass = require('gulp-compass'),
WebpackDevServer = require('webpack-dev-server'),
constants = require('./config/constants'),
request = require('request');
// Webpack compiler & its config.
//
var compiler = webpack(webpackCfg),
// Instantiate server only in development mode,
// otherwise gulp would not terminate itself after performing all tasks
server = isProduction() ? {} : new WebpackDevServer(compiler, {
contentBase: './',
publicPath: '/assets/'
}),
webpackStats;
// Runs webpack compiler and generates javascript files.
//
gulp.task('webpackCompile', function(cb){
compiler.run(function(err, stats){
if(err){
throw new gutil.PluginError('webpack', err)
}
gutil.log('[webpack]', ""+stats);
webpackStats = stats;
cb();
});
});
// Starts a Webpack dev server, which watch file changes,
// re-compiles in memory (so no actual file is generated) and reloads the browser
// as needed.
//
gulp.task('devServer', function(cb){
server.listen(8080, "0.0.0.0", function(err){
if(err) throw new gutil.PluginError("webpack-dev-server", err);
gutil.log("[webpack-dev-server]", "http://localhost:8080/webpack-dev-server/index.html");
});
});
// Jade compilation
//
gulp.task('jade', function(){
return gulp.src('src/jade/index.jade')
.pipe(jade({
pretty: true,
locals: {
hash: 'main',
totalPoints: constants.TOTAL_POINTS,
totalUsers: constants.TOTAL_USERS
}
}))
.pipe(gulp.dest('./'))
.on('end', forceReload);
});
// Jade compilation with webpack compilation stats
//
gulp.task('jadeProduction', ['webpackCompile', 'compass'], function(){
return gulp.src('src/jade/index.jade')
.pipe(gData(populateLocals))
.pipe(jade({
pretty: false
}))
.pipe(gulp.dest('./'));
});
// Compass compilation
//
gulp.task('compass', function(){
return gulp.src('src/scss/main.scss')
.pipe(compass({
config_file: 'config/compass.rb',
css: 'assets',
sass: 'src/scss'
}))
.pipe(gulp.dest('./assets'))
.on('end', forceReload);
});
// Watch file change and invoke corresponding compilers.
//
// Note: This task has nothing to do with browser reload.
// Browser reload setup is in config/webpack.js.
//
gulp.task('watch', ['jade', 'compass'], function(cb){
gulp.watch('./src/jade/*', ['jade']);
gulp.watch('./src/scss/*', ['compass']);
});
gulp.task('default', ['watch', 'devServer']);
gulp.task('build', isProduction() ?
['jadeProduction'] : ['webpackCompile', 'jade', 'compass']);
/* Utility functions */
function forceReload() {
if(isProduction()) {
return;
}
// Force browser reload by hacking webpack-dev-server
// (using non-public api)
if(server.io){
gutil.log('[forceReload] Reloading...')
server.io.sockets.emit('ok');
}else{
throw new gutil.PluginError("forceReload", "webpack-dev-server socket is not ready");
}
}
function populateLocals(file, cb){
request(constants.API_ENDPOINT, function(err, resp, data){
if(err){
throw new gutil.PluginError("populateLocals", err);
}
var hasParseError = false;
try{
data = JSON.parse(data);
} catch(e) {
gutil.beep();
gutil.log('[populateLocals]', 'Parse error on data:', e);
gutil.log('[populateLocals]', 'Data:', data);
hasParseError = true;
}
if(!hasParseError && resp.statusCode === 200){
gutil.log('[populateLocals]', 'Fetched data:', data);
cb(null, {
totalPoints: data.total_points,
totalUsers: data.total_users,
hash: webpackStats.hash
});
} else {
gutil.log('[populateLocals]', 'Fallback to manual setup:', {
totalPoints: constants.TOTAL_POINTS,
totalUsers: constants.TOTAL_USERS
});
cb(null, {
totalPoints: constants.TOTAL_POINTS,
totalUsers: constants.TOTAL_USERS,
hash: webpackStats.hash
});
}
});
}
function isProduction() {
return process.env.NODE_ENV === 'production';
}