Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit a1feab7

Browse files
author
Matt Gaunt
committed
Using sw-precache to generate the service worker file
1 parent 586a066 commit a1feab7

File tree

6 files changed

+59
-144
lines changed

6 files changed

+59
-144
lines changed

gulp-tasks/scripts.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ gulp.task('scripts:watch', function() {
3939

4040
// Takes a set of objects defining inputs of javascript files
4141
// to run through browserify and babelify
42-
function compileES6Bundles(browserifyBundles, minify) {
42+
function compileES6Bundles(browserifyBundles, cb) {
43+
var finishedCount = 0;
4344
browserifyBundles.forEach(function(bundle) {
4445
var browserifyBundle = browserify({
4546
entries: [bundle.srcPath]
@@ -55,14 +56,21 @@ function compileES6Bundles(browserifyBundles, minify) {
5556
// If this is a production build - minify JS
5657
.pipe(gulpif(GLOBAL.config.env === 'prod', streamify(uglify())))
5758
.pipe(license(GLOBAL.config.license, GLOBAL.config.licenseOptions))
58-
.pipe(gulp.dest(bundle.dest));
59+
.pipe(gulp.dest(bundle.dest))
60+
.on('end', function() {
61+
finishedCount++;
62+
63+
if (finishedCount === browserifyBundles.length) {
64+
cb();
65+
}
66+
});
5967
});
6068
}
6169

6270
// This takes a source path and finds all files ending
6371
// with .es6.js and creates the bundles to run through browserify
6472
// and babelify
65-
function generateES6Bundles(srcPath) {
73+
function generateES6Bundles(srcPath, cb) {
6674
if (!srcPath) {
6775
throw new Error('Invalid source path given to generateES6Bundles');
6876
}
@@ -88,7 +96,7 @@ function generateES6Bundles(srcPath) {
8896
});
8997
});
9098

91-
compileES6Bundles(browserifyBundles);
99+
compileES6Bundles(browserifyBundles, cb);
92100
}
93101

94102
gulp.task('scripts:eslint', function() {
@@ -108,9 +116,7 @@ gulp.task('scripts:eslint', function() {
108116
});
109117

110118
gulp.task('scripts:es6', function(cb) {
111-
generateES6Bundles(GLOBAL.config.src);
112-
113-
cb();
119+
generateES6Bundles(GLOBAL.config.src, cb);
114120
});
115121

116122
gulp.task('scripts:es5', function() {

gulp-tasks/service-worker.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
*
3+
* Copyright 2015 Google Inc. All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the 'License');
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an 'AS IS' BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
var gulp = require('gulp');
19+
var path = require('path');
20+
var swPrecache = require('sw-precache');
21+
22+
gulp.task('service-worker:watch', function(cb) {
23+
gulp.watch(GLOBAL.config.src + '/**/*.*', ['service-worker']);
24+
});
25+
26+
gulp.task('service-worker', function(cb) {
27+
swPrecache.write(path.join(GLOBAL.config.dest, 'sw.js'), {
28+
staticFileGlobs: [
29+
GLOBAL.config.dest + '/**/*.{js,html,css,png,jpg,jpeg,gif,svg}',
30+
GLOBAL.config.dest + '/manifest.json',
31+
],
32+
dynamicUrlToDependencies: {
33+
'/app-shell': ['server/layouts/app-shell.handlebars'],
34+
'/partials/': ['server/layouts/partial.handlebars', 'server/views/index.handlebars'],
35+
'/partials/url-1': ['server/layouts/partial.handlebars', 'server/views/url-1.handlebars'],
36+
'/partials/url-2': ['server/layouts/partial.handlebars', 'server/views/url-2.handlebars'],
37+
},
38+
stripPrefix: GLOBAL.config.dest,
39+
navigateFallback: '/app-shell'
40+
}, cb);
41+
});

gulp-tasks/third_party.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

gulpfile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ GLOBAL.config = {
3434
},
3535
};
3636

37-
var allTasks = ['styles', 'scripts', 'copy', 'html', 'images', 'third_party'];
37+
var allTasks = ['styles', 'scripts', 'copy', 'html', 'images'];
3838

3939
gulp.task('default', function(cb) {
4040
runSequence(
4141
'clean',
4242
'bump',
4343
allTasks,
44+
'service-worker',
4445
cb);
4546
});
4647

4748
gulp.task('dev', function() {
4849
GLOBAL.config.env = 'dev';
49-
return runSequence('clean', allTasks, 'watch');
50+
return runSequence('clean', allTasks, 'service-worker', 'watch');
5051
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app-shell",
3-
"version": "0.1.159",
3+
"version": "0.1.174",
44
"private": true,
55
"license": "Apache",
66
"engines": {
@@ -31,6 +31,7 @@
3131
"gulp-util": "^3.0.6",
3232
"require-dir": "^0.3.0",
3333
"run-sequence": "^1.1.4",
34+
"sw-precache": "^2.2.0",
3435
"vinyl-source-stream": "^1.1.0"
3536
}
3637
}

src/sw.es6.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)