forked from neuroradiology/backstroke
/
gulpfile.js
173 lines (161 loc) · 4.97 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
'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 notify = require('gulp-notify');
var buffer = require('vinyl-buffer');
var argv = require('yargs').argv;
// sass
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
// js
var watchify = require('watchify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var babelify = require('babelify');
var envify = require('envify');
// gulp build --production
var production = !!argv.production;
// 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;
// ----------------------------
// Error notification methods
// ----------------------------
var handleError = function(task) {
return function(err) {
notify.onError({
message: task + ' failed, check the logs..',
sound: false
})(err);
gutil.log(gutil.colors.bgRed(task + ' error:'), gutil.colors.red(err));
};
};
// --------------------------
// CUSTOM TASK METHODS
// --------------------------
var tasks = {
// --------------------------
// Delete build folder
// --------------------------
clean: function(cb) {
del(['build/']).then(cb.bind(null, null)).catch(cb);
},
// --------------------------
// Copy static assets
// --------------------------
assets: function() {
return gulp.src('./public/assets/**/*')
.pipe(gulp.dest('build/assets/'));
},
// --------------------------
// HTML
// --------------------------
// html templates (when using the connect server)
templates: function() {
gulp.src('public/*.html')
.pipe(gulp.dest('build/'));
},
// --------------------------
// SASS (libsass)
// --------------------------
sass: function() {
return gulp.src('./public/scss/*.scss')
// sourcemaps + sass + error handling
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(sass({
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}))
.on('error', handleError('SASS'))
// 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('build/css'));
},
// --------------------------
// Browserify
// --------------------------
browserify: function() {
var bundler = browserify('./public/js/index.js', {
debug: !production,
cache: {},
paths: ['./node_modules', './public/js'],
});
bundler.transform(babelify)
bundler.transform(envify)
// determine if we're doing a build
// and if so, bypass the livereload
var build = argv._.length ? argv._[0] === 'build' : false;
if (watch) {
bundler = watchify(bundler);
}
var rebundle = function() {
return bundler.bundle()
.on('error', handleError('Browserify'))
.pipe(source('build.js'))
.pipe(gulpif(production, buffer()))
.pipe(gulpif(production, uglify()))
.pipe(gulp.dest('build/js/'));
};
bundler.on('update', rebundle);
return rebundle();
},
};
// --------------------------
// CUSTOMS TASKS
// --------------------------
gulp.task('clean', tasks.clean);
// for production we require the clean method on every individual task
var req = build ? ['clean'] : [];
// individual tasks
gulp.task('templates', req, tasks.templates);
gulp.task('assets', req, tasks.assets);
gulp.task('sass', req, tasks.sass);
gulp.task('browserify', req, tasks.browserify);
gulp.task('lint:js', tasks.lintjs);
gulp.task('optimize', tasks.optimize);
gulp.task('test', tasks.test);
// --------------------------
// DEV/WATCH TASK
// --------------------------
gulp.task('watch', ['assets', 'templates', 'sass', 'browserify'], function() {
// --------------------------
// watch:js
// --------------------------
gulp.watch('./public/js/**/*.js', ['lint:js']);
gulp.watch('./public/**/*.scss', ['sass']);
gutil.log(gutil.colors.bgGreen('Watching for changes...'));
});
// build task
gulp.task('build', [
'clean',
'assets',
'templates',
'sass',
'browserify'
]);
gulp.task('default', ['watch']);
// gulp (watch) : for development and livereload
// gulp build : for a one off development build
// gulp build --production : for a minified production build