@@ -20,7 +20,8 @@ import { main as ngc } from '@angular/compiler-cli/src/main'
2020import { CompressionPlugin } from '../fusebox/compression.plugin'
2121import { appEnvironmentVariables } from '../utilities/environment-variables'
2222import { renderSassDir } from '../utilities/sass'
23- import { exec } from 'child_process'
23+ import { exec , execSync } from 'child_process'
24+ import { NgSwPlugin } from '../fusebox/ng.sw.plugin'
2425import clearTerminal from '../utilities/clear'
2526import readConfig_ from '../utilities/read-config'
2627
@@ -31,7 +32,7 @@ command(
3132 return args
3233 } ,
3334 args => {
34- serve ( args . prod )
35+ serve ( args . prod , args . sw )
3536 }
3637)
3738 . option ( 'prod' , {
@@ -51,7 +52,7 @@ function logServeCommandStart() {
5152 logInfo ( 'Launching Serve Command' )
5253}
5354
54- function serve ( isProdBuild = false ) {
55+ function serve ( isProdBuild = false , isServiceWorkerEnabled = false ) {
5556 readConfig_ ( )
5657 . pipe (
5758 tap ( logServeCommandStart ) ,
@@ -83,6 +84,7 @@ function serve(isProdBuild = false) {
8384 useTypescriptCompiler : true ,
8485 plugins : [
8586 isAotBuild && NgAotFactoryPlugin ( ) ,
87+ isServiceWorkerEnabled && NgSwPlugin ( ) ,
8688 Ng2TemplatePlugin ( ) ,
8789 [ '*.component.html' , RawPlugin ( ) ] ,
8890 WebIndexPlugin ( {
@@ -157,11 +159,32 @@ function serve(isProdBuild = false) {
157159 // tslint:disable-next-line:no-let
158160 let prevServerProcess : FuseProcess
159161
162+ const fuseSw = FuseBox . init ( {
163+ homeDir : resolve ( 'node_modules/@angular/service-worker' ) ,
164+ output : `${ browserOutput } /$name.js` ,
165+ target : 'browser@es5' ,
166+ plugins : [
167+ isProdBuild &&
168+ QuantumPlugin ( {
169+ warnings : false ,
170+ uglify : config . fusebox . browser . prod . uglify ,
171+ treeshake : config . fusebox . browser . prod . treeshake ,
172+ bakeApiIntoBundle : 'ngsw-worker'
173+ } ) ,
174+ CompressionPlugin ( )
175+ ] as any
176+ } )
177+ fuseSw . bundle ( 'ngsw-worker' ) . instructions ( ' > [ngsw-worker.js]' )
178+
160179 fuseBrowser
161180 . bundle ( 'vendor' )
162181 . watch ( watchDir )
163182 . instructions ( ` ~ ${ browserModule } ` )
164183 . completed ( fn => {
184+ isServiceWorkerEnabled &&
185+ execSync (
186+ `node_modules/.bin/ngsw-config .dist/public src/app/ngsw.json`
187+ )
165188 fuseServer
166189 . bundle ( 'server' )
167190 . instructions ( ` > [${ config . fusebox . server . serverModule } ]` )
@@ -200,6 +223,8 @@ function serve(isProdBuild = false) {
200223 process . exit ( 1 )
201224 } )
202225
203- fuseBrowser . run ( { chokidar : { ignored : / ^ ( .* \. s c s s $ ) * $ / gim } } )
226+ fuseSw . run ( ) . then ( ( ) => {
227+ fuseBrowser . run ( { chokidar : { ignored : / ^ ( .* \. s c s s $ ) * $ / gim } } )
228+ } )
204229 } )
205230}
0 commit comments