详解Grunt插件之LiveReload实现页面自动刷新(两种方案)

http://www.jb51.net/article/70415.htm    含Grunt系列教程javascript

这篇文章主要经过两种方案详解Grunt插件之LiveReload实现页面自动刷新,须要的朋友能够参考下css

 

方案一:grunt-livereload + Chrome Plug-inhtml

优势:安装、配置简单方便。
缺点:须要配合指定的浏览器插件(Firefox也有相关插件,IE么你懂的)。java

1. 须要安装2个插接件:grunt-contrib-watch、connect-livereloadweb

执行命令:npm

复制代码 代码以下:

npm install --save-dev grunt-contrib-watch connect-livereload

2. 安装浏览器插件:Chrome LiveReloadjson

3. 配置一个Web服务器(IIS/Apache),LiveReload须要在本地服务器环境下运行(对file:///文件路径支持并非很好)。浏览器

4. 修改Gruntfile.js文件:服务器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module.exports = function (grunt) {
  // 项目配置(任务配置)
  grunt.initConfig({
   pkg: grunt.file.readJSON( 'package.json' ),
   watch: {
    client: {
     files: [ '*.html' , 'css/*' , 'js/*' , 'images/**/*' ],
     options: {
      livereload: true
     }
    }
   }
  });
  // 加载插件
  grunt.loadNpmTasks( 'grunt-contrib-watch' );
  // 自定义任务
  grunt.registerTask( 'live' , [ 'watch' ]);
};

5. 执行:grunt livegrunt

看到以下提示,说明已经开始监放任务:

复制代码 代码以下:

Running "watch" task
Waiting...

6. 打开咱们的页面,例如:http://localhost/

7. 再点击Chrome LiveReload插件的ICON,此时ICON圆圈中心的小圆点变成实心的,说明插件执行成功。此时你改下网站文件看看,是否是实时更新了?

 

方案二:grunt-contrib-watch + grunt-contrib-connect + grunt-livereload

优势:自动搭建静态文件服务器,不需在本身电脑上搭建Web服务器。
   不须要浏览器插件的支持(不现定于某个浏览器)。
    不须要给网页手动添加livereload.js。
缺点:对于刚接触的人,配置略显复杂。

1. 安装咱们所须要的3个插件:grunt-contrib-watch、grunt-contrib-connect、connect-livereload

执行命令:

复制代码 代码以下:

npm install --save-dev grunt-contrib-watch grunt-contrib-connect connect-livereload

2. 修改Gruntfile.js文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module.exports = function (grunt) {
  // LiveReload的默认端口号,你也能够改为你想要的端口号
  var lrPort = 35729;
  // 使用connect-livereload模块,生成一个与LiveReload脚本
  // <script src="http://127.0.0.1:35729/livereload.js?snipver=1" type="text/javascript"></script>
  var lrSnippet = require( 'connect-livereload' )({ port: lrPort });
  // 使用 middleware(中间件),就必须关闭 LiveReload 的浏览器插件
  var lrMiddleware = function (connect, options) {
   return [
    // 把脚本,注入到静态文件中
    lrSnippet,
    // 静态文件服务器的路径
    connect.static(options.base[0]),
    // 启用目录浏览(至关于IIS中的目录浏览)
    connect.directory(options.base[0])
   ];
  };
  // 项目配置(任务配置)
  grunt.initConfig({
   // 读取咱们的项目配置并存储到pkg属性中
   pkg: grunt.file.readJSON( 'package.json' ),
   // 经过connect任务,建立一个静态服务器
   connect: {
    options: {
     // 服务器端口号
     port: 8000,
     // 服务器地址(可使用主机名localhost,也能使用IP)
     hostname: 'localhost' ,
     // 物理路径(默认为. 即根目录) 注:使用'.'或'..'为路径的时,可能会返回403 Forbidden. 此时将该值改成相对路径 如:/grunt/reloard。
     base: '.'
    },
    livereload: {
     options: {
      // 经过LiveReload脚本,让页面从新加载。
      middleware: lrMiddleware
     }
    }
   },
   // 经过watch任务,来监听文件是否有更改
   watch: {
    client: {
     // 咱们不须要配置额外的任务,watch任务已经内建LiveReload浏览器刷新的代码片断。
     options: {
      livereload: lrPort
     },
     // '**' 表示包含全部的子目录
     // '*' 表示包含全部的文件
     files: [ '*.html' , 'css/*' , 'js/*' , 'images/**/*' ]
    }
   }
  }); // grunt.initConfig配置完毕
  // 加载插件
  grunt.loadNpmTasks( 'grunt-contrib-connect' );
  grunt.loadNpmTasks( 'grunt-contrib-watch' );
  // 自定义任务
  grunt.registerTask( 'live' , [ 'connect' , 'watch' ]);
};

5. 执行:grunt live

看到以下提示,说明Web服务器搭建完成,而且开始监放任务:

复制代码 代码以下:

Running "connect:livereload" (connect) task
Started connect web server on 127.0.0.1:8000.

Running "watch" task
Waiting...

注:执行该命令前,若是你有安装过LiveReload的浏览器插件,必须关闭。

6. 打开咱们的页面,例如:http://localhost:8000/http://127.0.0.1:8000/
注:这里所打开的本地服务器地址,是咱们刚才经过connect所搭建的静态文件服务器地址,而不是以前你用IIS或Apache本身搭建Web服务器地址。

以上就是本文详解Grunt插件之LiveReload实现页面自动刷新(两种方案),但愿你们喜欢。

相关文章
相关标签/搜索