【问题标题】:Livereload of index.html with Gulp使用 Gulp 重新加载 index.html
【发布时间】:2014-03-07 22:33:32
【问题描述】:

当我对项目根目录中的 index.html 进行更改时,我正在尝试更新我的浏览器。更新 CSS 时,Livereloading 工作正常。 我已经删除了我为在我的 gulpfile.js 中工作所做的努力,如下...

谢谢!

var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
clean = require('gulp-clean'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
lr = require('tiny-lr'),
server = lr();

gulp.task('styles', function() {
return gulp.src('components/sass/style.scss')
.pipe(sass({ style: 'expanded' }))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6',         'android 4'))
.pipe(gulp.dest(''))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest(''))
.pipe(livereload(server))
.pipe(notify({ message: 'Styles task complete' }));
});

gulp.task('scripts', function() {
return gulp.src('components/js/*.js')
// .pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('main.js'))
.pipe(gulp.dest('js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js'))
.pipe(livereload(server))
.pipe(notify({ message: 'Scripts task complete' }));

});

gulp.task('images', function() {
return gulp.src('components/img/*')
.pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
.pipe(gulp.dest('img'))
.pipe(livereload(server))
.pipe(notify({ message: 'Images task complete' }));
});

// gulp.task('clean', function() {
//   return gulp.src(['css', 'js', 'img'], {read: false})
//     .pipe(clean());
// });

gulp.task('watch', function() {

server.listen(35729, function (err) {
if (err) {
return console.log(err)
};
// Watch .scss files
gulp.watch('components/sass/*.scss', ['styles']);
// Watch .js files
gulp.watch('components/js/*.js', ['scripts']);
// Watch image files
gulp.watch('components/img/*', ['images']);
});    
});

// gulp.task('default', ['clean'], function() {
gulp.task('default', function() {
gulp.start('styles', 'scripts', 'images');
});

【问题讨论】:

    标签: livereload gulp


    【解决方案1】:

    使用截至 4/2016 的当前 gulp 语法在项目根目录中手动重新加载特定文件类型的浏览器同步重新加载,此处引用:

    https://www.browsersync.io/docs/gulp/#gulp-manual-reload

    示例:在 SCSS 引用下方添加行以通过浏览器同步观看 HTML 和 HTM 文件:

    gulp.watch("./scss/*.scss", ['sass']);
    gulp.watch("*.html").on("change", reload);
    gulp.watch("*.htm").on("change", reload);
    

    【讨论】:

      【解决方案2】:

      如果您不想使用 livereload 或 express 实例,您可以执行以下操作,我在我的 laravel 5 项目中使用 elixir gulp 扩展并结合 live.js

      延长灵药

      elixir.extend('livereloader',function(message) { gulp.task('livereloader',function(){ return gulp.src('public/js/livereloader.js') .pipe(insert.append('')) .pipe(gulp.dest('public/js/')); }); this.registerWatcher("livereloader", 'resources/views/**/*.php'); return this.queueTask('livereloader'); });

      使用任务

      mix.livereloader();

      足够简单。即使没有 livereload 或 express(不明白为什么我应该使用它们...)

      【讨论】:

        【解决方案3】:

        可能有更好的方法来做你想做的事。

        您是否考虑过将express 的实例与livereload 一起使用?

        另一种方法是使用gulp-connect,这将使您尝试做的事情变得更加容易。在任何一种情况下,不要在任务中调用livereload,而是在设置服务器的任务中处理livereloading。一般的想法是,您在要实时重新加载的文件上设置监视任务,然后在更改时重新加载。

        gulp.task('serve', function(event) {
            connect.server({
                root: destinations.docs,
                port: 1987,
                livereload: true
            });
            // sets up a livereload that watches for any changes in the root
            watch({glob: [sources.html, sources.styles]})
                .pipe(connect.reload());
        });
        

        我已经尝试了这两种方法,gulp-connect 是一个更简单的选择。无论如何,我已经编写了两个 gulp 样板,您可以在 https://github.com/jh3y/gulp-boilerplate-v2https://github.com/jh3y/gulp-boilerplate 看到它们。

        希望有帮助!

        【讨论】:

          【解决方案4】:

          您可以尝试将您的 html 文件放入 components/html 之类的子文件夹中,并使用类似于样式任务的任务:

          gulp.task('html', function() {
              return gulp.src('components/html/**/*.html')
                  .pipe(gulp.dest(''))
                  .pipe(livereload(server))
                  .pipe(notify({ message: 'HTML task complete' }));
          });
          

          您的默认任务应如下所示:

          // gulp.task('default', ['clean'], function() {
          gulp.task('default', function() {
              gulp.start('styles', 'scripts', 'images', 'html');
          });
          

          并修改你的监视任务:

          gulp.task('watch', function() {
          
              server.listen(35729, function (err) {
                  if (err) {
                      return console.log(err)
                  };
                  // Watch .scss files
                  gulp.watch('components/sass/*.scss', ['styles']);
                  // Watch .js files
                  gulp.watch('components/js/*.js', ['scripts']);
                  // Watch image files
                  gulp.watch('components/img/*', ['images']);
                  // Watch html files
                  gulp.watch('components/html/**/*.html', ['html']);
              });
          });
          

          Ciao 拉尔夫

          【讨论】:

          • 它适用于我,但所有 html 文件都会重新加载,而不仅仅是那些有变化的文件。有什么想法只重新加载有变化的吗?
          猜你喜欢
          • 2016-02-15
          • 1970-01-01
          • 2014-07-28
          • 1970-01-01
          • 1970-01-01
          • 2015-09-11
          • 2020-04-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多