RENEWING THE GRUNT & LIVERELOAD MAGIC

简介: <p style="margin:0px auto 1.7143em; font-size:1.2em; color:rgb(102,93,84); font-family:ratio,sans-serif; background-color:rgb(230,219,208)"> 英文原文:<a target="_blank" href="http://rhumaric.com/2013

英文原文:http://rhumaric.com/2013/07/renewing-the-grunt-livereload-magic/

不久前, 我写了一篇 explaining how to use grunt and livereload的文章,讲述当HTML, CSS or javascipt发生变化或者改动时,如何自动刷新页面.grunt-livereload插件已经被弃用了,但是我们同样能得到其带来的魔力.看看是如何做的吧!

相同点

如之前那篇文章, 我们将配置grunt为:

  • 载入一个服务来支持你的项目文件 (helps inject the Javascript needed for livereload to work);
  • 打开浏览器访问你的项目 (optional, but saves a little time);
  • 最重要的是当index.html发生改变时自动重新加载页面.

如之前的文章那样,我们会让事情简单一点. 项目中只有index.html文件 (内容任意, 但是确保有一个 <body> 标签).同样的, 你需要在你的机器上安装 Grunt JS . 这也就是说你也需要安装 NodeJS and NPM .

不同点

和之前文章中不同的是,我们需要使用到不同的nodejs 包. 去掉grunt-livereload, 增加grunt-watch (它将替换同样了grunt-regarde).没有grunt-livereload, 我们同样需要注入用于重新加载页面的Javascript. 这就是为什么我们会将grunt-contrib-connect 换成 grunt-express (他有一个可选项可以实现此功能). 换完之后的清单如下:

package.json文件就变成:

1234567891011
 
        
{
"name": "grunt-livereload-renewed",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"matchdep": "~0.1.2",
"grunt-express": "~1.0.0-beta2",
"grunt-contrib-watch": "~0.5.1",
"grunt-open": "~0.2.1"
}
}

我们现在可以在命令行进入项目根目录中执行 npm install安装这些包 .完成后我们就可以配置Grunt了

SERVING AND RECEIVING服务并接受

在深入研究livereload之前, 让我们先看一下其中的一部分逻辑:加载一个服务并自动打开浏览器. Grunt 的配置和之前文章很相似,因为grunt-express和grunt-contrib-connect具有相似的选项 (自少满足了我们的需求). 下面是followingGruntfile.js文件的内容 (to place at the root of your project):

12345678910111213141516171819202122232425262728293031323334353637383940
 
        
// Gruntfile with the configuration of grunt-express and grunt-open. No livereload yet!
module. exports = function( grunt) {
 
// Load Grunt tasks declared in the package.json file
require( 'matchdep').filterDev( 'grunt-*').forEach(grunt.loadNpmTasks);
 
// Configure Grunt
grunt.initConfig({
 
// grunt-express will serve the files from the folders listed in `bases`
// on specified `port` and `hostname`
express : {
all : {
options : {
port : 9000,
hostname : "0.0.0.0",
bases : [ __dirname] // Replace with the directory you want the files served from
// Make sure you don't use `.` or `..` in the path as Express
// is likely to return 403 Forbidden responses if you do
// http://stackoverflow.com/questions/14594121/express-res-sendfile-throwing-forbidden-error
}
}
},
 
// grunt-open will open your browser at the project's URL
open : {
all : {
// Gets the port from the connect configuration
path : 'http://localhost:<%= express.all.options.port%>'
}
}
});
 
// Creates the `server` task
grunt.registerTask( 'server', [
'express',
'open',
'express-keepalive'
]);
};

完成了配置后,在你的命令行中输入grunt server(from the folder of the project, of course)将会在9000端口启动服务并打开http://localhost:9000,这样你就可以访问你的项目了(浏览器中).

自动重新加载

Livereload 通过基于文件的改动通知自动刷新页面,这就说:

  • 当文件被改动时某人需要通知livereload: 这就是grunt-contrib-watch 插件所扮演的角色 (它同样会由livereload启动)
  • 你将需要在你的页面中获取变化后的新内容. 这正是 grunt-express 提供了这样的服务.

这两个任务通过使用livereload都很容易实现, 它提供了一个livereload 选项. 将其设置为true 或者你想要livereload服务运行的端口号(这种情况下要主要要和express设置的端口一致)).

下面就是Gruntfile最后的被容:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
 
        
// Gruntfile with the configuration of grunt-express and grunt-open. No livereload yet!
module. exports = function( grunt) {
 
// Load Grunt tasks declared in the package.json file
require( 'matchdep').filterDev( 'grunt-*').forEach(grunt.loadNpmTasks);
 
// Configure Grunt
grunt.initConfig({
 
// grunt-express will serve the files from the folders listed in `bases`
// on specified `port` and `hostname`
express : {
all : {
options : {
port : 9000,
hostname : "0.0.0.0",
bases : [ __dirname], // Replace with the directory you want the files served from
// Make sure you don't use `.` or `..` in the path as Express
// is likely to return 403 Forbidden responses if you do
// http://stackoverflow.com/questions/14594121/express-res-sendfile-throwing-forbidden-error
livereload : true
}
}
},
 
// grunt-watch will monitor the projects files
watch : {
all : {
// Replace with whatever file you want to trigger the update from
// Either as a String for a single entry
// or an Array of String for multiple entries
// You can use globing patterns like `css/**/*.css`
// See https://github.com/gruntjs/grunt-contrib-watch#files
files : 'index.html',
options : {
livereload : true
}
}
},
 
// grunt-open will open your browser at the project's URL
open : {
all : {
// Gets the port from the connect configuration
path : 'http://localhost:<%= express.all.options.port%>'
}
}
});
 
// Creates the `server` task
grunt.registerTask( 'server', [
'express',
'open',
'watch'
]);
};

