|
1 | 1 | declare var require, Buffer; |
2 | 2 | const crypto = require('crypto'); |
3 | 3 |
|
| 4 | +export interface SwPluginConfig { |
| 5 | + manifestFile?: string; |
| 6 | + manifestKey?: string; |
| 7 | + baseHref?: string; |
| 8 | +} |
| 9 | + |
4 | 10 | /** |
5 | 11 | * Webpack plugin that generates a basic Angular service worker manifest. |
6 | 12 | */ |
7 | 13 | export class AngularServiceWorkerPlugin { |
8 | 14 |
|
9 | | - constructor(public manifestFile = 'ngsw-manifest.json', public manifestKey = 'static') {} |
| 15 | + public manifestFile: string; |
| 16 | + public manifestKey: string; |
| 17 | + public baseHref: string; |
| 18 | + |
| 19 | + constructor(config?: SwPluginConfig) { |
| 20 | + this.manifestFile = (config && config.manifestFile) || 'ngsw-manifest.json'; |
| 21 | + this.manifestKey = (config && config.manifestKey) || 'static'; |
| 22 | + this.baseHref = (config && config.baseHref) || '/'; |
| 23 | + if (!this.baseHref.endsWith('/')) { |
| 24 | + this.baseHref += '/'; |
| 25 | + } |
| 26 | + } |
10 | 27 |
|
11 | 28 | apply(compiler) { |
12 | 29 | // Determine the URL prefix under which all files will be served. |
13 | | - let publicPrefix = compiler.options.output.publicPath || ''; |
14 | 30 | compiler.plugin('emit', (compilation, callback) => { |
15 | 31 | // Manifest into which assets to be fetched will be recorded. This will either |
16 | 32 | // be read from the existing template or created fresh. |
@@ -40,7 +56,7 @@ export class AngularServiceWorkerPlugin { |
40 | 56 | .keys(compilation.assets) |
41 | 57 | .filter(key => key !== this.manifestFile) |
42 | 58 | .forEach(key => { |
43 | | - let url = `${publicPrefix}/${key}`; |
| 59 | + let url = `${this.baseHref}${key}`; |
44 | 60 | urls[url] = sha1(compilation.assets[key].source()); |
45 | 61 | }); |
46 | 62 |
|
|
0 commit comments