Skip to content

Commit

Permalink
Actually support Browserify scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Feb 12, 2015
1 parent f9ba95b commit 14463e2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
lib/*.js
lib/*/*.js
node_modules/
*.tgz
npm-debug.log
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module.exports = function(grunt) {
},
src: ['lib/startup.js'],
dest: 'dist/livereload.js'
},

test: {
src: ['test/html/browserified/main.js'],
dest: 'test/html/browserified/bundle.js'
}
},

Expand All @@ -35,7 +40,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-mocha-test');

grunt.registerTask('build', ['coffee', 'browserify']);
grunt.registerTask('build', ['coffee', 'browserify:dist']);
grunt.registerTask('test', ['mochaTest']);
grunt.registerTask('default', ['build', 'test']);

Expand Down
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,35 @@ Would love, but doesn't seem possible:
* live JS reloading


Installing using Bower and npm
------------------------------
Installing using Bower
----------------------

This script is published on Bower and npm. (But, to reiterate: the preferred method is to avoid installing it altogether, and instead use the one bundled with your LiveReload server/app/tool.)
This script is published on Bower. (But, to reiterate: the preferred method is to avoid installing it altogether, and instead use the one bundled with your LiveReload server/app/tool.)

Using Bower:
Installation:

bower install livereload-js --save-dev

This gives you a component containing a single script file, `dist/livereload.js`.

If you're using Browserify, you can require LiveReload via npm:

Installing using npm and Browserify
-----------------------------------

Including livereload.js into your Browserify bundle probably makes no sense, because livereload.js isn't something you would ship to production.

But if you insist _and_ you know what you're doing, you can install LiveReload via npm:

npm install livereload-js --save

Note that the package uses `window` and `document` globals, so won't run under Node.js environment.
and then add this to your bundle:

window.LiveReloadOptions = { host: 'localhost' };
require('livereload-js');

Note that livereload-js package uses `window` and `document` globals, so won't run under Node.js environment.

The reason you need to specify `LiveReloadOptions` is that `livereload.js` won't be able to find its `<script>` tag and would normally bail out with an error message.


Using livereload.js
Expand Down Expand Up @@ -121,6 +134,19 @@ Alternatively, instead of loading livereload.js from the LiveReload server, you
```


Options
-------

Options can either be specified as query parameters of the `<script src="..../livereload.js">` tag's source URL, or as a global `window.LiveReloadOptions` dictionary. If the dictionary is specified, `livereload.js` does not even try looking for its `<script>` tag.

The set of supported options is the same for both methods:

* `host`: the host that runs a LiveReload server; required if specifying `LiveReloadOptions`, otherwise will be autodetected as the origin of the `<script>` tag
* `port`: optional server port override
* `mindelay`, `maxdelay`: range of reconnection delays (if `livereload.js` cannot connect to the server, it will attempt to reconnect with increasing delays); defaults to 1,000 ms minimum and 60,000 ms maximum
* `handshake_timeout`: timeout for a protocol handshake to be completed after a connection attempt; mostly only needed if you're running an interactive debugger on your web socket server


Issues & Limitations
--------------------

Expand Down Expand Up @@ -173,6 +199,10 @@ To run tests:

grunt

Manual testing: open files in `test/html/*` in various browsers, make some changes and make sure they are applied.

Testing the Browserify usage scenario: `grunt browserify:test`, then perform manual testing of `test/html/browserified/`.


Releasing a new version
-----------------------
Expand Down
12 changes: 9 additions & 3 deletions src/livereload.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ exports.LiveReload = class LiveReload
return

# i can haz options?
unless @options = Options.extract(@window.document)
@console.error("LiveReload disabled because it could not find its own <SCRIPT> tag")
return
if 'LiveReloadOptions' of window
@options = new Options()
for own k, v of window['LiveReloadOptions']
@options.set(k, v)
else
@options = Options.extract(@window.document)
unless @options
@console.error("LiveReload disabled because it could not find its own <SCRIPT> tag")
return

# i can haz reloader?
@reloader = new Reloader(@window, @console, Timer)
Expand Down
1 change: 1 addition & 0 deletions test/html/browserified/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bundle.js
6 changes: 6 additions & 0 deletions test/html/browserified/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
window.LiveReloadOptions = { host: 'localhost' };
require('../../../lib/startup.js');

window.hellow = function hellow() {
return 42;
}
3 changes: 3 additions & 0 deletions test/html/browserified/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: green;
}
11 changes: 11 additions & 0 deletions test/html/browserified/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>LiveReload Test</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<script src="bundle.js?host=localhost"></script>
<p>Hello, world == <script>document.write(hellow());</script></p>
</body>
</html>

0 comments on commit 14463e2

Please sign in to comment.