Skip to content
This repository was archived by the owner on Jul 9, 2021. It is now read-only.

Commit f367a00

Browse files
committed
[added] the livereload helper function
1 parent 6f1d890 commit f367a00

6 files changed

Lines changed: 64 additions & 14 deletions

File tree

interfaces/webcompiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import type {ProgramData, ProgramDataCallback, StringOrErrorCallback, ResultOrErrorCallback,
3+
import type {ProgramData, ProgramDataCallback, StringOrErrorCallback, ResultOrErrorCallback, LiveReloadTrigger,
44
ObjectOrErrorCallback, DevServerConfig, LintError, LintCallback, PostCSSWarning, NodeSassError} from '../src/typedef';
55

66
declare module 'webcompiler' {
@@ -31,6 +31,7 @@ declare module 'webcompiler' {
3131
declare function watch(dir: string, type: string, callback: () => void): void;
3232
declare function yaml(filename: string, callback: ObjectOrErrorCallback): void;
3333
declare function findBinary(name: string, callback: ResultOrErrorCallback): void;
34+
declare function livereload(): LiveReloadTrigger;
3435

3536
declare class Message {}
3637

src/DevServer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import type {DevServerConfig} from './typedef';
44
import {SASSCompiler} from './SASSCompiler';
55
import {logError, log, consoleStyles} from './logger';
6+
import {livereload} from './livereload';
67
import {watch} from './watch';
78
import {getServer} from './webpack';
8-
import tinylr from 'tiny-lr';
99
import {join} from 'path';
1010
import noop from 'lodash/noop';
1111

12-
const LIVERELOAD_PORT = 35729,
13-
WEB_PORT = 3000,
12+
const WEB_PORT = 3000,
1413
cwd = process.cwd(),
1514
{green} = consoleStyles,
1615
defaultOptions = {
@@ -124,12 +123,11 @@ export class DevServer {
124123
}
125124

126125
const sass = new SASSCompiler(false),
127-
lr = tinylr(),
126+
lr = livereload(),
128127
compileSASS = sass.fe.bind(sass, style, join(contentBase, 'style.css'), () => {
129-
lr.changed({body: {files: ['style.css']}});
128+
lr('/style.css');
130129
});
131130

132-
lr.listen(LIVERELOAD_PORT);
133131
compileSASS();
134132
watch(cwd, 'scss', compileSASS);
135133
}

src/Documentation.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import type {NativeProcess} from './NativeProcess';
44
import {join} from 'path';
55
import noop from 'lodash/noop';
66
import {logError, logSequentialSuccessMessage} from './logger';
7+
import {livereload} from './livereload';
78
import {findBinary} from './findBinary';
89
import {watch} from './watch';
9-
import tinylr from 'tiny-lr';
1010

11-
const LIVERELOAD_PORT = 35729,
12-
cwd = process.cwd(),
11+
const cwd = process.cwd(),
1312
defaultOptions = {
1413
inputDir: join(cwd, 'src'),
1514
outputDir: join(cwd, 'docs'),
@@ -99,14 +98,12 @@ export class Documentation {
9998
* @param {Function} [callback=function () {}] - a callback function
10099
*/
101100
watch(callback: () => void = noop) {
102-
const lr = tinylr();
103-
104-
lr.listen(LIVERELOAD_PORT);
101+
const lr = livereload();
105102

106103
watch(this.options.inputDir, 'js', () => {
107104
this.run(() => {
108105
callback();
109-
lr.changed({body: {files: '*'}});
106+
lr();
110107
});
111108
});
112109
this.run(callback);

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {Documentation} from './Documentation';
66
export {watch} from './watch';
77
export {yaml} from './yaml';
88
export {findBinary} from './findBinary';
9+
export {livereload} from './livereload';
910
export {consoleStyles, log, logError, logPostCSSWarnings, logSASSError, logLintingErrors,
1011
logSequentialSuccessMessage} from './logger';
1112
export {isNode, isProduction, babelBEOptions, babelFEOptions} from './util';

src/livereload.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* @flow */
2+
3+
import type {LiveReloadTrigger} from './typedef';
4+
import tinylr from 'tiny-lr';
5+
6+
const LIVERELOAD_PORT = 35729;
7+
8+
let reloadFn;
9+
10+
/**
11+
* Starts a LiveReload server and returns a function that triggers the reload.
12+
*
13+
* @function livereload
14+
* @return {LiveReloadTrigger} the trigger function
15+
* @example
16+
* <link rel="stylesheet" href="css/style.css">
17+
* @example
18+
* import {livereload} from 'webcompiler';
19+
* // or - import {livereload} from 'webcompiler/lib/livereload';
20+
* // or - var livereload = require('webcompiler').livereload;
21+
* // or - var livereload = require('webcompiler/lib/livereload').livereload;
22+
*
23+
* // initialize the server
24+
* const lr = livereload();
25+
*
26+
* // only reload the styles
27+
* lr('css/style.css');
28+
*
29+
* // refresh the whole page
30+
* lr('*');
31+
* // or simply
32+
* lr();
33+
*/
34+
export function livereload(): LiveReloadTrigger {
35+
if (!reloadFn) {
36+
const lr = tinylr();
37+
38+
lr.listen(LIVERELOAD_PORT);
39+
40+
reloadFn = (file: string = '*') => {
41+
lr.changed({body: {files: [file]}});
42+
};
43+
}
44+
45+
return reloadFn;
46+
}

src/typedef.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,10 @@ export type WatchmanResponse = {
214214
* @param {WatchmanResponse} response - a watchman response object
215215
*/
216216
export type WatchCallback = (response: WatchmanResponse) => void;
217+
218+
/**
219+
* @callback LiveReloadTrigger
220+
* @param {string} [file="*"] - a file to reload (same path as written in the HTML document that includes it) or `"*"`
221+
* for a full page refresh
222+
*/
223+
export type LiveReloadTrigger = (file?: string) => void;

0 commit comments

Comments
 (0)