1 | gulp-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 |
|
6 | Serve a folder over HTTP and watch it for changes, telling the browser to
|
7 | reload itself when a change happens.
|
8 |
|
9 | * Uses [socket.io](http://socket.io) - livereload mechanism works even
|
10 | if your browser does not support WebSockets (PhoneGap developers rejoice!).
|
11 |
|
12 | * `window.console` capture - it can capture `console` output from the
|
13 | client-side and transmit it to the back-end for display. This is useful for
|
14 | when testing from Phonegap, etc.
|
15 |
|
16 | * Supports [CSS injection](#livereload-behaviour) (no need to reload the whole page if just your CSS
|
17 | has 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 |
|
35 | The folder supplied to `gulp.src()` will be the root folder from which files will be served.
|
36 |
|
37 | ```js
|
38 | var gulp = require('gulp');
|
39 | var server = require('gulp-server-livereload');
|
40 |
|
41 | gulp.task('webserver', function() {
|
42 | gulp.src('app')
|
43 | .pipe(server({
|
44 | livereload: true,
|
45 | directoryListing: true,
|
46 | open: true
|
47 | }));
|
48 | });
|
49 | ```
|
50 |
|
51 | If 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
|
54 | your HTML must have a closing `</body> tag.
|
55 |
|
56 | ### Command-line
|
57 |
|
58 | Install the package globally:
|
59 |
|
60 | ```bash
|
61 | $ npm install -g gulp-server-livereload
|
62 | ```
|
63 |
|
64 | Then you can run the `livereload` command to serve files out of the current folder.
|
65 | Here are the available options:
|
66 |
|
67 | ```bash
|
68 | $ livereload help
|
69 |
|
70 | Usage: livereload [options]
|
71 |
|
72 | Options:
|
73 |
|
74 | -h, --help output usage information
|
75 | -V, --version output the version number
|
76 | -n, --no-browser do not open the localhost server in a browser
|
77 | -l, --log [type] log level (default: info)
|
78 | -p, --port <n> the port to run on
|
79 | ```
|
80 |
|
81 |
|
82 | ## Options
|
83 |
|
84 | _Note: not all of these options are currently available via the CLI executable_
|
85 |
|
86 | Key | Type | Default | Description |
|
87 | --- | --- | --- | --- |
|
88 | `host` | String | `localhost` | hostname of the webserver
|
89 | `port` | Number | `8000` | port of the webserver
|
90 | `livereload` | Boolean/Object | `false` | whether to use livereload. For advanced options, provide an object.
|
91 | `livereload.port` | Number | `35729` | port for livereload server to listen on.
|
92 | `livereload.filter` | Function | - | function to filter out files to watch (default filters out `node_modules`).
|
93 | `livereload.clientConsole` | Boolean | `false` | whether to capture `window.console` output from the client and send it to the back-end for display.
|
94 | `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.
|
95 | `defaultFile` | String | `index.html` | default file to show when root URL is requested. If `directoryListing` is enabled then this gets disabled.
|
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 |
|
104 | By default when a file changes the livereload script in the browser does the
|
105 | following:
|
106 |
|
107 | 1. Checks to see whether the changed file is a CSS file
|
108 | 2. If it is a CSS file then it reloads the changed CSS files in the browser
|
109 | 3. Otherwise it reloads the whole page
|
110 |
|
111 | To 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 | */
|
120 | window._onLiveReloadFileChanged = function(file) {
|
121 | // do whatever you want here, e.g. location.reload();
|
122 | }
|
123 | ```
|
124 |
|
125 | The `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 |
|
139 | Set `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 |
|
143 | Set the `defaultFile` to `main.html`:
|
144 |
|
145 | ```js
|
146 | gulp.task('webserver', function() {
|
147 | gulp.src('app')
|
148 | .pipe(server({
|
149 | defaultFile: 'main.html'
|
150 | }));
|
151 | });
|
152 | ```
|
153 |
|
154 | <<<<<<< HEAD
|
155 | #### How can I use livereload if my HTML is already being served up by a node.js/other app?
|
156 |
|
157 | You'll have to add some Javascript to dynamically load in the browser-side scripts.
|
158 | For example, if the `gulp-server-livereload` livereload port is set to 34322 then you would add:
|
159 |
|
160 | ```js
|
161 | (function() {
|
162 | var lrHost = location.protocol + '//' + location.hostname + ':34322';
|
163 | var s = document.createElement('script');
|
164 | s.async = true;
|
165 | s.setAttribute('src', lrHost + '/livereload.js');
|
166 | document.body.appendChild(s);
|
167 | })();
|
168 | ```
|
169 |
|
170 | To enable console logging capture add the following query paramter:
|
171 |
|
172 | ```js
|
173 | s.setAttribute('src', lrHost + '/livereload.js?extra=capture-console');
|
174 | ```
|
175 |
|
176 |
|
177 | #### How can I pass a custom filter to livereload?
|
178 |
|
179 | In the `livereload` object, set the `enable` to `true` and provide filter function in `filter`:
|
180 |
|
181 | ```js
|
182 | gulp.task('webserver', function() {
|
183 | gulp.src('app')
|
184 | .pipe(server({
|
185 | livereload: {
|
186 | enable: true,
|
187 | filter: function(filePath, cb) {
|
188 | cb( !(/node_modules/.test(filePath)) );
|
189 | }
|
190 | }
|
191 | }));
|
192 | });
|
193 | ```
|
194 |
|
195 |
|
196 | =======
|
197 | >>>>>>> xuan9/master
|
198 | ## License
|
199 |
|
200 | MIT - see LICENSE.md
|