Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 092742c

Browse files
committed
feat(ServiceWorker): add broccoli plugin to generate service worker
Paired with @alxhub Closes #6
1 parent 2f39734 commit 092742c

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
lines changed

angular-cli-build.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ var concat = require('broccoli-concat');
1717
*/
1818
global.Event = function () {};
1919
const AppShellPlugin = require('./broccoli-app-shell.ts');
20+
const ServiceWorkerPlugin = require('./broccoli-service-worker.ts');
2021

2122
module.exports = function(defaults) {
2223
var app = new Angular2App(defaults, {
2324
vendorNpmFiles: [
2425
'angularfire2/**/*.js',
25-
'firebase/lib/*.js',
26-
'rxjs/**/*.js',
27-
'@angular2-material/**/*.+(js|css|svg)',
28-
'material-design-icons/**/*.+(css|svg|woff|ttf|eot|woff2)'
26+
'firebase/lib/firebase-web.js',
27+
'rxjs/bundles/Rx.js',
28+
'@angular2-material/**/*.+(js|css|svg|map)',
29+
'material-design-icons/**/*.(woff2|woff)',
30+
'angular2-service-worker/dist/worker.js'
2931
]
3032
});
3133
const ngTree = app.toTree();
34+
var swTree = new ServiceWorkerPlugin(ngTree);
3235
var appShellIndex = new AppShellPlugin(ngTree, 'index.html', 'app/issue-cli');
3336
var concatExtrasTree = new Funnel('src/client', {
3437
include: [
@@ -37,7 +40,7 @@ module.exports = function(defaults) {
3740
]
3841
});
3942
var jsBundleTree = concat(mergeTrees([ngTree, concatExtrasTree]), {
40-
inputFiles: [
43+
headerFiles: [
4144
// TODO: use minified for production build
4245
'vendor/es6-shim/es6-shim.js',
4346
'vendor/systemjs/dist/system-polyfills.js',
@@ -46,15 +49,15 @@ module.exports = function(defaults) {
4649
'vendor/rxjs/bundles/Rx.js',
4750
'vendor/angular2/bundles/angular2.dev.js',
4851
'vendor/angular2/bundles/http.dev.js',
49-
'vendor/angular2/bundles/router.dev.js',
50-
'system.config.js'
52+
'vendor/angular2/bundles/router.dev.js'
5153
],
54+
inputFiles: ['system.config.js'],
5255
header: ';(function() {',
5356
footer: '}());',
5457
footerFiles: ['auto-start.js'],
5558
sourceMapConfig: { enabled: true },
5659
allowNone: false,
5760
outputFile: '/app-concat.js'
5861
});
59-
return mergeTrees([ngTree, appShellIndex, jsBundleTree], { overwrite: true })
62+
return mergeTrees([ngTree, appShellIndex, jsBundleTree, swTree], { overwrite: true })
6063
};

broccoli-service-worker.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const fse = require('fs-extra');
2+
const path = require('path');
3+
const BroccoliPlugin:BroccoliPluginConstructor = require('broccoli-caching-writer');
4+
const ngsw = require ('angular2-service-worker');
5+
const MANIFEST_NAME = 'manifest.appcache';
6+
const WORKER_NAME = 'worker.js';
7+
8+
interface BroccoliPluginConstructor {
9+
new(inputNodes:any[], options?:any): BroccoliPluginConstructor
10+
inputPaths: string[];
11+
outputPath: string;
12+
}
13+
14+
class SourceResolver {
15+
constructor(public inputPaths:string[]) {}
16+
resolve(sources:string[]): Object {
17+
return Promise.resolve(sources.reduce((prev, curr) => {
18+
prev[`/${path.relative(this.inputPaths[0], curr)}`] = fse.readFileSync(curr, 'utf-8');
19+
return prev;
20+
}, {}));
21+
}
22+
}
23+
24+
class ServiceWorkerPlugin extends BroccoliPlugin {
25+
constructor(inputNodes:any, options?:any) {
26+
super([inputNodes]);
27+
}
28+
29+
build() {
30+
var sourceResolver = new SourceResolver(this.inputPaths);
31+
var manifestWriter = new ngsw.ManifestWriter(sourceResolver);
32+
// TODO(jeffbcross): plugin assumes single input path right now.
33+
return manifestWriter.generate({
34+
group: [{
35+
name: 'app',
36+
sources: this.inputPaths
37+
.map(p => recursiveReaddirSync(p))
38+
.reduce((prev, curr) => prev.concat(curr), [])
39+
.filter(p => {
40+
var relativePath = path.relative(this.inputPaths[0], p);
41+
return relativePath !== MANIFEST_NAME && relativePath !== WORKER_NAME;
42+
})
43+
}]
44+
})
45+
.then(manifest => {
46+
fse.writeFileSync(path.join(this.outputPath, MANIFEST_NAME), manifest);
47+
})
48+
.then(() => {
49+
fse.writeFileSync(path.resolve(this.outputPath, WORKER_NAME), fse.readFileSync(path.resolve(this.inputPaths[0], 'vendor/angular2-service-worker/dist/worker.js')));
50+
});
51+
}
52+
}
53+
54+
module.exports = ServiceWorkerPlugin;
55+
56+
function recursiveReaddirSync(src) {
57+
var files = [];
58+
fse.readdirSync(src).forEach(function(res) {
59+
var child = path.join(src, res);
60+
var stat = fse.statSync(child);
61+
if (stat.isFile()) {
62+
files.push(child);
63+
} else if (stat.isDirectory()) {
64+
files = files.concat(recursiveReaddirSync(child));
65+
}
66+
})
67+
return files;
68+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"angular2-express-engine": "^0.10.0",
3838
"angular2-gulp-prerender": "^0.10.1",
3939
"angular2-hapi-engine": "^0.10.1",
40+
"angular2-service-worker": "^0.1.1",
4041
"angular2-universal": "^0.90.2",
4142
"angular2-universal-polyfills": "^0.3.4",
4243
"broccoli-caching-writer": "^2.2.1",

src/client/index.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
<base href="/">
77
{{content-for 'head'}}
88
<link rel="icon" type="image/x-icon" href="favicon.ico">
9-
10-
<!-- Service worker support is disabled by default.
11-
Install the worker script and uncomment to enable.
12-
Only enable service workers in production.
13-
<script type="text/javascript">
9+
<meta name="viewport" content="width=device-width, initial-scale=1">
10+
<!--<script type="text/javascript">
1411
if ('serviceWorker' in navigator) {
1512
navigator.serviceWorker.register('/worker.js').catch(function(err) {
1613
console.log('Error installing service worker: ', err);
1714
});
1815
}
19-
</script>
20-
-->
16+
</script>-->
17+
2118
<style>
2219
html,body {
2320
margin: 0;

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"files": [
1111
"typings/main.d.ts",
12-
"broccoli-app-shell.ts"
12+
"broccoli-app-shell.ts",
13+
"broccoli-service-worker.ts"
1314
]
1415
}

0 commit comments

Comments
 (0)