forked from thedaviddias/Front-End-Checklist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
383 lines (321 loc) · 9.55 KB
/
gulpfile.babel.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
375
376
377
378
379
380
381
382
383
'use strict';
import path from 'path';
import fs from 'fs';
import gulp from 'gulp';
import runSequence from 'run-sequence';
import gutil from 'gulp-util';
import gulpIf from 'gulp-if';
import sourcemaps from 'gulp-sourcemaps';
import sass from 'gulp-sass';
import autoprefixer from 'gulp-autoprefixer';
import cssnano from 'gulp-cssnano';
import gcmq from 'gulp-group-css-media-queries';
const critical = require('critical').stream;
import pug from 'gulp-pug';
import data from 'gulp-data';
import htmlmin from 'gulp-htmlmin';
import webpack from "webpack";
import webpackStream from 'webpack-stream';
import webpackConfig from "./webpack.config.js";
import imagemin from 'gulp-imagemin';
import imageminWebp from 'imagemin-webp';
import browserSync from 'browser-sync';
import eslint from 'gulp-eslint';
import mocha from 'gulp-mocha';
import istanbul from 'gulp-istanbul';
import changed from 'gulp-changed';
import cached from 'gulp-cached';
import jsonConcat from 'gulp-json-concat';
import plumber from "gulp-plumber";
import htmlreplace from 'gulp-html-replace';
import cdnizer from "gulp-cdnizer";
import modernizr from 'modernizr';
import modernizrConfig from './modernizr-config.json';
import del from "del";
import rename from "gulp-rename";
import pkg from './package.json';
// ========================================
// VARIABLES
// ========================================
const dirs = {
root: '.',
src: 'src',
dest: 'dist'
};
const sassPaths = {
src: `${dirs.src}/styles/main.scss`,
dest: `${dirs.dest}/styles/`
};
const pugPaths = {
src: `${dirs.src}/views/index.pug`,
};
const isDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV == 'development';
var dir = './.tmp';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
fs.closeSync(fs.openSync('./.tmp/critical.min.css', 'w'));
}
// ========================================
// PUG / HTML
// ========================================
gulp.task('compile-pug', () => {
return gulp.src(pugPaths.src)
.pipe(plumber((error) => {
console.log(error);
this.emit('end');
}))
.pipe(data(() => {
return require('./data/en/_project.json');
}))
.pipe(pug({
locals: {},
pretty: true
}))
.pipe(gulp.dest(dirs.dest))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('pug-rebuild', ['compile-pug'], () => {
browserSync.reload();
});
gulp.task('minify-html', ['compile-pug'], () => {
return gulp.src('./dist/*.html')
.pipe(htmlreplace({
css: {
src: '/styles/main.min.css',
tpl: '<link rel="preload" href="%s" as="style" onload="this.rel=\'stylesheet\'">'
},
nocss: {
src: '/styles/main.min.css',
}
}))
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(gulp.dest(dirs.dest));
});
// ========================================
// CSS
// ========================================
gulp.task('compile-styles', () => {
return gulp.src(sassPaths.src)
.pipe(plumber((error) => {
console.log(error);
this.emit('end');
}))
.pipe(gulpIf(isDevelopment, sourcemaps.init()))
.pipe(sass.sync().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gcmq())
.pipe(cssnano())
// .pipe(uncss({
// html: [dirs.dest + '/**/*.html']
// }))
.pipe(rename({
suffix: ".min"
}))
.pipe(gulpIf(isDevelopment, sourcemaps.write('.')))
.pipe(gulp.dest(sassPaths.dest))
.pipe(browserSync.reload({stream: true}));
});
// Generate & Inline Critical-path CSS
gulp.task('critical', () => {
return gulp.src(dirs.dest + '/index.html')
.pipe(critical({
base: 'dist/',
inline: false,
dest: '../.tmp/critical.min.css',
css: 'dist/styles/main.min.css',
minify: true,
ignore: [/url\(/, '@font-face', /print/]
}))
.on('error', function(err) { gutil.log(gutil.colors.red(err.message)); })
.pipe(gulp.dest(dirs.dest));
});
gulp.task('lint-css', function lintCssTask() {
const gulpStylelint = require('gulp-stylelint');
return gulp.src(dirs.src + '/styles/**/*.scss')
.pipe(cached('css'))
.pipe(gulpStylelint({
reporters: [
{ formatter: 'string', console: true }
]
}));
});
// ========================================
// JAVASCRIPT
// ========================================
gulp.task('lint', () => {
return gulp.src(dirs.src + '/scripts/**/*.js')
.pipe(cached('lint'))
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.results(results => {
// Called once for all ESLint results.
console.log(`Total Results: ${results.length}`);
console.log(`Total Warnings: ${results.warningCount}`);
console.log(`Total Errors: ${results.errorCount}`);
}));
});
gulp.task("webpack", () => {
return gulp.src(dirs.src + '/scripts/main.js')
.pipe(plumber((error) => {
console.log(error);
this.emit('end');
}))
.pipe(webpackStream(webpackConfig, webpack))
.pipe(gulp.dest(dirs.dest + '/scripts/'))
.pipe(browserSync.reload({stream: true}));
});
// ========================================
// IMAGES
// ========================================
gulp.task('compress-images', ['compress-webp'], () => {
return gulp.src(dirs.src + '/img/**/*.{jpg,png}')
.pipe(imagemin([
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5})
]))
.pipe(gulp.dest(dirs.dest + '/img/'));
});
gulp.task('compress-webp', () => {
return gulp.src(dirs.src + '/img/**/*.webp')
.pipe(imagemin([
imageminWebp({quality: 70})
]))
.pipe(gulp.dest(dirs.dest + '/img/'));
});
// ========================================
// TDD
// ========================================
gulp.task("coverage", function () {
return gulp.src(dirs.src + '/scripts/**/*.js')
.pipe(istanbul({
includeUntested: true
}))
.pipe(istanbul.hookRequire());
});
gulp.task("report", function () {
gulp.src(dirs.src + '/scripts/**/*.js', { read: false })
.pipe(istanbul.writeReports());
});
gulp.task("mocha", function () {
return gulp.src("test/**/*.test.js", { read: false })
.pipe(mocha({
reporter: 'spec',
compilers: 'js:babel-core/register',
require: ['jsdom-global']
}));
});
// ========================================
// SERVER
// ========================================
gulp.task('browser-sync', () => {
browserSync({
server: {
baseDir: './dist',
index: 'index.html'
},
notify: false,
browser: false,
open: false
});
});
gulp.task('browser-reload', () => {
browserSync.reload();
});
// ========================================
// MISC
// ========================================
gulp.task('cdn', () => {
return gulp.src('./dist/index.html')
.pipe(cdnizer({
defaultCDNBase: "//everywhere-8a59.kxcdn.com",
allowRev: true,
files: [
'/scripts/app.bundle.js',
'/styles/main.min.css',
'/favicon-32x32.png',
'/favicon-16x16.png',
'/apple-touch-icon.png',
'/browserconfig.xml',
'/service-worker.js',
'/safari-pinned-tab.svg',
'/manifest.json',
'/img/social/facebook-banner.jpg',
'/img/logos/logo-front-end-checklist.jpg',
'/img/logos/logo-front-end-checklist.webp'
]
}))
.pipe(gulp.dest(dirs.dest));
});
gulp.task("clean-dist", () => {
return del(["./dist"], {force: true});
});
gulp.task("clean-tmp", () => {
return del(["./.tmp", './dist/index.css'], {force: true});
});
gulp.task("clean-coverage", () => {
return del(["./coverage"]);
});
gulp.task('json-rebuild', ['pug-rebuild'], () => {
gulp.src("./data/en/items/*.json")
.pipe(jsonConcat('./_items.json', (data) => {
return new Buffer(JSON.stringify(data));
}))
.pipe(gulp.dest('./data/en'))
.on('finish', () => {
return gulp.src([ "./data/en/_items.json", './data/en/project/*.json'])
.pipe(jsonConcat('./_project.json', (data) => {
return new Buffer(JSON.stringify(data));
}))
.pipe(gulp.dest('./data/en'));
})
.pipe(browserSync.reload({stream: true}));
});
gulp.task('copy', () => {
return gulp.src([
// Copy all files
`${dirs.src}/*.*`,
`${dirs.src}/_headers`,
`!${dirs.src}/modernizr-custom.min.js`,
], {
// Include hidden files by default
dot: true
})
.pipe(gulp.dest(dirs.dest));
});
gulp.task( 'modernizr', (done) => {
modernizr.build(modernizrConfig, (code) => {
fs.writeFile(`${dirs.src}/modernizr-custom.min.js`, code, done);
});
});
// ##########################################
// LIST TASKS
// ##########################################
gulp.task("watch", function () {
gulp.watch(dirs.src + '/styles/**/*.scss', ['lint-css', 'compile-styles']);
gulp.watch(dirs.src + '/views/**/*.pug', ['pug-rebuild']);
gulp.watch(['!'+ dirs.src + '/data/en/**/_*.json', dirs.src + '/data/**/*.json'], ['json-rebuild']); // When JSON files are updated, concatenate these
gulp.watch(dirs.src + '/data/**/_*.json', ['compile-pug']); // When JSON are updated, compile PUG files
gulp.watch(dirs.src + "/img/**/*", ["compress-images"]);
gulp.watch([dirs.src + '/scripts/**/*.js'], ['lint', 'webpack']);
gulp.watch(['test/**'], ['mocha']);
});
gulp.task("dev", ['compile-styles', 'json-rebuild', "browser-sync", "watch"]);
gulp.task("build", (done) => {
runSequence(
['json-rebuild', 'modernizr', "clean-dist"],
['lint-css'],
['minify-html', 'compile-styles', 'compress-images', 'webpack'],
['critical'],
['minify-html'],
['cdn'],
['clean-tmp'],
'copy',
done);
});
gulp.task("test", (done) => {
runSequence("clean-coverage", "coverage", "mocha", "report", done);
});
gulp.task("default", ["browser-sync", "watch"]);