Chrome中的Gulp自动刷新

6
以下代码似乎很好地运行,直到我访问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页面为何无法加载?我尝试浏览以下地址:localhost:8081/build/index.html,但该页面无法加载,显示以下消息:
{"error":"not_found","reason":"no such route"}
我还尝试使用Chrome插件,但是当我点击插件时出现以下消息:
Could not connect to LiveReload server. Please make sure that LiveReload 2.3 (or later) or another compatible server is running.
我已从Chrome插件中检查了插件设置,并勾选了文件URL选项。
下面是我的代码注释...
//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.
2个回答

7
这不是livereload的工作原理。它不运行服务器来加载您的内容 - 它运行一个单独的服务器来通知内容更改。启用livereload时,一个小的javascript嵌入到您的页面中,监听LR服务器。当您通知服务器资源已修改时,它会告诉任何监听器,这些监听器便从他们最初加载资源的位置重新加载该资源。如果您的Web应用/站点/页面完全自包含,您可以在浏览器中打开file://url以启用livereload,并且它应该能正常工作。但是,如果您正在处理外部资源,则应启动某种类型的服务器。我无法为您选择太多方式,但您可以使用connect、express或其他节点库,如果您安装了python,则可以在目录中运行python -m SimpleHTTPServer等。如果您想将connect服务器集成到构建过程中,请查看本文底部的食谱

* 你可以通过浏览器插件或使用 gulp-embedlr 插件 在开发过程中启用实时重载,我更喜欢使用后者,因为它可以跨多个浏览器和设备工作。


+1 提到了Python中的SimpleHTTPServer。直到现在我个人都不知道它。 - nyxz

0

1ocalhost:8081 还是 localhost:8081?也许是首字母拼写错误。


感谢Darryl的热心建议,不幸的是这只是一个笔误。 - Bill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接