简体   繁体   English

运行第一个任务后,Grunt watch未检测到对文件所做的更改

[英]Grunt watch is not detecting the changes made to the files after the first task is run

I have a simple Gruntfile configured to run sass, then autoprefix (postcss), and finally cssmin. 我有一个简单的Gruntfile配置为运行sass,然后运行autoprefix(postcss),最后运行cssmin。 It's supposed to run the tasks in that order after detecting a change in a scss file and the consequent changes (after running sass) in the resulting css file. 在检测到scss文件中的更改以及生成的css文件中的相应更改(运行sass之后)之后,应该按此顺序运行任务。 However, it is only running the sass task. 但是,它仅运行sass任务。 Here is my gruntfile: 这是我的gruntfile:

module.exports = function(grunt) {

  grunt.initConfig({

    watch: {
      sass: {
        files: 'dev/scss/**/*.scss', 
        tasks: ['sass']
      },
      autoprefix: {
        files: 'dist/css/main.css',
        tasks: ['postcss']
      },
      cssmin: {
        files: 'dist/css/main-prefixed.css',
        tasks: ['cssmin']
      },
      js_concat: {
        files: 'dev/scripts/**/*.js',
        tasks: ['concat']
      },
      js_uglify: {
        files: 'dist/scripts/built.js',
        tasks: ['uglify']
      }
    },

    sass: {
      dev: {
        files: {
          'dist/css/main.css' : 'dev/scss/main.scss'
        }
      }
    },

    cssmin: {
      build: {
        src: 'dist/css/main-prefixed.css',
        dest: 'dist/css/main.min.css'
      }    
    },

    postcss: {
      options: {
        map: true,
        processors: [
          require('autoprefixer')({browsers: ['last 2 versions']})
        ]
      },
      dist: {
        src: 'dist/css/main.css',
        dest: 'dist/css/main-prefixed.css'
      }
    },

    concat: {
      options: {
        separator: '\n\n\n'
      },
      dist: {
        src: ['dev/scripts/*.js'],
        dest: 'dist/scripts/built.js'
      }
    },

    uglify: {
      build: {
        files: {
          'dist/scripts/built.min.js': ['dist/scripts/built.js']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-postcss');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('default', ['watch']);

};

And this is the file structure: 这是文件结构:

Gruntfile.js
dev/
  |__ scss/
        |__ (all scss partials and main.scss file)
dist/
  |__ css/
        |__ main.css
        |__ main-prefixed.css
        |__ main.min.css

When I run the tasks manually it works without problem. 当我手动运行任务时,它可以正常工作。

What might be the issue here? 这里可能是什么问题?

No sure why you're getting these results, but you can try to solve it by configuring your watch task a bit different. 不确定为什么会得到这些结果,但是您可以尝试通过配置监视任务稍有不同来解决问题。 If you detect a change in your scss files, just run the chain of commands one after the other (instead of relying on watch to see the resulted change of each task and act): 如果在scss文件中检测到更改,则一个接一个地运行命令链(而不是依靠监视来查看每个任务和操作的结果更改):

watch: {
  sass: {
    files: 'dev/scss/**/*.scss', 
    tasks: ['sass', 'postcss', 'cssmin'] // This will run the 3 tasks one after the other.
  },
  js_concat: {
    files: 'dev/scripts/**/*.js',
    tasks: ['concat']
  },
  js_uglify: {
    files: 'dist/scripts/built.js',
    tasks: ['uglify']
  }
},

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

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