UNPKG

8 kBMarkdownView Raw
1gulp-server-livereload [![Build Status](http://img.shields.io/travis/hiddentao/gulp-server-livereload.svg?style=flat)](https://travis-ci.org/hiddentao/gulp-server-livereload)
2==============
3
4> Gulp plugin to run a local webserver with live reload using socket.io
5
6Serve a folder over HTTP and watch it for changes, telling the browser to
7reload itself when a change happens.
8
9* Uses [socket.io](http://socket.io) - livereload mechanism works even
10if your browser does not support WebSockets (PhoneGap developers rejoice!).
11
12* `window.console` capture - it can capture `console` output from the
13client-side and transmit it to the back-end for display. This is useful for
14when testing from Phonegap, etc.
15
16* Supports [CSS injection](#livereload-behaviour) (no need to reload the whole page if just your CSS
17has changed).
18
19* Proxy mode - proxy requests arriving at certain URLs to other servers.
20
21* Comes with a command-line runnable.
22
23
24_This was originally a fork of [gulp-webserver](https://github.com/schickling/gulp-webserver)._
25
26
27## Installation
28
29```sh
30$ npm install --save-dev gulp-server-livereload
31```
32
33## Usage
34
35The folder supplied to `gulp.src()` will be the root folder from which files will be served.
36
37```js
38var gulp = require('gulp');
39var server = require('gulp-server-livereload');
40
41gulp.task('webserver', function() {
42 gulp.src('app')
43 .pipe(server({
44 livereload: true,
45 directoryListing: true,
46 open: true
47 }));
48});
49```
50
51If you run `gulp webserver` your browser should automatically open up to `http://localhost:8000` and show a directory listing of the `app` folder.
52
53**Note:** In order for the livereload browser-side code to be injected properly your HTML must have a closing `</body>` tag.
54
55### Command-line
56
57Install the package globally:
58
59```bash
60$ npm install -g gulp-server-livereload
61```
62
63Then you can run the `livereload` command to serve files out of the current folder.
64Here are the available options:
65
66```bash
67$ livereload help
68
69 Usage: livereload [options]
70
71 Options:
72
73 -h, --help output usage information
74 -V, --version output the version number
75 -n, --no-browser do not open the localhost server in a browser
76 -l, --log [type] log level (default: info)
77 -p, --port <n> the port to run on
78```
79
80
81## Options
82
83_Note: not all of these options are currently available via the CLI executable_
84
85Key | Type | Default | Description |
86--- | --- | --- | --- |
87`host` | String | `localhost` | hostname of the webserver
88`port` | Number | `8000` | port of the webserver
89`livereload` | Boolean/Object | `false` | whether to use livereload. For advanced options, provide an object.
90`livereload.port` | Number | `35729` | port for livereload server to listen on.
91`livereload.filter` | Function | - | function to filter out files to watch (default filters out `node_modules`).
92`livereload.clientConsole` | Boolean | `false` | whether to capture `window.console` output from the client and send it to the back-end for display.
93`directoryListing` | Boolean/Object | `false` | whether to display a directory listing. For advanced options, provide an object. You can use the `path property to set a custom path or the `options` property to set custom [serve-index](https://github.com/expressjs/serve-index) options.
94`defaultFile` | String | `index.html` | default file to show when root URL is requested. If `directoryListing` is enabled then this gets disabled.
95`fallback` | String | `undefined` | file to fall back to (relative to webserver root) when requested resource not found. Useful when building single-page apps with non-has URLs.
96`open` | Boolean/Object | `false` | open the localhost server in the browser
97`https` | Boolean/Object | `false` | whether to use https or not. By default, `gulp-server-livereload` provides you with a development certificate but you remain free to specify a path for your key and certificate by providing an object like this one: `{key: 'path/to/key.pem', cert: 'path/to/cert.pem'}`.
98`log` | String | `info` | If set to `debug` you will see all requests logged to the console.
99`proxies` | Array | `[]`| a list of proxy objects. Each proxy object can be specified by `{source: '/abc', target: 'http://localhost:8080/abc', options: {headers: {'ABC_HEADER': 'abc'}}}`.
100
101
102## Livereload behaviour
103
104By default when a file changes the livereload script in the browser does the
105following:
106
1071. Checks to see whether the changed file is a CSS file
1082. If it is a CSS file then it reloads the changed CSS files in the browser
1093. Otherwise it reloads the whole page
110
111To override the default behaviour define the following method in Javascript:
112
113```js
114/**
115 * This method gets called by the livereload script when the server notifies it
116 * that something has changed.
117 *
118 * @param {Object} file File which changed.
119 */
120window._onLiveReloadFileChanged = function(file) {
121 // do whatever you want here, e.g. location.reload();
122}
123```
124
125The `file` parameter has the following structure:
126
127```js
128{
129 "path": ...full path to file which changed...
130 "name": ...file name (without path)...
131 "ext": ...file extension name...
132}
133```
134
135## FAQ
136
137#### Why can't I reach the server from the network?
138
139Set `0.0.0.0` as the `host` option.
140
141#### How can I set main.html to automatically load when I visit the URL?
142
143Set the `defaultFile` to `main.html`:
144
145```js
146gulp.task('webserver', function() {
147 gulp.src('app')
148 .pipe(server({
149 defaultFile: 'main.html'
150 }));
151});
152```
153
154#### How can I use livereload if my HTML is already being served up by a node.js/other app?
155
156You'll have to add some Javascript to dynamically load in the browser-side scripts.
157For example, if the `gulp-server-livereload` livereload port is set to 34322 then you would add:
158
159```js
160(function() {
161 var lrHost = location.protocol + '//' + location.hostname + ':34322';
162 var s = document.createElement('script');
163 s.async = true;
164 s.setAttribute('src', lrHost + '/livereload.js');
165 document.body.appendChild(s);
166})();
167```
168
169To enable console logging capture add the following query paramter:
170
171```js
172s.setAttribute('src', lrHost + '/livereload.js?extra=capture-console');
173```
174
175
176#### How can I pass a custom filter to livereload?
177
178In the `livereload` object, set the `enable` to `true` and provide filter function in `filter`:
179
180```js
181gulp.task('webserver', function() {
182 gulp.src('app')
183 .pipe(server({
184 livereload: {
185 enable: true,
186 filter: function(filePath, cb) {
187 cb( !(/node_modules/.test(filePath)) );
188 }
189 }
190 }));
191});
192```
193
194#### How can I use non-hash URLs for my single page app (i.e. HTML5 mode) with this plugin?
195
196When you're building a single-page app with non-hash URLs (html5 mode) then you
197want the server to always serve up the same file for every URL. This is where
198the `fallback` option comes into play:
199
200```js
201gulp.task('webserver', function() {
202 gulp.src('app')
203 .pipe(server({
204 fallback: 'index.html'
205 }));
206});
207```
208
209### How can I use this with CSS preprocessors such as LESS or SASS?
210
211If you use a CSS preprocessor in gulp, you'll need to run its gulp task (typically with `gulp-watch`) together with the server, so that LESS/SASS files are compiled as you save.
212
213You'll also want to configure livereload to ignore changes to the source files, and instead let it only handle changes to the compiled CSS (which will be refreshed inline).
214
215```js
216var watch = require('gulp-watch');
217
218gulp.task('watch', function () {
219 watch('./styles/*.less', batch(function (events, done) {
220 gulp.start('default', done);
221 }));
222});
223
224gulp.task('webserver', ['watch'], function () {
225 gulp.src('.')
226 .pipe(server({
227 livereload: {
228 enable: true,
229 filter: function (filename, cb) {
230 cb(!/\.(sa|le)ss$|node_modules/.test(filename);
231 }
232 },
233 directoryListing: true,
234 open: true
235 }));
236});
237```
238
239_Note: the livereload server automatically handles generated sourcemap files properly so don't worry about them_.
240
241## License
242
243MIT - see LICENSE.md