Skip to content

Commit af1cf2d

Browse files
committed
fix(sw): do not create empty SW when disabled
fixes: #78
1 parent d321d2f commit af1cf2d

2 files changed

Lines changed: 55 additions & 16 deletions

File tree

lib/broccoli-workbox.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-undef */
12
/* eslint-disable no-sync */
23
const glob = require('glob');
34
const fs = require('fs');
@@ -34,29 +35,46 @@ function BroccoliWorkbox(inputNodes, config) {
3435
this.workboxOptions = config.workboxOptions;
3536
}
3637

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+
3745
BroccoliWorkbox.prototype.build = function() {
3846
const workboxOptions = Object.assign({}, this.workboxOptions);
3947
const directory = this.inputPaths[0];
4048

49+
workboxOptions.swDest = path.join(directory, workboxOptions.swDest);
50+
4151
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;
4555
}
4656

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+
);
4961

5062
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+
);
5267

5368
// 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+
) {
5573
cleanPromise = cleanDir(workboxDirectory);
5674
}
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+
});
6078

6179
workboxOptions.importScripts = filesToIncludeInSW;
6280

node-tests/index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function assertFileExists(filePath) {
3838
assert.ok(fs.existsSync(filePath), `${filePath} exists`);
3939
}
4040

41+
function assertFileDoesNotExist(filePath) {
42+
assert.isNotOk(fs.existsSync(filePath), `${filePath} does not exist`);
43+
}
44+
4145
function assertContains(filePath, regexp) {
4246
const fileContent = fs.readFileSync(filePath, 'utf8');
4347

@@ -91,23 +95,40 @@ describe('Addon is enabled for production build', function() {
9195
describe('Addon is disabled for development', function() {
9296
this.timeout(TEST_TIMEOUT);
9397

94-
context('Precaches nothing and register serviceworker', () => {
98+
context('Precaches nothing and does not register serviceworker', () => {
9599
before(() => {
96100
mockConfig();
97-
return runEmberCommand(fixturePath, 'build');
101+
// eslint-disable-next-line max-nested-callbacks
102+
return runEmberCommand(fixturePath, 'build --prod').then(() => runEmberCommand(fixturePath, 'build'));
98103
});
99104

100105
after(() => {
101106
restoreConfig();
102107
return cleanup(fixturePath);
103108
});
104109

105-
it('produces a sw.js file', () => {
106-
assertFileExists(outputSWPath);
110+
it('does not produce a sw.js file', () => {
111+
assertFileDoesNotExist(outputSWPath);
112+
});
113+
});
114+
115+
context('Addon was enabled before and then disabled', () => {
116+
before(() => {
117+
mockConfig();
118+
return runEmberCommand(fixturePath, 'build --prod');
119+
});
120+
121+
after(() => {
122+
restoreConfig();
123+
return cleanup(fixturePath);
107124
});
108125

109-
it('precaches nothing', () => {
110-
assertContains(outputSWPath, /precacheManifest\s\=\s\[\]/);
126+
it('removes sw.js file', () => {
127+
assertFileExists(outputSWPath);
128+
// eslint-disable-next-line max-nested-callbacks
129+
runEmberCommand(fixturePath, 'build').finally(() => {
130+
assertFileDoesNotExist(outputSWPath);
131+
});
111132
});
112133
});
113134
});

0 commit comments

Comments
 (0)