@@ -159,14 +159,6 @@ class Angular2App extends BroccoliPlugin {
159159 merged = this . _getBundleTree ( merged ) ;
160160 }
161161
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-
170162 return new BroccoliFunnel ( merged , {
171163 destDir : this . _destDir ,
172164 overwrite : true
@@ -416,6 +408,7 @@ class Angular2App extends BroccoliPlugin {
416408 var indexContent = fs . readFileSync ( indexFile , 'utf8' ) ;
417409 var scriptTagVendorFiles = indexContent . match ( / v e n d o r \/ [ ^ " ' ] * \. j s / gi) ;
418410 var vendorTree = this . _getVendorNpmTree ( ) ;
411+ var assetsTree = this . _getAssetsTree ( ) ;
419412
420413 var scriptTree = new BroccoliFunnel ( preBundleTree , {
421414 include : scriptTagVendorFiles
@@ -452,9 +445,28 @@ class Angular2App extends BroccoliPlugin {
452445 bundleTree = uglify ( bundleTree , {
453446 mangle : false
454447 } ) ;
455- }
456-
457448
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+ }
458470
459471 return BroccoliMergeTrees ( [ nonJsTree , scriptTree , bundleTree ] , { overwrite : true } ) ;
460472 }
0 commit comments