Skip to content

Commit

Permalink
fix: Fix errors for MultiCompilers build (#78)
Browse files Browse the repository at this point in the history
Runs webpack callback only once, fixes #74
  • Loading branch information
rkostrzewski authored and goldhand committed May 31, 2017
1 parent bf14a8d commit 41f3087
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ class SWPrecacheWebpackPlugin {
// sw-precache needs physical files to reference so we MUST wait until after assets are emitted before generating the service-worker.
compiler.plugin('after-emit', (compilation, callback) => {
this.configure(compiler, compilation); // configure the serviceworker options

const done = () => callback();
const error = (err) => callback(err);
this.checkWarnings(compilation);

// generate service worker then write to file system
this.createServiceWorker()
.then(serviceWorker => this.writeServiceWorker(serviceWorker, compiler, callback))
.then(this.checkWarnings(compilation))
.then(done, error);
.then(serviceWorker => this.writeServiceWorker(serviceWorker, compiler))
.then(() => callback())
.catch(err => callback(err));
});
}

Expand Down Expand Up @@ -196,12 +194,18 @@ class SWPrecacheWebpackPlugin {
});
}

writeServiceWorker(serviceWorker, compiler, callback) {
writeServiceWorker(serviceWorker, compiler) {
const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => {
return err ? reject(err) : resolve(result);
}));
const mkdirp = promisify(compiler.outputFileSystem.mkdirp);
const writeFile = promisify(compiler.outputFileSystem.writeFile);

const {filepath} = this.workerOptions;

// use the outputFileSystem api to manually write service workers rather than adding to the compilation assets
return compiler.outputFileSystem.mkdirp(path.resolve(filepath, '..'),
() => compiler.outputFileSystem.writeFile(filepath, serviceWorker, callback));
return mkdirp(path.resolve(filepath, '..'))
.then(() => writeFile(filepath, serviceWorker));
}

/**
Expand Down
20 changes: 11 additions & 9 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,12 @@ test.serial('#createServiceWorker()', async t => {
plugin.configure(compiler, compilation);
return callback();
});

await runCompiler(compiler);

t.truthy(await plugin.createServiceWorker(), 'generate something');
});

test.serial('#writeServiceWorker(serviceWorker, compiler, callback)', async t => {
test.serial('#writeServiceWorker(serviceWorker, compiler)', async t => {
t.plan(2);

const filepath = path.resolve(__dirname, 'tmp/service-worker.js');
const compiler = webpack(webpackConfig());
const plugin = new SWPrecacheWebpackPlugin({filepath});
Expand All @@ -215,10 +212,12 @@ test.serial('#writeServiceWorker(serviceWorker, compiler, callback)', async t =>
plugin.apply(compiler);

compiler.plugin('after-emit', (compilation, callback) => {
plugin.writeServiceWorker(serviceWorker, compiler, callback);
plugin.writeServiceWorker(serviceWorker, compiler)
.then(() => callback())
.catch(err => callback(err));
});
await runCompiler(compiler);

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

});
Expand Down Expand Up @@ -257,7 +256,7 @@ test.serial('importScripts[<index>] should support entry point & dynamically imp
'some-script-path.js',
{filename: 'some-script-path.[hash].js'},
{chunkName: 'sw'},
{chunkName: 'service-worker-imported-script-2'}
{chunkName: 'service-worker-imported-script-2'},
],
});

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

plugin.apply(compiler);
compiler.plugin('after-emit', (compilation) => {
compiler.plugin('after-emit', (compilation, callback) => {
plugin.configure(compiler, compilation);
callback();
});

await runCompiler(compiler);

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

plugin.apply(compiler);
compiler.plugin('after-emit', (compilation) => {
compiler.plugin('after-emit', (compilation, callback) => {
plugin.configure(compiler, compilation);
callback();
});
await runCompiler(compiler);

Expand Down

0 comments on commit 41f3087

Please sign in to comment.