【问题标题】:grunt-autoprefixer loops endlessly when using grunt-contrib-watch使用 grunt-contrib-watch 时,grunt-autoprefixer 会无限循环
【发布时间】:2013-09-08 08:57:02
【问题描述】:

我只是在学习 咕噜声。我将使用 compass 来生成垂直节奏和图像精灵,并使用 autoprefixer 来为 css3 样式添加前缀。

这是我的 Gruntfile.js

module.exports = function(grunt) {
  var globalConfig = {
    sassDir: 'sass',
    cssDir: 'css'
  };

  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
  // Project configuration.
  grunt.initConfig({
    globalConfig: globalConfig,
    pkg: grunt.file.readJSON('package.json'),
    compass: {
      dev: {
        options: {
          sassDir: '<%= globalConfig.sassDir %>',
          cssDir: '<%= globalConfig.cssDir %>'
        }
      }
    },
    autoprefixer: {
      dev: {
        options: {
          browsers: ['last 2 versions']
        },
        src: '<%= globalConfig.cssDir %>/style.css',
        dest: '<%= globalConfig.cssDir %>/style.css'
      }
    },
    watch: {
      compass: {
        files: ['<%= globalConfig.sassDir %>/**/*.scss'],
        tasks: ['compass:dev'],
      },
      autoprefixer: {
        files: ['<%= globalConfig.cssDir %>/style.css'],
        tasks: ['autoprefixer:dev']
      },
      livereload: {
        options: { livereload: true },
        files: ['<%= globalConfig.cssDir %>/style.css']
      }
    }
  });

  // Default task(s) that will be run by invoking 'grunt' w/o args
  grunt.registerTask('styles:dev', ['compass', 'autoprefixer']);
  grunt.registerTask('default', ['styles:dev', 'watch']);
};

但每当我跑步时

grunt

除了 grunt-contrib-watch 无休止地调用 grunt-autoprefixer 之外,一切都按预期工作。

我才刚刚开始学习咕噜声。这可能是我的 Gruntfile.js

上的错误配置

希望你能帮帮我。

【问题讨论】:

    标签: gruntjs grunt-contrib-watch


    【解决方案1】:

    将您的任务配置更改为:

    watch: {
      compass: {
        files: ['<%= globalConfig.sassDir %>/**/*.scss'],
        tasks: ['compass:dev', 'autoprefixer:dev']
      },
      livereload: {
        options: { livereload: true },
        files: ['<%= globalConfig.cssDir %>/style.css']
      }
    }
    

    基本上,grunt-contrib-watch 将在文件更新时运行任务,并且由于您的源文件和目标文件相同,它会将其变成无限循环。一旦您的指南针任务构建了您的 css,只需运行自动前缀。希望这可以帮助。 :-)

    【讨论】:

    • 如果您不介意,您能分享一些配置 gruntfile.js 的最佳实践技巧吗? :) 谢谢。
    • 一旦您习惯了配置任务,您会发现大量不同的插件可以自动执行各种常见的开发工作。在这种情况下,我可以向您推荐的最佳实践并不多,您已经为公共目录设置了配置,使用了 matchdep 技巧等。我发现这一切都相当简单,只要您阅读每个插件的文档。绝对浏览 npm 存储库以获取新插件,但有很多发现:-)
    猜你喜欢
    • 2014-05-20
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2017-06-12
    • 2023-03-12
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多