@@ -20,7 +20,8 @@ import { main as ngc } from '@angular/compiler-cli/src/main'
20
20
import { CompressionPlugin } from '../fusebox/compression.plugin'
21
21
import { appEnvironmentVariables } from '../utilities/environment-variables'
22
22
import { 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'
24
25
import clearTerminal from '../utilities/clear'
25
26
import readConfig_ from '../utilities/read-config'
26
27
@@ -31,7 +32,7 @@ command(
31
32
return args
32
33
} ,
33
34
args => {
34
- serve ( args . prod )
35
+ serve ( args . prod , args . sw )
35
36
}
36
37
)
37
38
. option ( 'prod' , {
@@ -51,7 +52,7 @@ function logServeCommandStart() {
51
52
logInfo ( 'Launching Serve Command' )
52
53
}
53
54
54
- function serve ( isProdBuild = false ) {
55
+ function serve ( isProdBuild = false , isServiceWorkerEnabled = false ) {
55
56
readConfig_ ( )
56
57
. pipe (
57
58
tap ( logServeCommandStart ) ,
@@ -83,6 +84,7 @@ function serve(isProdBuild = false) {
83
84
useTypescriptCompiler : true ,
84
85
plugins : [
85
86
isAotBuild && NgAotFactoryPlugin ( ) ,
87
+ isServiceWorkerEnabled && NgSwPlugin ( ) ,
86
88
Ng2TemplatePlugin ( ) ,
87
89
[ '*.component.html' , RawPlugin ( ) ] ,
88
90
WebIndexPlugin ( {
@@ -157,11 +159,32 @@ function serve(isProdBuild = false) {
157
159
// tslint:disable-next-line:no-let
158
160
let prevServerProcess : FuseProcess
159
161
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
+
160
179
fuseBrowser
161
180
. bundle ( 'vendor' )
162
181
. watch ( watchDir )
163
182
. instructions ( ` ~ ${ browserModule } ` )
164
183
. completed ( fn => {
184
+ isServiceWorkerEnabled &&
185
+ execSync (
186
+ `node_modules/.bin/ngsw-config .dist/public src/app/ngsw.json`
187
+ )
165
188
fuseServer
166
189
. bundle ( 'server' )
167
190
. instructions ( ` > [${ config . fusebox . server . serverModule } ]` )
@@ -200,6 +223,8 @@ function serve(isProdBuild = false) {
200
223
process . exit ( 1 )
201
224
} )
202
225
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
+ } )
204
229
} )
205
230
}
0 commit comments