Skip to content

Commit

Permalink
Add LiveReload (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyrlam authored and JoelMarcey committed May 17, 2018
1 parent fed67dc commit f9a0907
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 9 deletions.
2 changes: 1 addition & 1 deletion admin/testing-changes-on-Docusaurus-itself.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Feel free to contribute debug instructions for other IDEs

### Observing changes

Now that the server is running, you can make changes to the core Docusaurus code and docs to see the effects on the Docusaurus site. Just refresh the local site, usually running at http://localhost:3000
Now that the server is running, you can make changes to the core Docusaurus code and docs to see the effects on the Docusaurus site. LiveReload will reflect changes to the local site in your browser, usually running at http://localhost:3000
11 changes: 10 additions & 1 deletion lib/core/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

const React = require('react');
const fs = require('fs');
const classNames = require('classnames');

const HeaderNav = require('./nav/HeaderNav.js');
const Head = require('./Head.js');
const Footer = require(process.cwd() + '/core/Footer.js');
const translation = require('../server/translation.js');
const classNames = require('classnames');
const constants = require('./constants');

const CWD = process.cwd();

Expand Down Expand Up @@ -159,6 +161,13 @@ class Site extends React.Component {
}}
/>
))}
{process.env.NODE_ENV === 'development' && (
<script
src={`http://localhost:${
constants.LIVE_RELOAD_PORT
}/livereload.js`}
/>
)}
</body>
</html>
);
Expand Down
10 changes: 10 additions & 0 deletions lib/core/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
LIVE_RELOAD_PORT: 35729,
};
31 changes: 31 additions & 0 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function execute(port) {
const mkdirp = require('mkdirp');
const glob = require('glob');
const chalk = require('chalk');
const gaze = require('gaze');
const tinylr = require('tiny-lr');

const constants = require('../core/constants');
const translate = require('./translate');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');

Expand Down Expand Up @@ -541,6 +545,33 @@ 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
);
});

// gaze watches some specified dirs and triggers a callback when they change.
gaze(
[
'../docs/**/*', // docs
'**/*', // website
],
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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function checkPort() {
const server = require('./server/server.js');
server(port);
const host = `http://localhost:${port}`;
console.log(`Starting Docusaurus server on ${host}`);
console.log('Docusaurus server started on port %d', port);
opn(host);
}
})
Expand Down
129 changes: 129 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"express": "^4.15.3",
"feed": "^1.1.0",
"fs-extra": "^5.0.0",
"gaze": "^1.1.2",
"glob": "^7.1.2",
"highlight.js": "^9.12.0",
"imagemin": "^5.3.1",
Expand All @@ -58,7 +59,8 @@
"shelljs": "^0.7.8",
"sitemap": "^1.13.0",
"tcp-port-used": "^0.1.2",
"tree-node-cli": "^1.1.1"
"tree-node-cli": "^1.1.1",
"tiny-lr": "^1.1.1"
},
"bin": {
"docusaurus-start": "./lib/start-server.js",
Expand Down
Loading

0 comments on commit f9a0907

Please sign in to comment.