From 400d18e3bbfb5a540012e12458763946503006c8 Mon Sep 17 00:00:00 2001 From: Jonas Pommerening Date: Wed, 13 Sep 2017 12:14:07 +0200 Subject: [PATCH] (#9) fix server startup and teardown in normal compile mode --- CHANGELOG.md | 2 ++ plugin.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e15ce5..991ebfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Last Changes +- [#9](https://github.com/aixigo/pdfreactor-loader/issues/9): Fix server startup and teardown inconsistencies + ## v0.3.0 diff --git a/plugin.js b/plugin.js index 4a12e23..15f94b4 100644 --- a/plugin.js +++ b/plugin.js @@ -16,6 +16,7 @@ module.exports = function( options ) { apply( compiler ) { const context = ( options && options.context ) || compiler.context; const middleware = []; + let watching = false; if( Array.isArray( options.middleware ) ) { middleware.push.apply( middleware, options.middleware ); @@ -49,6 +50,8 @@ module.exports = function( options ) { middleware.forEach( server.use ); } ); compiler.plugin( 'watch-run', ( watcher, callback ) => { + watching = true; + server.listen( err => { const baseUrl = `http://localhost:${server.address().port}/`; if( !err ) { @@ -63,7 +66,14 @@ module.exports = function( options ) { callback(); } ); compiler.plugin( 'before-run', ( compiler, callback ) => { - server.listen( callback ); + if( !watching ) { + server.listen( callback ); + } + } ); + compiler.plugin( 'done', stats => { + if( !watching ) { + server.close(); + } } ); compiler.plugin( 'compilation', compilation => { compilation.plugin( 'normal-module-loader', ( loaderContext, module ) => {