UNPKG

7.59 kBMarkdownView Raw
1gulp-live-server
2===
3
4[![status][1]][2] [![downloads][3]][4] [![tag][5]][6] [![license][7]][8]
5
6[1]: http://img.shields.io/travis/gimm/gulp-live-server/master.svg?style=flat-square
7[2]: https://travis-ci.org/gimm/gulp-live-server
8
9[3]: http://img.shields.io/npm/dm/gulp-live-server.svg?style=flat-square
10[4]: https://www.npmjs.com/package/gulp-live-server
11
12[5]: https://img.shields.io/github/tag/gimm/gulp-live-server.svg?style=flat-square
13[6]: https://github.com/gimm/gulp-live-server/releases
14
15[7]: http://img.shields.io/badge/license-WTFPL-blue.svg?style=flat-square
16[8]: http://www.wtfpl.net
17
18A handy, light-weight server you're going to love.
19
20- [Install](#install)
21- [Usage](#usage)
22- [API](#api)
23 - [static](#staticfolder-port)
24 - [new](#newscript)
25 - [gls](#glsargs-options-livereload)
26 - [start](#start)
27 - [stop](#stop)
28 - [notify](#notifyevent)
29- [livereload.js](#livereloadjs)
30- [Debug](#debug)
31
32Install
33---
34[![NPM](https://nodei.co/npm/gulp-live-server.png?compact=true)](https://nodei.co/npm/gulp-live-server/)
35
36Usage
37---
38- Serve a static folder(`gls.script`<'scripts/static.js'> is used as server script)
39
40 ```js
41 var gulp = require('gulp');
42 var gls = require('gulp-live-server');
43 gulp.task('serve', function() {
44 //1. serve with default settings
45 var server = gls.static(); //equals to gls.static('public', 3000);
46 server.start();
47
48 //2. serve at custom port
49 var server = gls.static('dist', 8888);
50 server.start();
51
52 //3. serve multi folders
53 var server = gls.static(['dist', '.tmp']);
54 server.start();
55
56 //use gulp.watch to trigger server actions(notify, start or stop)
57 gulp.watch(['static/**/*.css', 'static/**/*.html'], function () {
58 server.notify.apply(server, arguments);
59 });
60 });
61 ```
62- Serve with your own script file
63
64 ```js
65 gulp.task('serve', function() {
66 //1. run your script as a server
67 var server = gls.new('myapp.js');
68 server.start();
69
70 //2. run script with cwd args, e.g. the harmony flag
71 var server = gls.new(['--harmony', 'myapp.js']);
72 //this will achieve `node --harmony myapp.js`
73 //you can access cwd args in `myapp.js` via `process.argv`
74 server.start();
75
76 //use gulp.watch to trigger server actions(notify, start or stop)
77 gulp.watch(['static/**/*.css', 'static/**/*.html'], function () {
78 server.notify.apply(server, arguments);
79 });
80 gulp.watch('myapp.js', server.start.bind(server)); //restart my server
81 });
82 ```
83
84- Customized serving with gls
85
86 ```js
87 gulp.task('serve', function() {
88 //1. gls is the base for `static` and `new`
89 var server = gls([gls.script, 'static', 8000]);
90 //equals gls.new([gls.script, 'static', 8000]);
91 //equals gls.static('static', 8000);
92 server.start();
93
94 //2. set running options for the server, e.g. NODE_ENV
95 var server = gls('myapp.js', {env: {NODE_ENV: 'development'}});
96 server.start();
97
98 //3. customize livereload server, e.g. port number
99 var server = gls('myapp.js', undefined, 12345);
100 var promise = server.start();
101 //optionally handle the server process exiting
102 promise.then(function(result) {
103 //log, exit, re-start, etc...
104 });
105
106 //4. start with coffee-script executable e.g. installed with npm
107 var server = gls('myapp.coffee');
108 server.start('node_modules/coffee-script/bin/coffee');
109
110 //use gulp.watch to trigger server actions(notify, start or stop)
111 gulp.watch(['static/**/*.css', 'static/**/*.html'], function () {
112 server.notify.apply(server, arguments);
113 });
114 gulp.watch('myapp.js', server.start.bind(server)); //restart my server
115 });
116 ```
117
118API
119---
120### static([folder][, port])
121- `folder` - `String|Array` The folder(s) to serve.
122 Use array of strings if there're multi folders to serve.
123 If omitted, defaults to `public/`.
124- `port` - `Number` The port to listen on. Defaults to `3000`.
125- return [gls](#glsargs-options-livereload).
126
127Config new server using the [default server script](https://github.com/gimm/gulp-live-server/blob/master/scripts/static.js), to serve the given `folder` on the specified `port`.
128
129### new(script)
130- `script` - `String` The script file to run.
131- return [gls](#glsargs-options-livereload).
132
133Config new server using the given `script`.
134
135### gls(args[, options][, livereload])
136- `args` - `String|Array` The 2nd param for [ChildProcess.spawn](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
137- `options` - `Object` The 3rd param for [ChildProcess.spawn](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options),
138will be mixin into the default value:
139
140 ```js
141 options = {
142 cwd: undefined
143 }
144 options.env = process.env;
145 options.env.NODE_ENV = 'development';
146 ```
147- `livereload` - `Boolean|Number|Object` The option for tiny-lr server. The default value is `35729`.
148 - `false` - will disable tiny-lr livereload server.
149 - `number` - treated as port number of livereload server.
150 - `object` - used to create tiny-lr server new tinylr.Server(livereload);
151
152**`gls` here is a reference of `var gls = require('gulp-live-server')`**. It aims to assemble configuration for the server child process as well as the tiny-lr server.
153**`static` and `new` are just shortcuts for this.**
154Usually, `static` and `new` will serve you well, but you can get more customized server with `gls`.
155
156### start([execPath])
157- `execPath` - `String` The executable that is used to start the server. If none is given the current node executable is used.
158- return [promise](https://github.com/kriskowal/q/wiki/API-Reference) from [Q](https://www.npmjs.com/package/q), resolved with the server process exits.
159
160Spawn a new child process based on the configuration.
161- use [`ChildProcess.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) to start a node process;
162- use [`tiny-lr`](https://github.com/mklabs/tiny-lr) provide livereload ability;
163
164### stop()
165- return [promise](https://github.com/kriskowal/q/wiki/API-Reference) from [Q](https://www.npmjs.com/package/q)
166
167Stop the server.
168
169### notify([event])
170- `event` - `Event` Event object passed along with [gulp.watch](https://github.com/gulpjs/gulp/blob/master/docs/API.md#cbevent).
171Optional when used with `pipe`.
172
173Tell livereload.js to reload the changed resource(s)
174
175livereload.js
176---
177gulp-live-server comes with [tiny-lr](https://github.com/mklabs/tiny-lr/) built in, which works as a livereload server. `livereload.js` is **served** by `tiny-lr`, but in order to get it loaded with your page, you have 3 options( to **inject** `<script src="//localhost:35729/livereload.js"></script>` into your page):
178- [LiveReload](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei?hl=en) for Chrome;
179- Use [connect-livereload](https://github.com/intesso/connect-livereload) middleware;
180- Add [livereload.js](https://github.com/livereload/livereload-js) in your page mannully;
181
182Usually, if `http://livereload:35729/livereload.js` is accessible, then your livereload server is ok, if you don't have the script tag for livereload.js in you page, you've problem with either your chrome plugin or the connect-livereload middle-ware as mentioned above.
183
184DEBUG
185---
186If you want more output, set the `DEBUG` environment variables to `*` or `gulp-live-server`.