mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 19:57:25 +02:00
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
This commit is contained in:
parent
608e2c85a2
commit
62a2c7c1a5
3 changed files with 38 additions and 31 deletions
|
@ -145,6 +145,7 @@ This script will build the static website, apply translations if necessary, and
|
||||||
| Options | Default | Description |
|
| Options | Default | Description |
|
||||||
| ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
| ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| `--port <number>` | `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. |
|
| `--port <number>` | `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`. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function execute(port) {
|
function execute(port, options) {
|
||||||
const extractTranslations = require('../write-translations');
|
const extractTranslations = require('../write-translations');
|
||||||
|
|
||||||
const metadataUtils = require('./metadataUtils');
|
const metadataUtils = require('./metadataUtils');
|
||||||
|
@ -568,35 +568,38 @@ function execute(port) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start LiveReload server.
|
if (options.watch) startLiveReload();
|
||||||
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
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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(['/']);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
app.listen(port);
|
app.listen(port);
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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;
|
module.exports = execute;
|
||||||
|
|
|
@ -42,7 +42,10 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) {
|
||||||
|
|
||||||
const program = require('commander');
|
const program = require('commander');
|
||||||
|
|
||||||
program.option('--port <number>', 'Specify port number').parse(process.argv);
|
program
|
||||||
|
.option('--port <number>', '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 port = parseInt(program.port, 10) || process.env.PORT || 3000;
|
||||||
let numAttempts = 0;
|
let numAttempts = 0;
|
||||||
|
@ -68,7 +71,7 @@ function checkPort() {
|
||||||
} else {
|
} else {
|
||||||
// start local server on specified port
|
// start local server on specified port
|
||||||
const server = require('./server/server.js');
|
const server = require('./server/server.js');
|
||||||
server(port);
|
server(port, program.opts());
|
||||||
const host = `http://localhost:${port}`;
|
const host = `http://localhost:${port}`;
|
||||||
console.log('Docusaurus server started on port %d', port);
|
console.log('Docusaurus server started on port %d', port);
|
||||||
openBrowser(host);
|
openBrowser(host);
|
||||||
|
|
Loading…
Add table
Reference in a new issue