我正在努力获得Grunt的“实时重载”功能(已在中实现grunt-contrib-watch
)才能在我的应用中工作。我终于忍住了子弹,并尝试提出一个最小的例子。希望有人可以轻松注意到所缺少的内容。
档案结构:
├── Gruntfile.js
├── package.json
├── index.html
package.json
{
"name": "livereloadTest",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-watch": "~0.5.3"
}
}
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
src: {
files: ['*.html'],
options: { livereload: true }
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
};
index.html
<!doctype html>
<html>
<head><title>Test</title></head>
<body>
<p>....</p>
<script src="//localhost:35729/livereload.js"></script>
</body>
</html>
然后我跑步grunt watch
,什么都没炸。但是,没有浏览器窗口会自动打开(应该吗?)。
当我打开chrome时,http://localhost:35729/
我收到以下json:
{"tinylr":"Welcome","version":"0.0.4"}
并尝试在该端口上的任何其他路径给我
{"error":"not_found","reason":"no such route"}
http://localhost:35729/
是实时重新加载服务器的URL。它仅用于管理实时重载,而不用于实际网站。
通常,人们会使用grunt-contrib-connect为grunt服务静态站点。然后通过转到localhost:8000或您将其配置为驻留的任何位置来查看其站点。但是根据您的需要,也可以是apache,nginx等提供文件。
livereload
在grunt-contrib-connect上也有一个选项。这只会将<script src="//localhost:35729/livereload.js"></script>
标记注入HTML,而不会注入其他标记。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句