This repository has been archived by the owner on Jun 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
241 lines (213 loc) · 6.42 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var del = require('del');
var uglify = require('gulp-uglify');
var gulpif = require('gulp-if');
var exec = require('child_process').exec;
var buffer = require('vinyl-buffer');
var argv = require('yargs').argv;
var sourcemaps = require('gulp-sourcemaps');
// sass
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
// js
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var babelify = require('babelify');
var nodemon = require('gulp-nodemon');
// production flag
var production = !argv.dev;
var syncbrowser = argv.browsersync;
// determine if we're doing a build
// and if so, bypass the livereload
var build = argv._.length ? argv._[0] === 'build' : false;
var watch = argv._.length ? argv._[0] === 'watch' : true;
gutil.log(gutil.colors.bgGreen('[Gulp flags]', 'production:', production, '| build:', build, '| watch:', watch, "| syncbrowser:", syncbrowser));
if (watch) {
var watchify = require('watchify');
}
if (syncbrowser) {
var browserSync = require('browser-sync').create();
}
var reloadbrowsersync = function() {
if (syncbrowser) {
browserSync.reload();
}
}
// ----------------------------
// Error notification methods
// ----------------------------
var handleError = function(task) {
return function(err) {
gutil.log(gutil.colors.bgRed(task + ' error:'), gutil.colors.red(err));
if (watch) this.emit('end');
};
};
// --------------------------
// CUSTOM TASK METHODS
// --------------------------
var tasks = {
// --------------------------
// Delete build folder
// --------------------------
clean: function() {
del.sync(['public']);
return gulp.src('.gitignore')
.pipe(gulp.dest('public/')
);
},
// --------------------------
// CSS compilation (LibSass + Autoprefixer)
// --------------------------
sass: function() {
return gulp.src('src/assets/scss/[^_]*.scss')
// sourcemaps + sass + error handling
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(sass({
errLogToConsole: true,
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}))
.on('error', function(err) {
sass.logError.bind(this, err)();
})
// generate .maps
.pipe(gulpif(!production, sourcemaps.write({
'includeContent': false,
'sourceRoot': '.'
})))
// autoprefixer
.pipe(gulpif(!production, sourcemaps.init({
'loadMaps': true
})))
.pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
// we don't serve the source files
// so include scss content inside the sourcemaps
.pipe(sourcemaps.write({
'includeContent': true
}))
// write sourcemaps to a specific directory
// give it a file and save
.pipe(gulp.dest('public/css'));
},
// --------------------------
// Browserify bundles (Babelify + Watchify)
// --------------------------
babelify: function() {
// Create a separate vendor bundler that will only run when starting gulp
var vendorBundler = browserify({
debug: !production // Sourcemapping
})
.require('react');
var bundler = browserify({
debug: !production, // Sourcemapping
cache: {},
packageCache: {},
fullPaths: watch
})
.require(require.resolve('./src/app/app.jsx'), { entry: true })
.transform('babelify')
.external('react');
if (watch) {
bundler = watchify(bundler, {poll: true});
}
var rebundle = function() {
var result = bundler.bundle()
.on('error', handleError('Browserify'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulpif(production, uglify()))
.pipe(gulpif(!production, sourcemaps.init({loadMaps: true})))
.pipe(gulpif(!production, sourcemaps.write('./')))
.pipe(gulp.dest('public/js/'));
if(syncbrowser) {
return result.pipe(browserSync.reload({stream:true, once: true}));
}
return result;
};
if (watch) {
bundler.on('update', rebundle);
bundler.on('log', function (msg) {
gutil.log('Babelify rebundle:', msg);
});
}
vendorBundler.bundle()
.pipe(source('vendors.js'))
.pipe(buffer())
.pipe(gulpif(production, uglify()))
.pipe(gulp.dest('public/js/'));
return rebundle();
},
// --------------------------
// Node server (Nodemon + BrowserSync)
// --------------------------
serve: function(cb) {
var started = false;
return nodemon({
verbose: true,
legacyWatch: true,
script: 'src/server/index.js',
watch: ['src/server', 'src/templates'],
execMap: {
'js': 'babel-node'
},
ext: 'js html',
stdout: false,
env: {
'NODE_ENV': 'development',
'PORT': syncbrowser ? 8878 : 8877
}
}).on('start', function () {
gutil.log(gutil.colors.bgGreen('Nodemon ' + (started ? 're' : 'first ') + 'start...'));
if (!started) {
started = true;
if (syncbrowser) {
browserSync.init(null, {
port: 8877,
proxy: {
target: 'localhost:8878'
},
open: false
});
}
cb();
}
}).on('readable', function(data) {
// this is the best hack I have found so far to reload browsers once the
// server is actually running after a restart, not just starting up
this.stdout.on('data', function(chunk) {
if (/up and running on/.test(chunk)) {
reloadbrowsersync();
}
process.stdout.write(chunk);
});
this.stderr.pipe(process.stderr);
});
}
};
gulp.task('reload-sass', ['sass'], function(){
reloadbrowsersync();
});
// --------------------------
// CUSTOMS TASKS
// --------------------------
gulp.task('clean', tasks.clean);
gulp.task('sass', tasks.sass);
gulp.task('babelify', tasks.babelify);
gulp.task('serve', ['build'], tasks.serve);
gulp.task('start', ['clean', 'serve']);
// build task
gulp.task('build', [
'sass',
'babelify'
]);
// --------------------------
// DEV/WATCH TASK
// --------------------------
gulp.task('watch', ['start'], function() {
gulp.watch(['src/assets/scss/**/*.scss'], ['reload-sass']);
gutil.log(gutil.colors.bgGreen('Watching for changes...'));
});
gulp.task('default', ['start']);