From 62a2c7c1a599ccaf04a86e9ca2ec0fbca5220f5c Mon Sep 17 00:00:00 2001 From: "=^._.^=" Date: Thu, 14 Jun 2018 23:51:03 -0700 Subject: [PATCH] Add option to disable live reload server during development (#766) * add option to disable live reload server * change live to watch, fix incorrect argument parsing code * Update api-commands.md --- docs/api-commands.md | 1 + lib/server/server.js | 59 +++++++++++++++++++++++--------------------- lib/start-server.js | 7 ++++-- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/docs/api-commands.md b/docs/api-commands.md index 7b10c40aa20b..c9bf50dc1cac 100644 --- a/docs/api-commands.md +++ b/docs/api-commands.md @@ -145,6 +145,7 @@ This script will build the static website, apply translations if necessary, and | Options | Default | Description | | ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ | | `--port ` | `3000` | The website will be served from port 3000 by default, but if the port is taken up, Docusaurus will attempt to find an available one. | +| `--watch` | - | Whether to watch the files and live reload the page when files are changed. Defaults to true. Disable this by using `--no-watch`. | --- diff --git a/lib/server/server.js b/lib/server/server.js index 3e5ac7bdc4bd..d0d94ab91f01 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -function execute(port) { +function execute(port, options) { const extractTranslations = require('../write-translations'); const metadataUtils = require('./metadataUtils'); @@ -568,35 +568,38 @@ function execute(port) { }); }); - // Start LiveReload server. - process.env.NODE_ENV = 'development'; - const server = tinylr(); - server.listen(constants.LIVE_RELOAD_PORT, function() { - console.log( - 'LiveReload server started on port %d', - constants.LIVE_RELOAD_PORT - ); - }); + if (options.watch) startLiveReload(); + app.listen(port); - // gaze watches some specified dirs and triggers a callback when they change. - gaze( - [ - '../' + readMetadata.getDocsPath() + '/**/*', // docs - '**/*', // website - '!node_modules/**/*', // node_modules - ], - function() { - // Listen for all kinds of file changes - modified/added/deleted. - this.on('all', function() { - // Notify LiveReload clients that there's a change. - // Typically, LiveReload will only refresh the changed paths, - // so we use / here to force a full-page reload. - server.notifyClients(['/']); - }); - } - ); + function startLiveReload() { + // Start LiveReload server. + process.env.NODE_ENV = 'development'; + const server = tinylr(); + server.listen(constants.LIVE_RELOAD_PORT, function() { + console.log( + 'LiveReload server started on port %d', + constants.LIVE_RELOAD_PORT + ); + }); - app.listen(port); + // gaze watches some specified dirs and triggers a callback when they change. + gaze( + [ + '../' + readMetadata.getDocsPath() + '/**/*', // docs + '**/*', // website + '!node_modules/**/*', // node_modules + ], + function() { + // Listen for all kinds of file changes - modified/added/deleted. + this.on('all', function() { + // Notify LiveReload clients that there's a change. + // Typically, LiveReload will only refresh the changed paths, + // so we use / here to force a full-page reload. + server.notifyClients(['/']); + }); + } + ); + } } module.exports = execute; diff --git a/lib/start-server.js b/lib/start-server.js index 71576f86f81e..2eb616054729 100755 --- a/lib/start-server.js +++ b/lib/start-server.js @@ -42,7 +42,10 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) { const program = require('commander'); -program.option('--port ', 'Specify port number').parse(process.argv); +program + .option('--port ', 'Specify port number') + .option('--no-watch', 'Toggle live reload file watching') + .parse(process.argv); let port = parseInt(program.port, 10) || process.env.PORT || 3000; let numAttempts = 0; @@ -68,7 +71,7 @@ function checkPort() { } else { // start local server on specified port const server = require('./server/server.js'); - server(port); + server(port, program.opts()); const host = `http://localhost:${port}`; console.log('Docusaurus server started on port %d', port); openBrowser(host);