-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
52 lines (46 loc) · 1.64 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
const gulp = require('gulp');
const concat = require('gulp-concat');
const rename = require("gulp-rename");
const wbBuild = require('workbox-build');
const fs = require('fs');
const crypto = require('crypto');
function getHash(string) {
const md5 = crypto.createHash('md5');
md5.update(string);
return md5.digest('hex');
}
gulp.task('move-workbox', function() {
return gulp.src('./node_modules/workbox-sw/build/importScripts/workbox-sw.prod.v*.*.*.js')
.pipe(rename('workbox.js'))
.pipe(gulp.dest('./public/js/dist/'));
});
gulp.task('generate-wb-manifest', ['app'], function () {
return wbBuild.injectManifest({
swSrc: './public/js/src/precache-worker.js',
swDest: './public/js/dist/precache-worker.js',
globDirectory: './node_modules/todomvc/examples/vanillajs',
globIgnores: ['**/test/*'],
globPatterns: ['**\/*.{html,js,css,png,json}'],
manifestTransforms: [
(entries) => entries.map((entry) => {
if (entry.url.match(/app.js$/)) {
entry.revision = getHash(fs.readFileSync('./public/js/dist/app.js'))
} else if (entry.url.match(/index.html$/)) {
entry.revision = getHash(fs.readFileSync('./public/index.html'))
}
return entry;
})
]
});
});
gulp.task('service-worker', ['move-workbox', 'generate-wb-manifest'], function() {
return gulp.src(['./public/js/dist/precache-worker.js', './public/js/src/push-worker.js'])
.pipe(concat('sw.js'))
.pipe(gulp.dest('./public/js/dist'));
});
gulp.task('app', function() {
return gulp.src(['./public/js/src/app.js', './public/js/src/push-subscription.js'])
.pipe(concat('app.js'))
.pipe(gulp.dest('./public/js/dist'));
});
gulp.task('default', ['service-worker']);