【问题标题】:different grunt watch tasks and triggering events不同的 grunt watch 任务和触发事件
【发布时间】:2013-09-11 20:20:42
【问题描述】:

我尝试使用两个监视任务,一个用于 html 和 css,它(应该)只触发重新加载(也不会发生),另一个由 .js 文件触发。在一个事件中,我尝试仅对更改的文件进行 uglify,然后连接所有 js 文件。它不起作用,无论哪个文件更改,所有 js 文件始终都是 uglifies。刚接触 Grunt,有人能抽出一两分钟吗?

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        separator: ';'
      },
      files: {
        src: ['js/minified/jquery.min.js',
          'js/minified/jquery-migrate.min.js',
          'js/minified/jquery-ui-1.10.3.custom.min.js',
          'js/minified/jquery.imagesloaded.min.js',
          'js/minified/video.js',
          'js/minified/bigvideo.js',
          'js/minified/jquery.cycle.all.js',
          'js/minified/jquery.maximage.js',
          'js/minified/jquery.jfeed.js',
          'js/minified/moment.min.js',
          'js/minified/fastclick.js',
          'js/minified/cookies.min.js',
          'js/minified/jquery.hammer.min.js',
          'js/minified/index.js'
        ],
        dest: '<%= pkg.name %>.js'
      },
    },
    uglify: {
      files: {
        src: 'js/*.js', // source files mask
        dest: 'js/minified', // destination folder
        expand: true, // allow dynamic building
        flatten: true, // remove all unnecessary nesting
      }
    },
    jshint: {
      files: ['gruntfile.js', 'js/index.js'],
      options: {
        // options here to override JSHint defaults
        globals: {
          jQuery: true,
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      options: {
        livereload: true
      },
      javascript: {
        files: ['js/*.js', 'gruntfile.js'],
        tasks: ['watchtask'],
        options: {
          nospawn: true,
        }
      },
      therest: {
        files: ['index.html', 'css/*.css'],
        tasks: []
      }
    },
    open: {
      dev: {
        path: 'http://127.0.0.1:8000'
      },
    },
    connect: {
      server: {
        options: {
          hostname: '*',
          port: 8000
        }
      }
    }
  });

  grunt.event.on('watch', function(action, filepath) {
    //change the source in the uglify task at run time so that it affects the changed file only
    grunt.config(['uglify', 'files', 'src'], filepath);
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-open');

  grunt.registerTask('default', ['jshint', 'uglify', 'concat', 'connect', 'open', 'watch']);
  grunt.registerTask('watchtask', ['jshint', 'uglify', 'concat']);

};

【问题讨论】:

  • 我确实收到以下错误:Running "watch" task Waiting...2013-09-11 22:25 node[10342] (CarbonCore.framework) FSEventStreamFlushSync(): failed assertion '(SInt64)last_id &gt; 0LL'
  • Mac OS 10.8.4,节点 0.10.18,grunt 0.4.1

标签: gruntjs grunt-contrib-watch


【解决方案1】:

这是预期的行为。当watch:javascript 任务文件列表中的任何文件更改时,将执行关联的任务。你的 watchtask 运行 uglify 丑化了所有的 JS 代码。您需要为不同的 JS 文件指定单独的监视目标并单独的 uglify 目标来执行您的建议。

例如:

...
  uglify: {
    someTarget: {
      files: {
        src: 'js/file1.js', // source files mask
        dest: 'js/minified', // destination folder
        expand: true, // allow dynamic building
        flatten: true, // remove all unnecessary nesting
      }
    },
    otherTarget: {
      files: {
        src: 'js/file2.js', // source files mask
        dest: 'js/minified', // destination folder
        expand: true, // allow dynamic building
        flatten: true, // remove all unnecessary nesting
      }
    }
  },
  ...
  watch: {
    options: {
      livereload: true
    },
    javascriptOne: {
  files: ['js/file1.js'],
      tasks: ['uglify:someTarget'],
      options: {
        nospawn: true,
      }
    },
    javascriptTwo: {
      files: ['js/file2.js'],
      tasks: ['uglify:otherTarget'],
      options: {
        nospawn: true,
      }
    },
    ...
  },
...

【讨论】:

  • 我真的不明白我的设置有什么问题。是不是不能看一些文件,触发一个任务去丑化一些其他的文件?
  • 有点......一个监视任务说“当这些匹配文件之一发生变化时,执行这个任务”。所以对于你来说,它说当js 文件夹中的任何文件和.js 的扩展名被更改时,运行watchtask - 这反过来运行uglifyconcat 我猜这是丑化和连接 所有 JS 文件。如果您想在文件 X 更改时 对其进行 uglify,那么您将不得不单独观察它,然后运行仅对该文件进行 uglify 的任务。
  • 当然,您总是可以尝试编写一个插件来完成您的要求,甚至可能没有那么难。 ;)
猜你喜欢
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2016-08-08
  • 1970-01-01
相关资源
最近更新 更多