Skip to content

Commit f11b8df

Browse files
fix: initial work on service worker intrgration
1 parent 237e7e0 commit f11b8df

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/commands/serve.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { main as ngc } from '@angular/compiler-cli/src/main'
2020
import { CompressionPlugin } from '../fusebox/compression.plugin'
2121
import { appEnvironmentVariables } from '../utilities/environment-variables'
2222
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'
2425
import clearTerminal from '../utilities/clear'
2526
import 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: /^(.*\.scss$)*$/gim } })
226+
fuseSw.run().then(() => {
227+
fuseBrowser.run({ chokidar: { ignored: /^(.*\.scss$)*$/gim } })
228+
})
204229
})
205230
}

src/fusebox/ng.sw.plugin.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { File } from 'fuse-box/core/File'
2+
3+
// tslint:disable:no-class
4+
// tslint:disable:no-this
5+
// tslint:disable:no-if-statement
6+
// tslint:disable:no-object-mutation
7+
// tslint:disable:readonly-keyword
8+
9+
const defaults = {}
10+
11+
export interface NgSwPluginOptions {}
12+
13+
export class NgSwPluginClass {
14+
public test: RegExp = /app.browser.module/
15+
16+
constructor(public opts: NgSwPluginOptions = defaults) {}
17+
18+
transform(file: File) {
19+
const regex = new RegExp(/enabled: false/, 'g')
20+
file.contents = file.contents.replace(regex, 'enabled: true')
21+
}
22+
}
23+
24+
export const NgSwPlugin = (options?: NgSwPluginOptions) =>
25+
new NgSwPluginClass(options)

0 commit comments

Comments
 (0)