Skip to content

Commit 41f3087

Browse files
rkostrzewskigoldhand
authored andcommitted
fix: Fix errors for MultiCompilers build (#78)
Runs webpack callback only once, fixes #74
1 parent bf14a8d commit 41f3087

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ class SWPrecacheWebpackPlugin {
7070
// sw-precache needs physical files to reference so we MUST wait until after assets are emitted before generating the service-worker.
7171
compiler.plugin('after-emit', (compilation, callback) => {
7272
this.configure(compiler, compilation); // configure the serviceworker options
73-
74-
const done = () => callback();
75-
const error = (err) => callback(err);
73+
this.checkWarnings(compilation);
7674

7775
// generate service worker then write to file system
7876
this.createServiceWorker()
79-
.then(serviceWorker => this.writeServiceWorker(serviceWorker, compiler, callback))
80-
.then(this.checkWarnings(compilation))
81-
.then(done, error);
77+
.then(serviceWorker => this.writeServiceWorker(serviceWorker, compiler))
78+
.then(() => callback())
79+
.catch(err => callback(err));
8280
});
8381
}
8482

@@ -196,12 +194,18 @@ class SWPrecacheWebpackPlugin {
196194
});
197195
}
198196

199-
writeServiceWorker(serviceWorker, compiler, callback) {
197+
writeServiceWorker(serviceWorker, compiler) {
198+
const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => {
199+
return err ? reject(err) : resolve(result);
200+
}));
201+
const mkdirp = promisify(compiler.outputFileSystem.mkdirp);
202+
const writeFile = promisify(compiler.outputFileSystem.writeFile);
203+
200204
const {filepath} = this.workerOptions;
201205

202206
// use the outputFileSystem api to manually write service workers rather than adding to the compilation assets
203-
return compiler.outputFileSystem.mkdirp(path.resolve(filepath, '..'),
204-
() => compiler.outputFileSystem.writeFile(filepath, serviceWorker, callback));
207+
return mkdirp(path.resolve(filepath, '..'))
208+
.then(() => writeFile(filepath, serviceWorker));
205209
}
206210

207211
/**

test/plugin.spec.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,12 @@ test.serial('#createServiceWorker()', async t => {
196196
plugin.configure(compiler, compilation);
197197
return callback();
198198
});
199-
200199
await runCompiler(compiler);
201-
202200
t.truthy(await plugin.createServiceWorker(), 'generate something');
203201
});
204202

205-
test.serial('#writeServiceWorker(serviceWorker, compiler, callback)', async t => {
203+
test.serial('#writeServiceWorker(serviceWorker, compiler)', async t => {
206204
t.plan(2);
207-
208205
const filepath = path.resolve(__dirname, 'tmp/service-worker.js');
209206
const compiler = webpack(webpackConfig());
210207
const plugin = new SWPrecacheWebpackPlugin({filepath});
@@ -215,10 +212,12 @@ test.serial('#writeServiceWorker(serviceWorker, compiler, callback)', async t =>
215212
plugin.apply(compiler);
216213

217214
compiler.plugin('after-emit', (compilation, callback) => {
218-
plugin.writeServiceWorker(serviceWorker, compiler, callback);
215+
plugin.writeServiceWorker(serviceWorker, compiler)
216+
.then(() => callback())
217+
.catch(err => callback(err));
219218
});
220-
await runCompiler(compiler);
221219

220+
await runCompiler(compiler);
222221
t.truthy(await fsExists(filepath), 'service-worker should exist');
223222

224223
});
@@ -257,7 +256,7 @@ test.serial('importScripts[<index>] should support entry point & dynamically imp
257256
'some-script-path.js',
258257
{filename: 'some-script-path.[hash].js'},
259258
{chunkName: 'sw'},
260-
{chunkName: 'service-worker-imported-script-2'}
259+
{chunkName: 'service-worker-imported-script-2'},
261260
],
262261
});
263262

@@ -313,9 +312,11 @@ test.serial('should keep [hash] in importScripts after configuring SW', async t
313312
const plugin = new SWPrecacheWebpackPlugin({filepath, importScripts: ['some_sw-[hash].js']});
314313

315314
plugin.apply(compiler);
316-
compiler.plugin('after-emit', (compilation) => {
315+
compiler.plugin('after-emit', (compilation, callback) => {
317316
plugin.configure(compiler, compilation);
317+
callback();
318318
});
319+
319320
await runCompiler(compiler);
320321

321322
t.truthy(plugin.options.importScripts[0] === 'some_sw-[hash].js', 'hash should be preserve after writing the sw');
@@ -329,8 +330,9 @@ test.serial('should not modify importScripts value when no [hash] is provided',
329330
const plugin = new SWPrecacheWebpackPlugin({filepath, importScripts: ['some_script.js']});
330331

331332
plugin.apply(compiler);
332-
compiler.plugin('after-emit', (compilation) => {
333+
compiler.plugin('after-emit', (compilation, callback) => {
333334
plugin.configure(compiler, compilation);
335+
callback();
334336
});
335337
await runCompiler(compiler);
336338

0 commit comments

Comments
 (0)