-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
executable file
·374 lines (335 loc) · 11 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
'use strict';
// *************************
//
// Run 'gulp' to watch directory for changes for images, fonts icons, Sass, etc.
// Or for full site testing run 'gulp test'
//
// *************************
// Include gulp.
const gulp = require('gulp');
// Include plug-ins.
const jshint = require('gulp-jshint');
const imagemin = require('gulp-imagemin');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const del = require('del');
const uglify = require('gulp-uglify');
const iconfont = require('gulp-iconfont');
const iconfontCss = require('gulp-iconfont-css');
const iconfontTemplate = require('gulp-iconfont-template');
const groupMediaQueries = require('gulp-group-css-media-queries');
const extractMediaQueries = require('gulp-extract-media-queries');
const gulpLoadPlugins = require('gulp-load-plugins');
const pa11y = require('gulp-pa11y');
const w3cValidation = require('gulp-w3c-html-validation');
const casperJs = require('gulp-casperjs');
const realFavicon = require('gulp-real-favicon');
const fs = require('fs'); // Used by check-for-favicon-update.
const plumber = require('gulp-plumber'); // For error handling.
const gutil = require('gulp-util'); // For error handling.
const postcss = require('gulp-postcss');
const cssNano = require('cssnano');
const atImport = require('postcss-import');
const runSequence = require('run-sequence');
const modernizr = require('gulp-modernizr');
// Project vars
// URL to test locally.
const localSiteURL = 'http://localhost:3000/';
// Name of the icon font.
const fontName = 'govstrap-icons';
// Favicon related settings.
const faviconColour = '#ffffff';
const faviconBackgroundColour = '#384249';
var options = {};
options.rootPath = {
project : __dirname + '/',
styleGuide : __dirname + '/styleguide/',
theme : __dirname + '/'
};
options.theme = {
root : options.rootPath.theme,
css : options.rootPath.theme + 'css/',
sass : options.rootPath.theme + 'src/sass/',
jsSrc : options.rootPath.theme + 'src/js/',
js : options.rootPath.theme + 'js/',
src : options.rootPath.theme + 'src/'
};
var sassFiles = [
options.theme.sass + '**/*.scss',
// Do not open Sass partials as they will be included as needed.
'!' + options.theme.sass + '**/_*.scss',
// Hide additional files
'!' + options.theme.sass + 'vendors/uikit.scss'
];
var $ = gulpLoadPlugins();
// ********************************************************************************************************************************************
// Error Handling to stop file watching from dying on an error (ie: Sass compiling).
var onError = function(err) {
gutil.beep();
console.log(err);
};
// Clean CSS files.
gulp.task('clean:css', function() {
return del([
options.theme.css + '**/*.css',
options.theme.css + '**/*.map',
'!' + options.theme.css + 'print.css'
], {force: true});
});
// Clean JS files.
gulp.task('clean:js', function() {
return del([
options.theme.js + 'dist/*.js',
], {force: true});
});
/**
* Generate css with source map.
*/
gulp.task('styles:generate:dev', function(){
return gulp.src(sassFiles)
.pipe($.sourcemaps.init())
.pipe($.sass({
sourcemap: true,
precision: 10
}).on('error', $.sass.logError))
.pipe($.sourcemaps.write('./'))
.pipe($.size({title: 'Styles'}))
.pipe(gulp.dest(options.theme.css));
});
/**
* Generate css without source map.
*/
gulp.task('styles:generate:prod', function(){
return gulp.src(sassFiles)
.pipe($.sass({
precision: 10,
sourcemap: false
}).on('error', $.sass.logError))
.pipe($.size({title: 'Styles (prod)'}))
.pipe(gulp.dest(options.theme.css));
});
/**
* Compress css.
*/
gulp.task('css:compress', function(){
return gulp.src(options.theme.css + '/*.css')
.pipe(cleanCSS({compatibility: 'ie10'}))
.pipe(gulp.dest(options.theme.css));
});
/**
* Break a css into separate files for each break point.
*/
gulp.task('css:break-at-media', function() {
return gulp.src(options.theme.css + '/styles.css')
.pipe(extractMediaQueries())
.pipe(groupMediaQueries())
.pipe(gulp.dest(options.theme.css));
});
/**
* Generate styles: source map = on, compression = off.
*/
gulp.task('styles:dev', function(callback) {
runSequence(
'clean:css',
'styles:generate:dev',
'css:break-at-media',
callback);
});
/**
* Generate JS
*/
gulp.task('js:build', function(callback) {
runSequence(
'clean:js',
'js:minify',
callback);
});
/**
* Compress JS.
*/
gulp.task('js:minify', function() {
gulp.src(options.theme.jsSrc + '*.js')
.pipe(plumber({
errorHandler: onError
}))
.pipe(uglify())
.pipe(gulp.dest(options.theme.js))
});
/**
* Generate styles: source map = off, compression = on.
*/
gulp.task('styles:prod', function(callback) {
runSequence(
'clean:css',
'styles:generate:prod',
'css:break-at-media',
'css:compress',
callback);
});
/**
* Modernizr
*/
gulp.task('modernizr', function() {
gulp.src(options.theme.css + 'js/*.js')
.pipe(modernizr())
.pipe(gulp.dest('./js/'))
});
/**
* Optimise favicons
*/
gulp.task('favicons', function() {
return gulp.src(options.theme.css + 'favicon/favicons/**/*')
.pipe(imagemin({
optimizationLevel: 3
}))
.pipe(gulp.dest('./favicons'))
});
/**
* Optimise images
*/
gulp.task('images', function() {
return gulp.src(options.theme.css + 'img/**/*')
.pipe(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true,
}))
.pipe(gulp.dest('./img'))
});
// SVG files to a font file.
gulp.task('iconFont', function() {
var runTimestamp = Math.round(Date.now() / 1000);
return gulp.src([options.theme.css + 'font-icons/**'])
.pipe(iconfontCss({
fontName: fontName,
path: 'scss',
targetPath: options.theme.css + 'sass/_' + fontName + '.scss', // Relative to the path used in gulp.dest()
fontPath: '/fonts/' // Relative to the site.
}))
.pipe(iconfont({
fontName: fontName, // Required.
prependUnicode: true, // Recommended option.
formats: ['ttf', 'eot', 'woff', 'woff2'], // Default, 'woff2' and 'svg' are available.
timestamp: runTimestamp, // Recommended to get consistent builds when watching files.
normalize: true, // The provided icons does not have the same height it could lead to unexpected results. Using the normalize option could solve the problem.
fontHeight: 1001, // Stops the SVG being redrawn like a 3yo did them.. (https://github.com/nfroidure/gulp-iconfont/issues/138)
}))
.pipe(gulp.dest('./fonts/'));
});
// Favicons creation
var faviconDataFile = options.theme.css + 'favicon/faviconData.json';
// Generate the icons. This task takes a few seconds to complete.
// You should run it at least once to create the icons. Then,
// you should run it whenever RealFaviconGenerator updates its
// package (see the check-for-favicon-update task below).
gulp.task('favicon', function(done) {
realFavicon.generateFavicon({
masterPicture: options.theme.css + 'master-favicon.svg',
dest: options.theme.css + 'favicon/favicons',
iconsPath: '/favicons/',
design: {
ios: {
pictureAspect: 'noChange',
assets: {
ios6AndPriorIcons: false,
ios7AndLaterIcons: false,
precomposedIcons: false,
declareOnlyDefaultIcon: true
}
},
desktopBrowser: {},
windows: {
pictureAspect: 'noChange',
backgroundColor: faviconBackgroundColour,
onConflict: 'override',
assets: {
windows80Ie10Tile: false,
windows10Ie11EdgeTiles: {
small: false,
medium: true,
big: false,
rectangle: false
}
}
},
androidChrome: {
pictureAspect: 'noChange',
themeColor: faviconColour,
manifest: {
display: 'standalone',
orientation: 'notSet',
onConflict: 'override',
declared: true
},
assets: {
legacyIcon: false,
lowResolutionIcons: false
}
},
safariPinnedTab: {
pictureAspect: 'silhouette',
themeColor: faviconColour
}
},
settings: {
scalingAlgorithm: 'Mitchell',
errorOnImageTooSmall: true
},
markupFile: faviconDataFile
}, function() {
done();
});
});
// Inject the favicon markups in your HTML pages. You should run
// this task whenever you modify a page. You can keep this task
// as is or refactor your existing HTML pipeline.
// gulp.task('inject-favicon-markups', function() {
// return gulp.src([ 'TODO: List of the HTML files where to inject favicon markups' ])
// .pipe(realFavicon.injectFaviconMarkups(JSON.parse(fs.readFileSync(faviconDataFile)).favicon.html_code))
// .pipe(gulp.dest('TODO: Path to the directory where to store the HTML files'));
// });
// Check for updates on RealFaviconGenerator
// (ie: If Apple has just released a new Touch icon along with the latest version of iOS).
// Run this task from time to time. Ideally, make it part of your CI.
gulp.task('check-for-favicon-update', function(done) {
var currentVersion = JSON.parse(fs.readFileSync(faviconDataFile)).version;
realFavicon.checkForUpdates(currentVersion, function(err) {
if (err) {
throw err;
}
});
});
// Clean all directories.
gulp.task('clean', ['clean:css', 'clean:js']);
// ********************************************************************************************************************************************
// Default gulp task.
gulp.task('default', ['clean', 'images', 'modernizr', 'styles:dev', 'js:build']);
// Development gulp task, kinda same as default
gulp.task('dev', ['clean', 'images', 'modernizr', 'styles:dev', 'js:build']);
// Production gulp task.
gulp.task('prod', ['clean', 'images', 'modernizr', 'styles:prod', 'js:build']);
// Watch changes.
gulp.task('watch', ['images', 'modernizr'], function() {
// Watch for govkit Sass changes.
gulp.watch([options.theme.sass + 'themes/govkit/*.scss'], ['styles:dev']);
// Watch for partials Sass changes.
gulp.watch([options.theme.sass + 'partials/*.scss'], ['styles:dev']);
// Watch for JS changes.
gulp.watch([options.theme.jsSrc + '*.js'], ['js:build']);
// Watch for img optim changes.
gulp.watch('./src/img/**', function() {
gulp.start('images');
});
// Watch for font icon changes.
gulp.watch('./src/font-icons/**', function() {
gulp.start('iconFont');
});
// Watch for master Favicon changes.
gulp.watch('./src/favicon/master-favicon.svg', function() {
gulp.start('favicon');
});
// Once the favicons are built, create an optimised copy to use.
gulp.watch('./src/favicon/favicons/**', function() {
gulp.start('favicons');
});
});