From 750276b3f695b2a1fb3fd5361c1b8eee8f4246cd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 27 Apr 2017 21:14:31 +0430 Subject: [PATCH] refactor client watch Fixes duplicate READY message --- lib/build.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/build.js b/lib/build.js index fdbe87637d..45a2c1f88a 100644 --- a/lib/build.js +++ b/lib/build.js @@ -139,8 +139,8 @@ export function * build () { function * buildFiles () { if (this.dev) { debug('Adding webpack middleware...') - const clientCompiler = createWebpackMiddleware.call(this) - webpackWatchAndUpdate.call(this, clientCompiler) + createWebpackMiddleware.call(this) + webpackWatchAndUpdate.call(this) watchPages.call(this) } else { debug('Building files...') @@ -386,6 +386,7 @@ function createWebpackMiddleware () { }) ) const clientCompiler = webpack(clientConfig) + this.clientCompiler = clientCompiler // Add the middleware to the instance context this.webpackDevMiddleware = pify(require('webpack-dev-middleware')(clientCompiler, { publicPath: clientConfig.output.publicPath, @@ -406,14 +407,14 @@ function createWebpackMiddleware () { interpolate: /{{([\s\S]+?)}}/g }) } + this.watchHandler() }) - return clientCompiler } -function webpackWatchAndUpdate (clientCompiler) { +function webpackWatchAndUpdate () { const MFS = require('memory-fs') // <- dependencies of webpack const serverFS = new MFS() - const clientFS = clientCompiler.outputFileSystem + const clientFS = this.clientCompiler.outputFileSystem const serverConfig = getWebpackServerConfig.call(this) const serverCompiler = webpack(serverConfig) const bundlePath = join(serverConfig.output.path, 'server-bundle.json') @@ -428,13 +429,13 @@ function webpackWatchAndUpdate (clientCompiler) { } else if (!manifestExists) { debug('Waiting for client manifest...') } else { - const bundle = serverFS.readFileSync(bundlePath, 'utf-8') - const manifest = clientFS.readFileSync(manifestPath, 'utf-8') + const bundle = serverFS.readFileSync(bundlePath, 'utf8') + const manifest = clientFS.readFileSync(manifestPath, 'utf8') createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest)) } } + this.watchHandler = watchHandler this.webpackServerWatcher = serverCompiler.watch(this.options.watchers.webpack, watchHandler) - this.webpackClientWatcher = clientCompiler.watch(this.options.watchers.webpack, watchHandler) } function webpackRunClient () {