-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
425 lines (388 loc) · 12.3 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
*
* Based on Google Web Starter Kit
* Improved by Bigger Picture Agency
* (c) 2019 https://www.biggerpicture.agency
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*
*/
'use strict';
// This gulpfile makes use of new JavaScript features.
// Babel handles this without us having to do anything. It just works.
// You can read more about the new JavaScript features here:
// https://babeljs.io/docs/learn-es2015/
const { watch, series, parallel, task, src, dest } = require('gulp');
import path from 'path';
import del from 'del';
import browserSync from 'browser-sync';
import swPrecache from 'sw-precache';
import gulpLoadPlugins from 'gulp-load-plugins';
import pkg from './package.json';
import scriptsConfig from './javascripts.config.json';
import svgstore from 'gulp-svgstore';
import svgmin from 'gulp-svgmin';
import rename from 'gulp-rename';
import jshint from 'gulp-jshint';
import replace from 'gulp-replace';
const AUTOPREFIXER_BROWSERS = [
'ie >= 11',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
const $ = gulpLoadPlugins();
const server = browserSync.create();
const stylish = require('jshint-stylish');
function serve(done) {
server.init({
open: false,
notify: false,
// Customize the Browsersync console logging prefix
logPrefix: 'BP',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main'],
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
// server: ['.tmp', 'app'],
server: ['.tmp'],
port: 3000
});
done();
}
function reload(done) {
server.reload();
done();
}
let scriptHtmlBody = '';
if (scriptsConfig.body.length) {
for (var i = 0; i < scriptsConfig.body.length; i++) {
scriptHtmlBody += `<script src="${scriptsConfig.body[i]}"></script>`;
}
}
// templates - variables replacement
task('templates', (cb) => {
src('app/*.html')
.pipe(replace('@BodyJS', scriptHtmlBody))
.pipe(replace('@Timestamp', Date.now() ))
.pipe(dest('.tmp/'));
cb();
});
// templates build
task('templates-build', (cb) => {
src('dist/*.html')
.pipe(replace('@BodyJS', scriptHtmlBody))
.pipe(replace('@Timestamp', Date.now() ))
.pipe(dest('dist/'));
cb();
});
// Lint JavaScript
task('jsLinter', (cb) => {
src(['./app/scripts/**/*.js', './app/scripts/**/*.es6'])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {beep: true}));
cb();
});
// Optimize images
task('images', (cb) => {
src('app/images/**/*')
.pipe(dest('dist/images'))
.pipe($.size({title: 'images'}));
cb();
});
// Copy images to .tmp folder while developing
task('copyImagesDev', (cb) => {
src('app/images/**/*')
.pipe(dest('.tmp/images'));
cb();
});
// Copy fonts to .tmp folder while developing
task('copyFontsDev', (cb) => {
src('app/fonts/**/*')
.pipe(dest('.tmp/fonts'));
cb();
});
// Copy all files at the root level (app)
task('copy', (cb) =>
src([
'app/*',
'!app/*.html'
], {
dot: true
})
.pipe(dest('dist'))
.pipe($.size({title: 'copy'}))
);
// Copy fonts
task('copy-fonts', (cb) => {
src([
'app/fonts/**/*'
], {
dot: true
})
.pipe(dest('dist/fonts'))
.pipe($.size({title: 'copy fonts'}));
cb();
});
// Sass compilation
task('styles', (cb) => {
src('app/styles/*.scss')
.pipe($.newer('.tmp/styles'))
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'compressed',
precision: 6
}).on('error', $.sass.logError))
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe(dest('.tmp/styles'))
// Concatenate and minify styles
.pipe($.if('*.css', $.cssnano({
autoprefixer: {browsers: AUTOPREFIXER_BROWSERS, add: true},
reduceIdents: false
})))
.pipe($.size({title: 'styles'}))
.pipe($.sourcemaps.write('./'))
.pipe(dest('dist/styles'))
.pipe(dest('.tmp/styles'));
cb();
});
task('styles:dev', (cb) => {
src('app/styles/*.scss')
.pipe($.newer('.tmp/styles'))
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'nested'
}).on('error', $.sass.logError))
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe(dest('.tmp/styles'))
.pipe($.size({title: 'styles'}))
.pipe($.sourcemaps.write('./'))
.pipe(dest('.tmp/styles'))
.pipe(server.reload({stream: true}));
cb();
});
// Concatenate and minify JavaScript. Optionally transpiles ES2015 code to ES5.
// to enable ES2015 support remove the line `"only": "gulpfile.babel.js",` in the
// `.babelrc` file.
task('scripts', (cb) => {
src(scriptsConfig.ownJs)
//.pipe($.newer('.tmp/scripts'))
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
.pipe(dest('.tmp/scripts'))
.pipe($.concat('main.min.js'))
.pipe($.uglify({preserveComments: false}))
// Output files
.pipe($.size({title: 'scripts'}))
.pipe($.sourcemaps.write('.'))
.pipe(dest('dist/scripts'))
.pipe(dest('.tmp/scripts'));
cb();
});
task('scripts:vendor', (cb) => {
src(scriptsConfig.vendor)
//.pipe($.newer('.tmp/scripts'))
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
.pipe(dest('.tmp/scripts'))
.pipe($.concat('vendor.min.js'))
.pipe($.uglify({preserveComments: false}))
// Output files
.pipe($.size({title: 'scripts'}))
.pipe($.sourcemaps.write('.'))
.pipe(dest('dist/scripts'))
.pipe(dest('.tmp/scripts'));
cb();
});
// Copy Vendor Script from app/scripts that you want to load separately, not concatenated to main.min.js
task('scripts:vendor-copy', (cb) => {
src([
'app/scripts/vendor/lazyload/lazyload.min.js',
'app/scripts/vendor/lazyload/lazyload-intersection-observer.min.js'
])
.pipe(dest('dist/scripts'))
.pipe(dest('.tmp/scripts'));
cb();
});
// Concatenate minified Barba.js with uglified and minified scripts from dist/main.min.js (created via `scripts` task)
// to fix some of the IE11 issues (minified Barba.js cannot be uglified with the rest of the scripts).
// If you use Barba.js in your project, please run this task in `gulp` build task after the lint, html and scripts tasks
task('scripts-merge-with-barba', (cb) => {
src(['./node_modules/barba.js/dist/barba.min.js', './dist/scripts/vendor.min.js'])
.pipe($.concat('vendor.min.js'))
.pipe(dest('dist/scripts'))
.pipe(dest('.tmp/scripts'));
cb();
});
// transpilation only own JS files
task('scripts:dev', (cb) => {
src(
scriptsConfig.vendor.concat(scriptsConfig.ownJs),
)
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
.pipe($.size({title: 'scripts'}))
.pipe($.sourcemaps.write('.'))
.pipe(dest('.tmp/scripts'));
cb();
});
// scripts just for develop - without minification and concatenation
task('scripts:serve', (cb) => {
src(scriptsConfig.ownJs)
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
// Output files
.pipe($.size({title: 'scripts'}))
.pipe($.sourcemaps.write('.'))
.pipe(dest('.tmp/scripts'));
cb();
});
// Scan your HTML for assets & optimize them
task('html', (cb) => {
src('app/**/*.html')
.pipe($.useref({
searchPath: '{.tmp,app}',
noAssets: true
}))
// Minify any HTML
.pipe($.if('*.html', $.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true
})))
// Output files
.pipe(replace('@BodyJS', ''))
.pipe($.if('*.html', $.size({title: 'html', showFiles: true})))
.pipe(dest('dist'));
cb();
});
// all images/icons/*.svg files into one file
task('svgstore', function (cb) {
src('app/images/icons/*.svg')
.pipe(svgmin((file) => {
var prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}]
}
}))
.pipe(rename({prefix: 'icon-'}))
.pipe(svgstore())
.pipe(dest('app/images'))
.pipe(dest('.tmp/images'))
.pipe(dest('dist/images'));
cb();
});
// Clean output directory
task('clean', () => del(['.tmp', 'dist/*', '!dist/.git'], {dot: true}));
// Copy over the scripts that are used in importScripts as part of the generate-service-worker task.
task('copy-sw-scripts', () =>
src(['node_modules/sw-toolbox/sw-toolbox.js', 'app/scripts/sw/runtime-caching.js'])
.pipe(dest('dist/scripts/sw'))
);
task('write-service-worker', (cb) => {
const rootDir = 'dist';
const filepath = path.join(rootDir, 'service-worker.js');
swPrecache.write(filepath, {
// Used to avoid cache conflicts when serving on localhost.
cacheId: 'webdevinsider-demo-barba-before',
// sw-toolbox.js needs to be listed first. It sets up methods used in runtime-caching.js.
importScripts: [
'scripts/sw/sw-toolbox.js',
'scripts/sw/runtime-caching.js'
],
staticFileGlobs: [
// Add/remove glob patterns to match your directory setup.
`${rootDir}/images/**/*`,
`${rootDir}/scripts/**/*.js`,
`${rootDir}/styles/**/*.css`,
`${rootDir}/*.html`
],
// Translates a static file path to the relative URL that it's served from.
// This is '/' rather than path.sep because the paths returned from
// glob always use '/'.
stripPrefix: rootDir + '/'
}, cb);
});
// See http://www.html5rocks.com/en/tutorials/service-worker/introduction/ for
// an in-depth explanation of what service workers are and why you should care.
// Generate a service worker file that will provide offline functionality for
// local resources. This should only be done for the 'dist' directory, to allow
// live reload to work as expected when serving from the 'app' directory.
task('generate-service-worker', series('copy-sw-scripts', 'write-service-worker'));
// Watch files for changes & reload
const watchStyles = () => watch(['app/styles/**/*.{scss,css}'], series('styles:dev'));
const watchTemplates = () => watch(['app/*.html'], series('templates', reload));
const watchScripts = () => watch(['app/scripts/**/*.js', 'app/scripts/**/*.es6'], series('scripts:serve', 'jsLinter', reload));
const watchImages = () => watch(['app/images/**/*', '!app/images/**/*.svg'], series('copyImagesDev', reload));
const watchIcons = () => watch(['app/images/icons/**/*'], series('svgstore', reload));
const watchFonts = () => watch(['app/fonts/**/*'], series('copyFontsDev', reload));
task('watch', parallel(serve, watchStyles, watchTemplates, watchScripts, watchImages, watchIcons, watchFonts));
task('buildForDev', series('styles:dev', 'templates', 'scripts:vendor-copy', 'scripts:dev', 'copyFontsDev', 'copyImagesDev', 'svgstore'));
task('serve', series('buildForDev', 'watch'));
// Build production files, the default task
task('default', series(
'clean',
series(
'styles',
'html',
'scripts:vendor',
'scripts',
'scripts:vendor-copy',
'images',
'svgstore',
'copy',
'copy-fonts',
'templates-build',
),
//'scripts-merge-with-barba',
'generate-service-worker'
));
// Build and serve the output from the dist build
task('serve:dist', parallel('default', (cb) => {
browserSync({
open: false,
notify: false,
logPrefix: 'BP',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main'],
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: 'dist',
port: 3001
});
cb();
}));