0

几天来我一直在尝试配置这个 gruntfile,我已经看过并阅读了多个关于如何设置它的文档。我已经能够设置它,因此我可以通过在 bash 中输入单个命令来运行单个任务:

grunt uglify grunt sass grunt cssmin

但是 grunt watch 似乎没有看我的任何 js 文件

所有这些都可以正常工作,但是当我尝试在“监视”任务下运行它们时,我的 js 文件都被忽略了。我的命令行只是显示“等待...”。更改我的 js 文件中的任何内容都不会触发更新。我也有运行 jshint 的 toruble,因为它一直告诉我它正在寻找一个字符串等,所以我现在禁用了那个插件。这是我的文件:

module.exports = function (grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    sass: { // Task 
      dist: { // Target        
        files: { // Dictionary of files 
          'assets/css/style.css': 'assets/scss/style.scss'
          // 'destination': 'source' 

        }
      }
    },

    cssmin: {
      options: {
        banner: '/*\n <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") 
      %> \n*/\n'
      },
      build: {
        files: {
          'assets/css/style.min.css': 'assets/css/style.css'
        }
      }
    },
    uglify: {
      options: {
        banner: '/*\n <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") 
      %> \n*/\n'
      },
      build: {
        files: {
          'assets/js/magic.min.js': ['assets/js/magic.js', 
           'assets/js/server.js', 'assets/js/script.js']
        }
      }
    },
    // configure jshint to validate js files -------------------------------
    // jshint: {
    //   options: {
    //     reporter: require('jshint-stylish')  

    //   },

    //   // when this task is run, lint the Gruntfile and all js files in 
    src
    //   build: ['Gruntfile.js', 'assets/**/*.js']
    // },


    watch: {

      // for stylesheets, watch css and less files 
      // only run less and cssmin stylesheets: { 
      files: ['assets/**/*.css', 'assets/**/*.less'],
      tasks: ['sass', 'cssmin']
    },

    // for scripts, run jshint and uglify 
    scripts: {
      files: 'assets/**/*.js',
      tasks: 'uglify'
    }
  });
  // Default tasks
  //grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('default', ['uglify', 'cssmin', 'sass', 'watch']);
};
4

1 回答 1

1

我想你想这样做吗?

watch: {
    stylesheets: { 
      files: ['assets/**/*.css', 'assets/**/*.less'],
      tasks: ['sass', 'cssmin']
    },

    // for scripts, run jshint and uglify 
    scripts: {
      files: 'assets/**/*.js',
      tasks: 'uglify'
    }
  }
于 2017-08-29T06:27:18.630 回答