简体   繁体   English

Gulp监视任务不适用于SASS

[英]Gulp Watch Task not working for SASS

My gulp watch task is working for JS, but not for my SASS. 我的gulp watch任务适用于JS,但不适用于我的SASS。 It's only compiling the new CSS file when I re-run my gulp command. 当我重新运行gulp命令时,它只会编译新的CSS文件。

Here's my 'styles' and 'watch' tasks code, any help would be appreciated: 这是我的“样式”和“观看”任务代码,任何帮助将不胜感激:

var gulp = require('gulp'),
uglify = require('gulp-uglify'),
sass = require('gulp-ruby-sass'),
livereload = require('gulp-livereload');

function errorLog(error) {
  console.error.bind(error);
  this.emit('end');
}

// Scripts task
// Uglifies js
gulp.task('scripts', function() {
  gulp.src('js/*.js')
    .pipe(uglify())
    .on('error', errorLog)
    .pipe(gulp.dest('build/js'));
});

// Styles Task
// Uglifies scss/css
gulp.task('styles', function() {
 return sass('sass/*.scss', {
  style: 'compressed'
 })
  .on('error', errorLog)
  .pipe(gulp.dest('build/css'))
  .pipe(livereload());
});

// Watch Task
// Watches JS
gulp.task('watch', function() {

  var server = livereload();

  gulp.watch('js/*.js', ['scripts']);
  gulp.watch('sass/*.scss', ['styles']);
});

gulp.task('default', ['scripts', 'styles', 'watch']);

Your question is a little light on details, but I think you could make some improvements to your code which will help. 您的问题有点细节,但是我认为您可以对代码进行一些改进,这将有所帮助。 You are only including files in the root of the js and sass directories (this might be intentional). 您仅将文件包括在jssass目录的根目录中(这可能是有意的)。

This means if changes are made in js/myfolder/somefile.js the watch task will run. 这意味着,如果在js/myfolder/somefile.js中进行了更改,则watch任务将运行。 Likewise if you have changes made to a file sass/myfolder/somestyle.scss or partial sass/myfolder/_mypartial.scss the task gets run. 同样,如果您对文件sass/myfolder/somestyle.scss或部分sass/myfolder/_mypartial.scsssass/myfolder/_mypartial.scss则任务将运行。

You might only want the root directory, if so, ignore the double asterisk below and just stick with what you have. 您可能只需要根目录,如果需要,请忽略下面的双星号,并坚持使用现有目录。 I personally use a double asterisk as it is rare all your files are just in the root directory (especially using Sass and partials). 我个人使用双星号,因为很少会把所有文件都放在根目录中(尤其是使用Sass和partials)。

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

    livereload.listen();

    gulp.watch('js/**/*.js', ['scripts']);
    gulp.watch('sass/**/*.scss', ['styles']);
});

Notice above how I added livereload.listen() this is the recommended way the plugin works. 注意上面我如何添加livereload.listen()这是插件工作的推荐方式。 What you were doing would not work properly and changes not updated via LiveReload. 您正在执行的操作无法正常工作,并且更改无法通过LiveReload更新。

My advice would also be to store your file paths in variables. 我的建议也是将文件路径存储在变量中。 You have a little bit of duplicate in your file (which is fine, but makes for harder to maintain Gulp files). 您的文件中有一些重复项(很好,但是使维护Gulp文件更加困难)。

I realise my recommendations go beyond the scope of your question, the fix for your question specifically is to use livereload.listen() in your watch task. 我意识到我的建议超出了您的问题范围,您的问题的解决方法特别是在watch任务中使用livereload.listen() The rest is sage advice based on what has worked for me the last couple of years. 其余的是根据最近几年对我有用的建议提供的明智建议。

My final recommendation would be something like this: 我的最终建议是这样的:

var gulp = require('gulp'),
uglify = require('gulp-uglify'),
sass = require('gulp-ruby-sass'),
livereload = require('gulp-livereload');

var outputDir = 'build/';

var path = {
    styles: 'sass/**/*.scss',
    stylesOutput: outputDir + 'css',
    js: 'js/**/*.js',
    jsOutput: outputDir + 'js'
};

function errorLog(error) {
  console.error.bind(error);
  this.emit('end');
}

// Scripts task
// Uglifies js
gulp.task('scripts', function() {
  gulp.src(path.js)
    .pipe(uglify())
    .on('error', errorLog)
    .pipe(gulp.dest(path.jsOutput));
});

// Styles Task
// Uglifies scss/css
gulp.task('styles', function() {
 return sass(path.styles, {
  style: 'compressed'
 })
  .on('error', errorLog)
  .pipe(gulp.dest(path.stylesOutput))
  .pipe(livereload());
});

// Watch Task
// Watches JS
gulp.task('watch', function() {

  var server = livereload();

  gulp.watch(path.js, ['scripts']);
  gulp.watch(path.styles, ['styles']);
});

gulp.task('default', ['scripts', 'styles', 'watch']);

Check your directory and globs for your sass. 检查目录和glob中是否存在Sass。 In my example below, I used an object for the paths but I had the commented out as my main since thats the file importing the partials. 在下面的示例中,我使用了一个对象作为路径,但是我注释掉了它作为主要对象,因为那是导入部分文件的文件。 It works for a single $ gulp sass but when I would watch nothing happened. 它适用于单个$ gulp sass ,但当我什么也没看到的时候。 I just need to tell gulp task to check all file ending with .sass. 我只需要告诉gulp任务来检查所有以.sass结尾的文件。 Simple but its easy to miss sometimes when doing a lot. 简单,但有时做很多事情时容易错过。

var paths = {
sass: ['./src/sass/**/*.sass'],
//sass: ['./src/sass/main.sass'],
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM