From 1cefff81944f8bbf601d87143096be8fe2a46a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 25 Mar 2017 15:16:07 +0100 Subject: [PATCH] Add watchers option and url after build --- bin/nuxt-dev | 2 +- lib/build.js | 19 ++++++++++++++----- lib/nuxt.js | 4 ++++ lib/server.js | 2 +- yarn.lock | 4 ++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bin/nuxt-dev b/bin/nuxt-dev index ad484a5e56..9b847bcd08 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -63,6 +63,6 @@ function listenOnConfigChanges (nuxt, server) { }) }, 200) var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js') - chokidar.watch(nuxtConfigFile, { ignoreInitial: true }) + chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true })) .on('all', build) } diff --git a/lib/build.js b/lib/build.js index a265c0b835..ca772683b3 100644 --- a/lib/build.js +++ b/lib/build.js @@ -7,6 +7,7 @@ import fs from 'fs-extra' import hash from 'hash-sum' import pify from 'pify' import webpack from 'webpack' +import PostCompilePlugin from 'post-compile-webpack-plugin' import serialize from 'serialize-javascript' import { createBundleRenderer } from 'vue-server-renderer' import { join, resolve, sep } from 'path' @@ -370,11 +371,18 @@ function getWebpackServerConfig () { function createWebpackMiddleware () { const clientConfig = getWebpackClientConfig.call(this) + const host = process.env.HOST || process.env.npm_package_config_nuxt_port || '127.0.0.1' + const port = process.env.PORT || process.env.npm_package_config_nuxt_host || '3000' // setup on the fly compilation + hot-reload clientConfig.entry.app = _.flatten(['webpack-hot-middleware/client?reload=true', clientConfig.entry.app]) clientConfig.plugins.push( new webpack.HotModuleReplacementPlugin(), - new webpack.NoEmitOnErrorsPlugin() + new webpack.NoEmitOnErrorsPlugin(), + new PostCompilePlugin(stats => { + if (!stats.hasErrors() && !stats.hasWarnings()) { + console.log(`> Open http://${host}:${port}\n`) // eslint-disable-line no-console + } + }) ) const clientCompiler = webpack(clientConfig) // Add the middleware to the instance context @@ -382,7 +390,8 @@ function createWebpackMiddleware () { publicPath: clientConfig.output.publicPath, stats: webpackStats, quiet: true, - noInfo: true + noInfo: true, + watchOptions: this.options.watchers.webpack })) this.webpackHotMiddleware = pify(require('webpack-hot-middleware')(clientCompiler, { log: () => {} @@ -406,7 +415,7 @@ function webpackWatchAndUpdate () { const serverCompiler = webpack(serverConfig) const outputPath = join(serverConfig.output.path, 'server-bundle.json') serverCompiler.outputFileSystem = mfs - this.webpackServerWatcher = serverCompiler.watch({}, (err) => { + this.webpackServerWatcher = serverCompiler.watch(this.options.watchers.webpack, (err) => { if (err) throw err createRenderer.call(this, JSON.parse(mfs.readFileSync(outputPath, 'utf-8'))) }) @@ -471,9 +480,9 @@ function watchPages () { r(this.srcDir, 'layouts/*.vue'), r(this.srcDir, 'layouts/**/*.vue') ] - const options = { + const options = Object.assign({}, this.options.watchers.chokidar, { ignoreInitial: true - } + }) /* istanbul ignore next */ const refreshFiles = _.debounce(() => { co(generateRoutesAndFiles.bind(this)) diff --git a/lib/nuxt.js b/lib/nuxt.js index 26ce30a368..52e2c3ac64 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -46,6 +46,10 @@ class Nuxt { }, prefetch: true }, + watchers: { + webpack: {}, + chokidar: {} + }, build: {} } // Sanitization diff --git a/lib/server.js b/lib/server.js index 9f93754a21..86a03760a9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -15,7 +15,7 @@ class Server { } listen (port, host) { - host = host || 'localhost' + host = host || '127.0.0.1' port = port || 3000 this.server.listen(port, host, () => { console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console diff --git a/yarn.lock b/yarn.lock index 186c0f8bcb..8e97bbe9ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4347,6 +4347,10 @@ pluralize@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45" +post-compile-webpack-plugin: + version "0.1.1" + resolved "https://registry.yarnpkg.com/post-compile-webpack-plugin/-/post-compile-webpack-plugin-0.1.1.tgz#1b1a0eea890ce748556ca49e066a48c900e0b370" + postcss-calc@^5.2.0: version "5.3.1" resolved "https://registry.yarnpkg.com/postcss-calc/-/postcss-calc-5.3.1.tgz#77bae7ca928ad85716e2fda42f261bf7c1d65b5e"