node.js - Chrome 中的 Gulp Livereload

标签 node.js google-chrome livereload gulp

在我转到 1ocalhost:8081 之前,下面的代码似乎工作正常......

然后我收到消息..

<pre>{"tinylr":"Welcome","version":"0.0.5"}</pre>

我的目录结构是....

____gulp
| |____build
| | |____images
| | |____index.html
| | |____scripts
| | |____styles
| |____gulpfile.js
| |____node_modules
| |____src
| | |____images
| | |____index.html
| | |____scripts
| | |____styles

为什么我的 html 页面没有加载?如果我尝试浏览到 1ocalhost:8081/build/index.html 页面无法加载,我收到消息

{"error":"not_found","reason":"no such route"}

I've also tried the chrome plugin but I get the below msg when I hit the plugin

Could not connect to LiveReload server. Please make sure that LiveReload 2.3 (or later) or another compatible server is running.

I checked the plugin settings from the plugin in Chrome and check the option for file urls

Heres my commented code.....

//sudo npm install gulp -g
// install chrome extension from https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei
//Go into the settings from the plugin in Chrome and check the option for file urls: chrome://extensions/


// include gulp
var gulp = require('gulp'); 

// include plug-ins
var jshint = require('gulp-jshint');
var changed = require('gulp-changed');
var imagemin = require('gulp-imagemin');
var minifyHTML = require('gulp-minify-html');
var concat = require('gulp-concat');
var stripDebug = require('gulp-strip-debug');
var uglify = require('gulp-uglify');
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var livereload = require('gulp-livereload');
var lr = require('tiny-lr');
var server = lr();


// JS hint task
gulp.task('jshint', function() {
  gulp.src('./src/scripts/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'))
    .pipe(livereload(server));
});

// minify new images
gulp.task('imagemin', function() {
  var imgSrc = './src/images/**/*',
      imgDst = './build/images';

  gulp.src(imgSrc)
    .pipe(changed(imgDst))
    .pipe(imagemin())
    .pipe(gulp.dest(imgDst))
    .pipe(livereload(server));
});

// minify new or changed HTML pages
gulp.task('htmlpage', function() {
  var htmlSrc = './src/*.html',
      htmlDst = './build';

  gulp.src(htmlSrc)
    .pipe(changed(htmlDst))
    .pipe(minifyHTML())
    .pipe(gulp.dest(htmlDst))
    .pipe(livereload(server));
});

// JS concat, strip debugging and minify
gulp.task('scripts', function() {
  gulp.src(['./src/scripts/lib.js','./src/scripts/*.js'])
    .pipe(concat('script.js'))
    .pipe(stripDebug())
    .pipe(uglify())
    .pipe(gulp.dest('./build/scripts/'))
    .pipe(livereload(server));
});

// CSS concat, auto-prefix and minify
gulp.task('styles', function() {
  gulp.src(['./src/styles/*.css'])
    .pipe(concat('styles.css'))
    .pipe(autoprefix('last 2 versions'))
    .pipe(minifyCSS())
    .pipe(gulp.dest('./build/styles/'))
    .pipe(livereload(server));
});

// default gulp task
gulp.task('default', ['imagemin', 'htmlpage', 'scripts', 'styles'], function() {

    server.listen(8081, function (err) { if (err) return console.log(err);

          // watch for HTML changes
          gulp.watch('./src/*.html', function() {
            gulp.run('htmlpage');
          });

          // watch for JS changes
          gulp.watch('./src/scripts/*.js', function() {
            gulp.run('jshint', 'scripts');
          });

            // watch for IMG changes
          gulp.watch('./src/images/*.png', function() {
            gulp.run('imagemin');
          });

          // watch for CSS changes
          gulp.watch('./src/styles/*.css', function() {
            gulp.run('styles');
          });
    });
});
</pre>

gulp 的输出看起来不错......

Bills-MacBook-Pro:gulp Bill$ gulp
[gulp] Using file /Users/Bill/gulp/gulpfile.js
[gulp] Working directory changed to /Users/Bill/gulp
[gulp] Running 'imagemin'...
[gulp] Finished 'imagemin' in 77 ms
[gulp] Running 'htmlpage'...
[gulp] Finished 'htmlpage' in 2.47 ms
[gulp] Running 'scripts'...
[gulp] Finished 'scripts' in 4.05 ms
[gulp] Running 'styles'...
[gulp] Finished 'styles' in 1.09 ms
[gulp] Running 'default'...
[gulp] Finished 'default' in 1.38 ms
gulp.run() has been deprecated. Use task dependencies or gulp.watch task triggering instead.
[gulp] Running 'htmlpage'...
[gulp] Finished 'htmlpage' in 3.5 ms
[gulp] index.html was reloaded.
[gulp] Running 'htmlpage'...
[gulp] Finished 'htmlpage' in 712 μs
[gulp] Running 'htmlpage'...
[gulp] Finished 'htmlpage' in 1.05 ms
[gulp] index.html was reloaded.

最佳答案

这不是 livereload 的工作方式。它不运行服务器来加载您的内容 — 它运行单独的服务器以在内容更改时通知

当您启用 livereload* 时,一个小的 javascript 会嵌入到您的页面中,用于监听 LR 服务器。当您通知服务器资源已修改时,它会通知所有监听器,然后监听器会从最初加载资源的位置重新加载资源。

如果您的网络应用程序/站点/页面完全独立,您只需在浏览器中打开指向您想要的页面的 file:// url,启用 livereload,它应该可以工作。

但是,如果您要处理外部资源,则应该启动某种服务器。方法太多了,我无法为你选择一个,但你可以使用 connect、express 或其他一些 Node 库,如果你安装了 python,你可以在你的目录中运行 python -m SimpleHTTPServer,等

如果您想将connect 服务器集成到您的构建过程中,我有一个recipe at the bottom of this article。 .


* 您可以通过浏览器插件或使用 gulp-embedlr plugin 启用 livereload在开发期间,我更喜欢这样做,因为它可以跨多个浏览器和设备工作。

关于node.js - Chrome 中的 Gulp Livereload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21829524/

相关文章:

javascript - Yeoman Web 应用程序生成器 LiveReload 片段不工作

ruby-on-rails - WebSocket 连接到 'ws://localhost:35729/livereload' 失败:连接建立错误:net::ERR_CONNECTION_REFUSED

javascript - Node.js 中的 TCP 套接字和文件操作

php - 表单提交遗漏了一个值

google-chrome - 如何在 Google Chrome 中模拟捏缩放?

google-chrome - 使用 Google Chrome 扩展程序访问本地文件

gruntjs - Grunt livereload 不工作

javascript - 套接字 : Get Client sessionID at any point

node.js - 如何通过 BrowserSync 中间件设置 session cookie?

node.js - Knex 迁移不创建表