Grunt Watch:每次SASS错误后需要保存两次

时间:2015-12-16 20:51:49

标签: sass gruntjs grunt-contrib-watch

在编辑我的SASS文件时使用grunt-watch时,如果我收到SASS错误,我必须在纠正错误后保存两次才能解决。这是事件的顺序:

  1. 我添加了一个在我的SASS文件中不存在的mixin并保存
  2. grunt-watch抛出SASS错误
  3. 我修正错误并保存
  4. grunt-watch抛出相同的错误
  5. 我再次保存
  6. grunt-watch正确编译
  7. 这是我的Gruntfile.js

    module.exports = function(grunt) {
    
        // Configuration
        grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
    
            imagemin: {
                dynamic: {
                    files: [{
                        expand: true,
                        cwd: 'assets/img',
                        src: ['assets/img/**/*.{png,jpg,gif}'],
                        dest: 'assets/img'
                    }]
                }
            },
            sass: {
                dist: {
                  options: {
                    loadPath: require('node-neat').includePaths,
                    style: 'compact',
                    lineNumbers: true,
                    cacheLocation: 'assets/sass/.sass-cache'
                  },
                  files: [{
                    expand: true,
                    cwd: 'assets/sass',
                    src: ['*.scss'],
                    dest: 'assets/css',
                    ext: '.css'
                  }]
                }
              },
            watch: {
                options: {
                    livereload: true
                },
                css: {
                    files: ['assets/sass/**/*.scss'],
                    tasks: ['newer:sass'],
                    options: {
                        spawn: false
                    }
                },
                images: {
                    files: ['assets/img/**/*.{png,jpg,gif}'],
                    tasks: ['imagemin'],
                    options: {
                        spawn: false
                    }
                },
                js: {
                    files: ['assets/js/**/*.js'],
                    options: {
                        spawn: false
                    }
                },
                html: {
                    files: ['*.html'],
                    options: {
                        spawn: false
                    }
                },
                php: {
                    files:['**/*.php'],
                    options: {
                        spawn: false
                    }
                }
            }
    
        });
    
        // List plugins we're using
        grunt.loadNpmTasks('grunt-contrib-watch'); // Watch - http://goo.gl/yxNE0
        grunt.loadNpmTasks('grunt-contrib-imagemin'); // Image Minify - http://goo.gl/mkIRPE
        grunt.loadNpmTasks('grunt-contrib-sass'); // SASS - http://goo.gl/pCHySn
        grunt.loadNpmTasks('grunt-newer'); // Newer - https://goo.gl/3vBTnf
    
        // Plugins to run when we run the 'grunt' command
        grunt.registerTask('default', [
            'imagemin',
            'sass'
        ]);
    
    };
    

1 个答案:

答案 0 :(得分:0)

事实证明Watch中的神秘spawn选项实际上需要为此特定实例设置为true。像大多数人一样,我真的不知道spawn是如何工作的,但它解决了这个问题。

    watch: {
        css: {
            files: ['assets/sass/**/*.scss'],
            tasks: ['newer:sass'],
            options: {
                spawn: true
            }
        }
    }

您不必将选项明确设置为true,默认情况下设置它,因此您根本不能添加它。我只是把它包括在这里作为一个例子。