@@ -159,14 +159,6 @@ class Angular2App extends BroccoliPlugin {
159
159
merged = this . _getBundleTree ( merged ) ;
160
160
}
161
161
162
- if ( this . ngConfig . apps [ 0 ] . mobile ) {
163
- var ServiceWorkerPlugin = require ( '@angular/service-worker' ) . ServiceWorkerPlugin ;
164
- var swTree = new ServiceWorkerPlugin ( merged ) ;
165
- merged = BroccoliMergeTrees ( [ merged , swTree ] , {
166
- overwrite : true
167
- } ) ;
168
- }
169
-
170
162
return new BroccoliFunnel ( merged , {
171
163
destDir : this . _destDir ,
172
164
overwrite : true
@@ -416,6 +408,7 @@ class Angular2App extends BroccoliPlugin {
416
408
var indexContent = fs . readFileSync ( indexFile , 'utf8' ) ;
417
409
var scriptTagVendorFiles = indexContent . match ( / v e n d o r \/ [ ^ " ' ] * \. j s / gi) ;
418
410
var vendorTree = this . _getVendorNpmTree ( ) ;
411
+ var assetsTree = this . _getAssetsTree ( ) ;
419
412
420
413
var scriptTree = new BroccoliFunnel ( preBundleTree , {
421
414
include : scriptTagVendorFiles
@@ -452,9 +445,28 @@ class Angular2App extends BroccoliPlugin {
452
445
bundleTree = uglify ( bundleTree , {
453
446
mangle : false
454
447
} ) ;
455
- }
456
-
457
448
449
+ // Required here since the package isn't installed for non-mobile apps.
450
+ var ServiceWorkerPlugin = require ( '@angular/service-worker' ) . ServiceWorkerPlugin ;
451
+ // worker.js is needed so it can be copied to dist
452
+ var workerJsTree = new BroccoliFunnel ( jsTree , {
453
+ include : [ 'vendor/@angular/service-worker/dist/worker.js' ]
454
+ } ) ;
455
+ /**
456
+ * ServiceWorkerPlugin will automatically pre-fetch and cache every file
457
+ * in the tree it receives, so it should only receive the app bundle,
458
+ * and non-JS static files from the app. The plugin also needs to have
459
+ * the worker.js file available so it can copy it to dist.
460
+ **/
461
+ var swTree = new ServiceWorkerPlugin ( BroccoliMergeTrees ( [
462
+ bundleTree ,
463
+ assetsTree ,
464
+ workerJsTree
465
+ ] ) ) ;
466
+ bundleTree = BroccoliMergeTrees ( [ bundleTree , swTree ] , {
467
+ overwrite : true
468
+ } ) ;
469
+ }
458
470
459
471
return BroccoliMergeTrees ( [ nonJsTree , scriptTree , bundleTree ] , { overwrite : true } ) ;
460
472
}
0 commit comments