hmreload

nice easy use live refresh you website

Usage no npm install needed!

<script type="module">
  import hmreload from 'https://cdn.skypack.dev/hmreload';
</script>

README

?# hmreload nice easy use live refresh you website

An implementation of the LiveReload server in Node.js. It's an alternative to the graphical http://livereload.com/ application, which monitors files for changes and reloads your web browser. ����������Node.jsʵ����LiveReload�ķ����ˣ���������(http://livereload.com/) �����ṩ�ķ��������������Ĺ��ܶ������ڼ����ļ��仯���Զ�ˢ��������ҳ��

Example Usage

You can use this by either adding a snippet of code to the bottom of your HTML pages or install the Browser Extensions. �������ַ�ʽ���������������ˣ�һ���ǰ�װ(http://livereload.com/) ��վ�ṩ�IJ�����һ����������HTMLҳ��β������һ��JS���롣 ps:��Ҫ˵�����ǣ�(http://livereload.com/) �����ṩ�IJ����ǿ�Դ���ѵģ������ṩ�ķ��������շѵġ�

Method 1: Add browser extension

Install the LiveReload browser plugins by visiting [chrome: https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei]). ��װLiveReload�ṩ��������������Chrome������[https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei]

Only Google Chrome supports viewing file:/// URLS, and you have to specifically enable it. If you are using other browsers and want to use file:/// URLs, add the JS code to the page as shown in the next section.

Method 2: Add code to page

Add this code:

<script>
  document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
  ':35729/livereload.js?snipver=1"></' + 'script>')
</script>

Note: If you are using a different port other than 35729 you will need to change the above script. ��HTMLҳ��body�պ�֮ǰ����������js���룬ע���������ķ����˵Ķ˿ں������޸ģ����������еĶ˿ں�ҲҪ���޸ġ�

install livereload

firstly, you should install livereload.js by bower ���ȣ�����Ҫͨ��bower��װlivereload.js

    bower install

Running hmreload

You can run LiveReload two ways: �������ַ�������hm-livereload:

Option 1: Command line

To use livereload from the command line:

    $ npm install -g hmreload
    $ hmreload [path]

��һ�ַ����������У��������������

Option 2: From within your own project

To use the api within a project: �ڶ��ַ�����Ϊ��ʹ��livereload��api���Ȱ�װ����ģ�飺

    $ npm install hmreload

Then, create a server and fire it up. Ȼ�󴴽�һ�������󣬲��𶯡�

    livereload = require('hmreload');
    server = livereload.createServer();
    server.watch(__dirname + "/public");

You can also use this with a Connect server. Here's an example of a simple server using connect and a few other modules just to give you an idea: ��Ҳ����ʹ��Connect��������������һ��ʹ��connect������ģ��������

var connect  = require('connect');
var compiler = require('connect-compiler');
var static = require('serve-static');

var server = connect();

server.use(
  compiler({
      enabled : [ 'coffee', 'uglify' ],
      src     : 'src',
      dest    : 'public'
  })
);

server.use(  static(__dirname + '/public'));

server.listen(3000);

livereload = require('hmreload');
server = livereload.createServer();
server.watch(__dirname + "/public");

You can then start up the server which will listen on port 3000. �������������ķ������ˣ������˿���3000

Watching multiple paths:

Passing an array of paths or glob patterns will allow you to watch multiple directories. All directories have the same configuration options. ������Ҫ��������Ŀ¼����������watch�IJ����д���һ�����顣

server.watch([__dirname + "/js", __dirname + "/css"]);

Using the originalPath option:

// server.js
var server = livereload.createServer({
    originalPath: "http://domain.com"
});
server.watch('/User/Workspace/test');

Then run the server: Ȼ�������𶯷�������

$ node server.js`

Then, assuming your HTML file has a stylesheet link like this: ��������HTMLҳ����һ��CSS�ļ������ӣ����£�

<!-- html -->
<head>
    <link rel="stylesheet" href="http://domain.com/css/style.css">
</head>

When /User/Workspace/test/css/style.css is modified, the stylesheet will be reload. ��/User/Workspace/test/css/style.css�����ļ��޸ĵ�ʱ�򣬻��Զ����¼����µ�css�ļ���

Command-line Options

The commandline options are ���������е�ѡ�

  • -p or --port to specify the listening port
  • -d or --debug to show debug messages when the browser reloads.
  • -e or --exts to include additional extentions that you want to observe. An example being -e 'jade scss'.
  • -u or --usepolling to poll for file system changes. Set this to true to successfully watch files over a network.

Specify the path when using the options. ʹ��ѡ����ʱ������Ҫ�ƶ�·��

$ hmreload . -i 200

API Options

The createServer() method supports a few basic options, passed as a JavaScript object:

  • https is an optional object of options to be passed to https.createServer (if not provided, http.createServer is used instead)
  • port is the listening port. It defaults to 35729 which is what the LiveReload extensions use currently.
  • exts is an array of extensions you want to observe. The default extensions are html, css, js, png, gif, jpg, php, php5, py, rb, erb, and "coffee."
  • applyJSLive tells LiveReload to reload JavaScript files in the background instead of reloading the page. The default for this is false.
  • applyCSSLive tells LiveReload to reload CSS files in the background instead of refreshing the page. The default for this is true.
  • applyImgLive tells LiveReload to reload image files in the background instead of refreshing the page. The default for this is true. Namely for these extensions: jpg, jpeg, png, gif
  • exclusions lets you specify files to ignore. By default, this includes .git/, .svn/, and .hg/
  • originalPath Set URL you use for development, e.g 'http:/domain.com', then LiveReload will proxy this url to local path.
  • overrideURL lets you specify a different host for CSS files. This lets you edit local CSS files but view a live site. See http://feedback.livereload.com/knowledgebase/articles/86220-preview-css-changes-against-a-live-site-then-uplo for details.
  • usePolling Poll for file system changes. Set this to true to successfully watch files over a network.

License

Copyright (c) 2010-2015 Chale Cao Brian P. Hogan and Joshua Peek

Released under the MIT license. See LICENSE for details.