-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
268 lines (230 loc) · 6.08 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Gulp file for colour tools site
*/
'use strict';
/**
* Config
*/
const sass_conf = {
outputStyle: 'nested',
includePaths: [
'web/lib/font-awesome/scss',
],
precision: 10,
errLogToConsole: true,
};
const prefix_conf = {
browsers: ['last 2 versions', 'ie > 8'],
};
/**
* Load plugins
*/
// Gulp and utilities
const gulp = require('gulp-help')(require('gulp'));
const u = require('gulp-util');
const log = u.log;
const c = u.colors;
const del = require('del');
const plumber = require('gulp-plumber');
const argv = require('yargs').default('production', false).argv;
const watch = require('gulp-watch');
const gulpif = require('gulp-if');
// SASS
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const prefix = require('gulp-autoprefixer');
const clean = require('gulp-clean-css');
const globbing = require('gulp-css-globbing');
// JavaScript
const fs = require('fs');
const path = require('path');
const merge = require('merge-stream');
const concat = require('gulp-concat');
const babel = require('gulp-babel');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
// Images
const imagemin = require('gulp-imagemin');
// Nunjucks templating
const nunjucks = require('gulp-nunjucks-render');
// Browser Sync
const bs = require('browser-sync').create();
// Check environment
let env = 'dev';
if (argv.production) {
env = 'prod';
sass_conf.outputStyle = 'compressed';
}
function is_dev() {
return env === 'dev';
}
/**
* Error handler
*/
function onError(err) {
log(c.red(err.message));
if (err.message !== 'undefined') {
log(c.red(`Error: ${err.message}`));
} else if (err.messageFormatted) {
// Send the error to the console.
log(c.red(`Error in file: ${err.messageFormatted}`));
}
this.emit('end');
}
/**
* Get sub directories in dir
*/
function getDirs(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
/**
* Compile SASS
*/
gulp.task('sass', 'Compile SASS', () => {
return gulp.src([
'sass/**/*.scss',
],
{ base: 'sass' })
.pipe(plumber({
errorHandler: onError,
}))
.pipe(globbing({
extensions: ['.scss'],
}))
.pipe(gulpif(is_dev, sourcemaps.init()))
.pipe(sass(sass_conf))
.pipe(prefix(prefix_conf))
.pipe(gulpif(!is_dev, uglify()))
.pipe(gulpif(is_dev, sourcemaps.write()))
.pipe(gulp.dest('./web/css'))
.pipe(bs.stream({match: '**/*.css'}));
});
/**
* Delete compiled CSS
*/
gulp.task('clean-css', 'Delete all the CSS files in the ./web/css directory', () => {
return del(['web/css/*.css'], (err) => {
if (err) {
log(c.red('clean-css'), err);
} else {
log(c.green('clean-css'), 'deleted old stylesheets');
}
});
});
/**
* Transpile, minify and concatenate Javascript files in scripts directory,
* creating separate files for everything in each sub directory.
* Service workers need to live in root, anything in sw dir goes there.
*/
gulp.task('scripts', () => {
const scriptsPath = 'scripts';
const dirs = getDirs('scripts');
const is_sw = function(dir) {
return dir === 'sw';
}
const tasks = dirs.map((dir) => {
return gulp.src(path.join('scripts', dir, '/**/*.js'))
.pipe(gulpif(is_dev, sourcemaps.init()))
.pipe(plumber({
errorHandler: onError,
}))
.pipe(babel({ presets: ['es2015'] }))
.pipe(concat(dir + '.js'))
.pipe(gulpif(is_sw(dir), gulp.dest('./web'), gulp.dest('./web/js')))
.pipe(uglify())
.pipe(rename(dir + '.min.js'))
.pipe(gulpif(is_dev, sourcemaps.write()))
.pipe(gulpif(is_sw(dir), gulp.dest('./web'), gulp.dest('./web/js')))
});
const root = gulp.src('scripts/*.js')
.pipe(gulpif(is_dev, sourcemaps.init()))
.pipe(plumber({
errorHandler: onError,
}))
.pipe(babel({ presets: ['es2015'] }))
.pipe(concat('scripts.js'))
.pipe(gulp.dest('./web/js'))
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(gulpif(is_dev, sourcemaps.write()))
.pipe(gulp.dest('./web/js'));
return merge(tasks, root)
.pipe(bs.stream({ match: '**/*.js' }));
});
/**
* Compress images
*/
gulp.task('images', 'Compress image in ./images directory', () => {
return gulp.src('images/**/*.+(gif|jpeg|jpg|png|svg)')
.pipe(plumber({
errorHandler: onError,
}))
.pipe(imagemin({
plugins: [
{ removeDoctype: false },
]}))
.pipe(gulp.dest('./web/img'));
});
/**
* Compile Nunjucks templates
*/
gulp.task('nunjucks', 'Compile Nunjucks templates in the ./pages directory', () => {
return gulp.src('pages/*.+(html|nunjucks)')
.pipe(plumber({
errorHandler: onError,
}))
.pipe(nunjucks({
path: ['pages/templates'],
}))
.pipe(gulp.dest('web'))
.pipe(bs.stream({ match: '**/*.html' }));
});
/**
* Set up Browser Sync
*/
gulp.task('bs', 'Set Browser Sync to serve the ./web directory', () => {
bs.init({
server: {
baseDir: './web/',
},
});
});
/**
* Watch SASS directory for changes.
*/
gulp.task('watch-sass', () => {
watch('sass/**/*.scss', { verbose: true, usePolling: true, useFsEvents: true }, () => {
gulp.start(`sass-${env}`);
});
});
/**
* Watch JavaScript directory for changes.
*/
gulp.task('watch-scripts', () => {
watch('scripts/**/*.js', { verbose: true, usePolling: true, useFsEvents: true }, () => {
gulp.start('scripts');
});
});
/**
* Watch images directory for changes.
*/
gulp.task('watch-images', () => {
watch('images/**/*.svg', { verbose: true, usePolling: true, useFsEvents: true },() => {
gulp.start('images');
});
});
/**
* Watch Nunjucks pages directory for changes.
*/
gulp.task('watch-pages', () => {
watch('pages/**/*.+(html|nunjucks)', { verbose: true, usePolling: true, useFsEvents: true }, () => {
gulp.start('nunjucks');
});
});
/**
* Default task - compile and watch SASS, JavaScript and Nunjucks templates
*/
gulp.task('default', false, ['bs', 'scripts', 'images', 'nunjucks', 'sass', 'watch-scripts', 'watch-images', 'watch-pages', 'watch-sass']);