当grunt服务启动后, 如果你改动了index.html文件, 浏览器将自动重新加载页面. 模拟器可以监听多个文件,但是需要在任务中配置。文档 中有解释.

注意: 如果你在Ubuntu中执行此任务时(或者是其他的linux的发行版本) 并遇到一个监听 ENOSPC 错误, 你也许需要查看它的 Github 说明.

注意: 似乎grunt-express提供了一个监听 选项 来模拟文件的变化并触发livereload. 这样会让Gruntfile更简洁一些. 但是,我更愿意配置一个单独的grunt-contrib-watch任务. 因为我经常需要它去模拟其他文件达到其他目的(YUIDoc, LESS…). 并将概念也进行了区分: grunt-express 提供文件服务, grunt-contrib-watch 模拟他们. 这样很容易找到改动之前的位置, 最后 :这只是个人的偏好)

总结

grunt-contrib-livereload被弃用, 但是livereload通过grunt仍然移植的很好. 更欣慰的是, 其他插件配合他同样做的很好,这样就会让配置更简洁.最后我要感谢 Chandler Van De Water 通知我旧的插件被弃用了,并且让我们享受livereload的魔力吧!

相关文章
|
移动开发 JavaScript 前端开发
Element Plus for Vue 3 入门教程
Element Plus 是为了适配 Vue 3 对 Element UI 进行的重构。Vue 3.0 的这次大版本升级,对于第三方组件库来说是一件好事,那些已经修修补补无数次,还无法彻底解决的问题,在这次面向 Vue 3.0 重构时,一次性全部解决。
4250 0
|
XML 缓存 前端开发
Android 架构之 MVI 初级体 | Flow 替换 LiveData 重构数据链路(下)
Android 架构之 MVI 初级体 | Flow 替换 LiveData 重构数据链路
1173 0
|
传感器 存储 定位技术
一起玩转树莓派(18)——MPU6050陀螺仪加速度传感器模块应用
现在智能手机的功能已经非常强大,除了基础的通信功能外,测位测速,空间角度等数据的测量也非常方便,这在线路导航,地图,体感游戏等应用中十分重要。不知你是否想过,智能设备是如何获取到其所在的空间状态与加速度等数据的呢?MPU6050就是提供这类数据测量的一种传感器模块。
3254 0
|
缓存 openCL 算法
关于实现Halcon算法加速的基础知识(2)(多核并行/GPU)
关于实现Halcon算法加速的基础知识(多核并行/GPU)
4833 0
关于实现Halcon算法加速的基础知识(2)(多核并行/GPU)
|
安全 生物认证 网络安全
windows10无法设置默认保存位置怎么办?显示错误代码0x80070002怎么解决?
Win10系统下载文件时,默认会保存在特定位置,但用户可自行修改。若更改后仍无效,可通过删除目标磁盘中的特定文件夹、修改注册表权限、“干净启动”排除干扰软件或使用第三方修复工具等方式解决此问题。
2034 0
|
人工智能 运维 专有云
持续领先!阿里云入选2025年Gartner®分布式混合基础设施魔力象限
近日,Gartner发布2025年《分布式混合基础设施魔力象限》报告,在混合云场景下,阿里云凭借飞天企业版(Apsara Stack)、边缘云ENS和云盒CloudBox产品组合能力,在“执行能力”和“愿景完整性”两大维度分别处于亚太厂商中最高最远的位置。
929 6
|
11月前
|
缓存 API 数据安全/隐私保护
淘宝京东拼多多API应用差异大揭秘,商家必看!
本文深度解析淘宝、京东、拼多多三大电商API在接口风格、权限控制、数据推送等方面的差异,涵盖实战代码与成本优化策略,助力商家高效对接平台系统,提升运营效率。
|
11月前
|
存储 缓存 算法
哈希表核心原理
本文深入剖析哈希表底层原理,澄清常见误区:Map是接口,哈希表是实现。通过哈希函数将key映射为数组索引,实现O(1)增删查改。详解哈希冲突的两种解决方式——拉链法与开放寻址法,探讨负载因子、扩容机制及遍历顺序无序的原因。强调不可变类型作key的重要性,避免因hashCode变化导致键值对丢失。助你真正理解哈希表的工作机制。
381 0
|
消息中间件 缓存 监控
获取商品券后价接口设计与实现
电商券后价计算接口,支持商品页、购物车等多场景实时定价。规范入参出参,结合缓存、分布式锁与批量处理,保障高性能与一致性。核心算法精准叠加优惠,辅以安全校验与监控体系,已支撑12000+ QPS,平均响应78ms。
|
存储 C语言
【C语言】输入/输出函数详解
在C语言中,输入/输出操作是通过标准库函数来实现的。这些函数分为两类:标准输入输出函数和文件输入输出函数。
2067 6