|
| 1 | +/* eslint-disable no-undef */ |
1 | 2 | /* eslint-disable no-sync */ |
2 | 3 | const glob = require('glob'); |
3 | 4 | const fs = require('fs'); |
@@ -34,29 +35,46 @@ function BroccoliWorkbox(inputNodes, config) { |
34 | 35 | this.workboxOptions = config.workboxOptions; |
35 | 36 | } |
36 | 37 |
|
| 38 | +BroccoliWorkbox.prototype.removeServiceWorker = function(filePath) { |
| 39 | + if (fs.existsSync(filePath)) { |
| 40 | + debug(yellow('Addon disabled. Service worker exist, remove it')); |
| 41 | + fs.unlinkSync(filePath); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
37 | 45 | BroccoliWorkbox.prototype.build = function() { |
38 | 46 | const workboxOptions = Object.assign({}, this.workboxOptions); |
39 | 47 | const directory = this.inputPaths[0]; |
40 | 48 |
|
| 49 | + workboxOptions.swDest = path.join(directory, workboxOptions.swDest); |
| 50 | + |
41 | 51 | if (!this.options.enabled) { |
42 | | - debug(yellow('Addon disabled. Generating empty service worker...')); |
43 | | - workboxOptions.globPatterns = []; |
44 | | - workboxOptions.runtimeCaching = []; |
| 52 | + debug(yellow('Addon disabled. Disable and remove service worker...')); |
| 53 | + this.removeServiceWorker(workboxOptions.swDest); |
| 54 | + return false; |
45 | 55 | } |
46 | 56 |
|
47 | | - workboxOptions.globDirectory = path.join(directory, workboxOptions.globDirectory); |
48 | | - workboxOptions.swDest = path.join(directory, workboxOptions.swDest); |
| 57 | + workboxOptions.globDirectory = path.join( |
| 58 | + directory, |
| 59 | + workboxOptions.globDirectory |
| 60 | + ); |
49 | 61 |
|
50 | 62 | let cleanPromise = Promise.resolve(); |
51 | | - const workboxDirectory = path.join(directory, `workbox-v${workboxBuildPkg.version}`); |
| 63 | + const workboxDirectory = path.join( |
| 64 | + directory, |
| 65 | + `workbox-v${workboxBuildPkg.version}` |
| 66 | + ); |
52 | 67 |
|
53 | 68 | // Remove workbox libraries directory to prevent exception on recopying it. |
54 | | - if (!workboxOptions.importWorkboxFromCDN && fs.existsSync(workboxDirectory)) { |
| 69 | + if ( |
| 70 | + !workboxOptions.importWorkboxFromCDN && |
| 71 | + fs.existsSync(workboxDirectory) |
| 72 | + ) { |
55 | 73 | cleanPromise = cleanDir(workboxDirectory); |
56 | 74 | } |
57 | | - const filesToIncludeInSW = glob.sync('assets/service-workers/*.js', |
58 | | - { cwd: workboxOptions.globDirectory } |
59 | | - ); |
| 75 | + const filesToIncludeInSW = glob.sync('assets/service-workers/*.js', { |
| 76 | + cwd: workboxOptions.globDirectory |
| 77 | + }); |
60 | 78 |
|
61 | 79 | workboxOptions.importScripts = filesToIncludeInSW; |
62 | 80 |
|
|
0 commit comments